Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help guys. Integer variables post

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Help guys. Integer variables post

    Hey guys. I need help. Im java newbie and i write small program. I want post my integer variables in the next class. Here a code(I wanna change answer color):
    Main class(pagrindine.java):
    import java.awt.*;
     
    import javax.swing.*;
     
    import java.awt.event.*;
     
    import javax.swing.event.*;
    //Mokinio rinkinukas su skaiciofke kur gali keisti atsakymo spalva, pabandyti sukurti uzrasu knygute.
     
    public class pagrindine extends JFrame {
     
    	JLabel pasisveikinimas;
    	JButton calc, uzrasai;
    	JMenuBar menubar;
    	JMenu menu;
    	JMenuItem item;
     
     
     
    	public pagrindine() {
    		setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
     
    		pasisveikinimas = new JLabel("Sveiki. Ka naudosite?");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 3;
    		c.gridy = 3;
    		add(pasisveikinimas, c);
     
    		calc = new JButton("Skaiciuotuvas");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 3;
    		c.gridy = 4;
    		add(calc, c);
     
    		uzrasai = new JButton("Uzrasai");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 4;
    		c.gridy = 4;
    		add(uzrasai, c);
     
    		calcs e = new calcs ();
    		calc.addActionListener(e);
     
    		uzras ee = new uzras ();
    		uzrasai.addActionListener(ee);
     
     
    	}
     
    	public class calcs implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			pagrindines gui = new pagrindines(pagrindine.this);
    			gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    			gui.setSize(350,200);
    			gui.setLocation(300,300);
    			gui.setVisible(true);
     
    		}
    	}
     
    	public class uzras implements ActionListener {
     
    		public void actionPerformed(ActionEvent ee) {
     
    		}
    	}
     
     
    	public static void main(String[]args) {
    		pagrindine gui = new pagrindine();
    		gui.setSize(350, 250);
    		gui.setVisible(true);
    		gui.setTitle("Mokinio Rinkinukas");
    		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    }

    Next Class(pagrindiness.java):
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
    import javax.swing.event.*;
     
     
    public class pagrindines extends JDialog  {
    	JButton atimtis, pridetis, dalyba, daugyba;
    	JLabel atsakymas, nu1, nu2;
    	JTextField n1, n2;
    	JMenuBar menubar;
    	JMenu menu;
    	JMenuItem item;
    	double nn1, nn2, ats;
    	String op;
     
    	public pagrindines(JFrame frame) {
    		super(frame, "Skaiciuotuvas", true);
     
    		setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
     
    		menubar = new JMenuBar();
    		setJMenuBar(menubar);
     
    		menu = new JMenu("Nustatymai");
    		menubar.add(menu);
     
    		item = new JMenuItem("Keisti atsakymo spalva");
    		menu.add(item);
     
    		nu1 = new JLabel("1st: ");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridy = 0;
    		add(nu1, c);
     
    		n1 = new JTextField(10);
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 1;
    		c.gridy = 0;
    		c.gridwidth = 3;
    		add(n1, c);
     
    		nu2= new JLabel("2nd: ");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		add(nu2, c);
     
     
    		n2 = new JTextField(10);
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 1;
    		c.gridy = 1;
    		c.gridwidth = 3;
    		add(n2, c);
     
     
    		pridetis = new JButton("+");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridy = 2;
    		c.gridwidth = 1;
    		add(pridetis, c);
     
    		atimtis = new JButton("-");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 1;
    		c.gridy = 2;
    		add(atimtis, c);
     
    		daugyba = new JButton("*");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 2;
    		c.gridy = 2;
    		add(daugyba, c);
     
    		dalyba = new JButton("/");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 3;
    		c.gridy = 2;
    		add(dalyba, c);
     
    		atsakymas = new JLabel("");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridy = 4;
    		c.gridwidth = 4;
    		add(atsakymas, c);
     
     
     
    		event e = new event();
    		atimtis.addActionListener(e);
    		pridetis.addActionListener(e);
    		daugyba.addActionListener(e);
    		dalyba.addActionListener(e);
     
    		keist k = new keist();
    		item.addActionListener(k);
     
    	}
     
     
    	public class keist implements ActionListener {
     
    		public void actionPerformed(ActionEvent k) {
     
    			slideris gui = new slideris(pagrindines.this);
    			gui.setSize(400,200);
    			gui.setVisible(true);
    			gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
     
     
    		}
     
    	}
     
     
     
     
     
    	public class event implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
     
    			try {
    				nn1 = Double.parseDouble(n1.getText());
     
    			} catch(NumberFormatException e1) {
     
    				atsakymas.setText("Klaida!Turi buti skaicius pirmame langelyje!");
    				return;
     
    			} try {
    				nn2 = Double.parseDouble(n2.getText());
    			}catch (NumberFormatException e2) {
    				atsakymas.setText("Klaida!Turi buti skaicius antrame langelyje!");
    				return;
    			}
     
    			op = e.getActionCommand();
    			if (op.equals("+")) {
    				ats = nn1 + nn2;
    				atsakymas.setText("Atsakymas yra "+ats);
    				atsakymas.setForeground(new Color(r,g,b));
    			} else {
    			if (op.equals("-")) {
    				ats = nn1 - nn2;
    				atsakymas.setText("Atsakymas yra "+ats);
    				atsakymas.setForeground(new Color(r,g,b));
    			} else {
    				if (op.equals("*")) {
    				ats = nn1 * nn2;
    				atsakymas.setText("Atsakymas yra "+ats);
    				atsakymas.setForeground(new Color(r,g,b));
    				} else {
    				if (op.equals("/")) {
    				if (nn1 == 0 || nn2 == 0) {
    					atsakymas.setText("Dalyboje turi negali buti nuliu!!!");
    					atsakymas.setForeground(new Color(r,g,b));
    				} else {
    					ats = nn1 / nn2;
    					atsakymas.setText("Atsakymas yra "+ats);
    					atsakymas.setForeground(new Color(r,g,b));
    				}
    					}
    				}
    			}
    			}
    		}
     
    	}
    }

    Next Class(slideris.java):
    import java.awt.*;
     
    import javax.swing.*;
     
    import java.awt.event.*;
     
    import javax.swing.event.*;
     
    public class slideris extends JDialog {
    	JSlider redSlider,greenSlider,blueSlider;
    	JLabel redLabel,greenLabel,blueLabel;
    	JPanel colorPanel, sliders, labels;
    	int r,g,b;
     
    	public slideris(pagrindines pagrindines) {
    		super(pagrindines, "Spalvu keitiklis", true);
    		setLayout(new FlowLayout());
     
    		redSlider = new JSlider(JSlider.HORIZONTAL,0,255,0);
    		greenSlider = new JSlider(JSlider.HORIZONTAL,0,255,0);
    		blueSlider = new JSlider(JSlider.HORIZONTAL,0,255,0);
     
    		redLabel = new JLabel("Red = 0");
    		greenLabel = new JLabel("Green = 0");
    		blueLabel = new JLabel("Blue = 0");
     
    		colorPanel = new JPanel();
    		colorPanel.setBackground(Color.BLACK);
     
    		event e = new event();
    		redSlider.addChangeListener(e);
    		blueSlider.addChangeListener(e);
    		greenSlider.addChangeListener(e);
     
    		Container pane = this.getContentPane();
    		pane.setLayout(new GridLayout(1,3,3,3));
     
    		sliders = new JPanel();
    		labels = new JPanel();
     
    		pane.add(sliders);
    		pane.add(labels);
    		pane.add(colorPanel);
     
    		sliders.setLayout(new GridLayout(3,1,2,2));
    		sliders.add(redSlider);
    		sliders.add(blueSlider);
    		sliders.add(greenSlider);
     
    		labels.setLayout(new GridLayout(3,1,2,2));
    		labels.add(blueLabel);
    		labels.add(greenLabel);
    		labels.add(redLabel);
     
    }
     
     
    	public class event implements ChangeListener {
    		public void stateChanged(ChangeEvent e) {
    			r = redSlider.getValue();
    			g = greenSlider.getValue();
    			b = blueSlider.getValue();
     
     
    			colorPanel.setBackground(new Color(r,g,b));
     
    		}
     
    	}
     
     
     
     
    }

    So, guys, i want post variables r,g,b from class slideris.java in the pagrindiness.java. So you can help me?
    Sorry for bad english language ;D Im from Lithuanian, so....


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help guys. Integer variables post

    Welcome to the forum! Thanks for taking the time to learn to post code correctly.

    What you describe is typically done by passing the variables to the desired instance of the class, either through the constructor or by using a "setter" or mutator method. For the setter method approach, something like:

    in slideris.java, the following statement could be used to pass the variables to the instance pagrindiness:

    pagrindiness.setVariables( r, g, b );

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help guys. Integer variables post

    I dont understeand. Can you write this thing in code ? Thanks...

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help guys. Integer variables post

    Hope this helps. I demonstrated both approaches. Let me know if you have questions.
    // a class with a main() method to start
    // a simple class that sends values to another class via the
    // other class' constructor and using a setter method in the other class
    public class SendClass
    {
        // instance variables
        int rValue;
        int gValue;
        int bValue;
        ReceiveClass receiveClass;
     
        // default constructor
        public SendClass()
        {
            rValue = 5;
            gValue = 15;
            bValue = 25;
     
            // create an instance of ReceiveClass and pass the values
            // through the constructor
            receiveClass = new ReceiveClass( rValue, gValue, bValue );
     
            // output the values
            receiveClass.printRGBValues();
     
            // or, alternatively send the values to the receiveClass
            // instance using the setter method
            receiveClass.setRGBValues( rValue + 5, gValue + 5, bValue + 5 );
     
            // output the values
            receiveClass.printRGBValues();
     
        } // end default constructor
     
        // a main() method
        public static void main( String[] args )
        {
            new SendClass();
     
        } // end method main()
     
    } // end class SendClass
     
    // a class to receive the values
    class ReceiveClass
    {
        // instance variables
        int rValue;
        int gValue;
        int bValue;
     
        // a constructor that accepts the 3 values
        public ReceiveClass( int rValue, int gValue, int bValue )
        {
            this.rValue = rValue;
            this.gValue = gValue;
            this.bValue = bValue;
     
        } // end constructor
     
     
        // a method to set the values
        public void setRGBValues( int rValue, int gValue, int bValue )
        {
            this.rValue = rValue;
            this.gValue = gValue;
            this.bValue = bValue;
     
        } // end method setRGBValues()
     
        // to output the values
        public void printRGBValues()
        {
            System.out.println( "The values are: " );
            System.out.println( "rValue: " + rValue );
            System.out.println( "gValue: " + gValue );
            System.out.println( "bValue: " + bValue );
     
        } // end method printRGBValues()
     
    } // end class ReceiveClass

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help guys. Integer variables post

    Very thank You. I analized this code. But maybe you can show this thing inputed in my code?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help guys. Integer variables post

    No, I won't modify your code. You try it and post the changes with any errors if you need help fixing any problems you're having.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help guys. Integer variables post

    Okey. Thanks for help

Similar Threads

  1. Integer pyramid 1-15
    By CruelCoin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2013, 06:49 AM
  2. [SOLVED] Integer and if statement
    By that_guy in forum Loops & Control Statements
    Replies: 3
    Last Post: November 28th, 2012, 12:05 PM
  3. Integer class
    By freakycoder in forum Collections and Generics
    Replies: 1
    Last Post: June 27th, 2012, 07:28 AM
  4. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  5. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM