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.
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.
Re: JButtons making a Phone Calculator
Thank you for the quick reply, this is what I have so far..
Code java:
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.
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
Code :
if ( e.getSource() == numberButtons[0] ){
///you know button 0 was pressed.
}