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: JButtons making a Phone Calculator

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JButtons making a Phone Calculator

    Right now I am working on the logic/theory of how to get this program to work. I can upload the code if it helps. I have to make a phone calculator using JButtons, when the user clicks on a button 0-9 it will send that key to a TextField, continually the user will keep pressing buttons to fill up the textfield to 9 numbers to create the phone number. I have created a button array, and am currently trying to get the buttons to show up in the textField using actionlistener but do not know how to implement the array in the actionlistener because I have the error I cannot invoke actionlistener on an array of JButtons. I was wondering if doing the mousehandler route would be the correct way of doing the button click to show up in the textfield or not.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JButtons making a Phone Calculator

    You mention errors, but post no code or description of said errors - without which leaves us only guessing at what could be the problem. Please post a short piece of code that demonstrates the problem if you wish to receive specific help on your code. I also recommend reading the Getting Help link in my signature.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JButtons making a Phone Calculator

    Thank you for the quick reply, this is what I have so far..

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
     
    public class PhoneGUI implements ActionListener {
     
    	private JFrame frame = new JFrame("Calculator");
    	private JPanel[] panels = new JPanel[6];
    	private JTextField textField = new JTextField();
    	private JButton dialButton = new JButton("Dial");
    	private JButton hangUpButton = new JButton("Hang Up");
    	private JButton resetButton = new JButton(" . ");
    	private JButton[] numberButtons = new JButton[10];
    	private JButton dotButton = new JButton(" . ");
     
    	public void buildGUI(){
     
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JPanel contentPane = (JPanel) frame.getContentPane();
     
    		//initialize panels
    		for(int i = 0; i < panels.length; i++){
    			panels[i] = new JPanel();			
     
    		}
     
    		//initialize button 0-9
    		for(int i=0;i<numberButtons.length; i++){
    			numberButtons[i] = new JButton(" " + i + " ");
    			numberButtons[i].setActionCommand(String.valueOf(i));
    			numberButtons[i].addActionListener(this);
     
    		}
     
    		//default layout = BorderLayout.CENTER
    		textField.setColumns(20);
    		textField.setText(" 0 ");
    		textField.setHorizontalAlignment (JTextField.RIGHT);
    		panels[0].add(textField);
     
    		//layout = FlowLayout.RIGHT
    		 panels[1].setLayout (new FlowLayout (FlowLayout.RIGHT));
    		 panels[1].add (dialButton);
    		 panels[1].add (hangUpButton);
     
    		 panels[2].setLayout (new FlowLayout (FlowLayout.LEFT));
    		 panels[2].add (numberButtons[7]);
    		 panels[2].add (numberButtons[8]);
    		 panels[2].add (numberButtons[9]);
     
    		 panels[3].setLayout (new FlowLayout (FlowLayout.LEFT));
    		 panels[3].add (numberButtons[4]);
    		 panels[3].add (numberButtons[5]);
    		 panels[3].add (numberButtons[6]);
     
    		 panels[4].setLayout (new FlowLayout (FlowLayout.LEFT));
    		 panels[4].add (numberButtons[1]);
    		 panels[4].add (numberButtons[2]);
    		 panels[4].add (numberButtons[3]); 
     
    		 panels[5].setLayout (new FlowLayout (FlowLayout.LEFT));
    		 panels[5].add (numberButtons[0]);
    		 panels[5].add (dotButton);
     
    		 contentPane.setLayout ( new BoxLayout (contentPane, BoxLayout.Y_AXIS));
    		 for(JPanel jPanel : panels){
    			 contentPane.add(jPanel);
     
    		 }
     
    		 resetButton.addActionListener(new resetNumberListener());
     
     
    		 frame.pack();
    		 frame.setVisible(true);
     
     
    	}
     
    	public void actionPerformed (ActionEvent e){
    		int a = String.valueOf(numberButtons[i]);
    		if(e.getSource() == String.valueOf(i)){
    				textField.setText(i);
     
    		}
    	}
     
     
     
    }

    From the looks of the code, I am guessing I will need another class to put a variable on the actually numbers to be implemented? Because once the user enters the number it will have to stay there while they are entering more numbers instead of new numbers just showing up.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: JButtons making a Phone Calculator

    If I understand the requirements correctly, implement an actionListener as you have (more examples and explanations here: How to Write an Action Listener (The Java Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) ). You can check which button has been pressed by checking the source of the ActionEvent...for example
    if ( e.getSource() == numberButtons[0] ){
       ///you know button 0 was pressed.
    }

Similar Threads

  1. JButtons not showing
    By Shaybay92 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 27th, 2011, 07:42 AM
  2. GUI: JButtons aren't visible until clicked
    By Staticity in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 02:49 PM
  3. Adding JButtons to Frame
    By iKlaush in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 12th, 2011, 10:52 AM
  4. Directing JButtons to functions?
    By scopolamine in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:22 AM
  5. [SOLVED] How to start writing java mobile application?
    By Kok in forum Java ME (Mobile Edition)
    Replies: 15
    Last Post: July 30th, 2009, 01:52 AM