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 4 of 4

Thread: Action Methods..

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

    Default Action Methods..

    Hello all. So i creating Calculator and i have a problem. First see my code :
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
     
    public class Salutine extends JFrame {
     
    	//Numbers
     
    	private JButton nulis;
    	private JButton vienas;
    	private JButton du;
    	private JButton trys;
    	private JButton keturi;
    	private JButton penki;
    	private JButton sesi;
    	private JButton septyni;
    	private JButton astuoni;
    	private JButton devyni;
    	private JTextField atsakymas;
     
     
     
    	//Operators
     
    	private JButton dalyba,daugyba,pridetis,atimtis;
     
    	//Constructor
     
    	public Salutine() {
    		super("Skaiciuotuvas By xkendzu (Beta)");
    		setLayout(new FlowLayout());
     
     
    		nulis = new JButton("0");
    		vienas = new JButton("1");
    		du = new JButton("2");
    		trys = new JButton("3");
    		keturi = new JButton("4");
    		penki = new JButton("5");
    		sesi = new JButton("6");
    		septyni = new JButton("7");
    		astuoni = new JButton("8");
    		devyni = new JButton("9");
     
     
     
    		dalyba = new JButton("/");
    		daugyba = new JButton("*");
    		pridetis = new JButton("+");
    		atimtis = new JButton("-");
     
     
     
    		atsakymas = new JTextField("",20);
     
     
     
    		add(nulis);
    		add(vienas);
    		add(du);
    		add(trys);
    		add(keturi);
    		add(penki);
    		add(sesi);
    		add(septyni);
    		add(astuoni);
    		add(devyni);
    		add(nulis);
    		add(atsakymas);
     
     
    		add(dalyba);
    		add(daugyba);
    		add(pridetis);
    		add(atimtis);
     
    		//Creating, when button pressing
     
     
    		Skaiciai skaiciai = new Skaiciai();
    		nulis.addActionListener(skaiciai);
    		vienas.addActionListener(skaiciai);
    		du.addActionListener(skaiciai);
    		trys.addActionListener(skaiciai);
    		keturi.addActionListener(skaiciai);
    		penki.addActionListener(skaiciai);
    		sesi.addActionListener(skaiciai);
    		septyni.addActionListener(skaiciai);
    		astuoni.addActionListener(skaiciai);
    		devyni.addActionListener(skaiciai);
     
    		Veiksmai veiksmai = new Veiksmai();
    		dalyba.addActionListener(veiksmai);
    		daugyba.addActionListener(veiksmai);
    		pridetis.addActionListener(veiksmai);
    		atimtis.addActionListener(veiksmai);
     
    	}
     
    	//Number class with method
     
    	public class Skaiciai implements ActionListener {
     
    		public void actionPerformed(ActionEvent e) {
    			String nr1 = e.getActionCommand();
    			String nr2 = e.getActionCommand();
     
    			int nr11 = Integer.parseInt(nr1);
    			int nr22 = Integer.parseInt(nr2);
     
     
     
     
    		}
     
     
     
    	}
     
    	//Operator class with method
     
    	public class Veiksmai implements ActionListener {
     
    		public void actionPerformed(ActionEvent e1) {
     
    			String op = e1.getActionCommand();
     
     
    		}
     
     
     
     
    	}
     
     
     
     
     
    }

    So when you pressed button of number, JTextField change text like 1 and when you pressed operator changed text to 1 +-/* and finally when you pressed last number change text to 1 +-/* 2 = 3.. So i need know how post variables with value in the next actionPerformed method. Maybe my code bad? Maybe I must using other method? Can you help me? Thanks..


  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: Action Methods..

    You can do what you describe in the existing actionPerformed() method. For example, you could set the TextField to the button pressed with the statement:

    atsakymas.setText( nr1 );

    You can then build on that using a StringBuilder object perhaps to which the operator and subsequent buttons could be added.

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

    Default Re: Action Methods..

    Can you tell me more about String builder?

  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: Action Methods..

    Anytime you need to know more about a Java core or SE class, simply Google, 'java class doc' where 'class' = the name of the class you're interested in. For example, for StringBuilder, Google 'java stringbuilder doc'. Using that formula, the top result will normally be the desired page.

Similar Threads

  1. [SOLVED] Anonymous inner class with Action Listener and Abstract Action
    By shakes6791 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 13th, 2013, 03:52 PM
  2. Accessing non-static methods from action listener in another class?
    By mfj in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 10th, 2013, 06:23 PM
  3. No action time out
    By skuskusas in forum Java Theory & Questions
    Replies: 4
    Last Post: November 9th, 2012, 07:29 AM
  4. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  5. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM