Reputation: 17
Just finished my first year of university and wanted to improve my coding skills in Java. I'm currently trying to develop a simple GUI calculator application. However, I'm stuck on how I would make on my of my jButtons such as the number one appear on my textfield once pressed. I understand I will probably have to use an action listener for this function but I'm currently stuck. Any help would be amazing. Thank you very much.
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack();
frame.setIconImage(new ImageIcon("F:\\Java Summer Project\\Sumer_Project\\images\\farme_image.jpg").getImage());
frame.setSize(500, 480);
frame.setResizable(false);
frame.setVisible(true);
////// panel\\\
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
TextField textField = new TextField();
textField.setBounds(0, 0, 500, 200);
textField.setEnabled(true);
panel.add(textField);
JButton openbracketButton = new JButton("(");
openbracketButton.setBounds(0, 200, 50, 50);
panel.add(openbracketButton);
JButton closebracketButton = new JButton(")");
closebracketButton.setBounds(50, 200, 50, 50);
panel.add(closebracketButton);
JButton percButton = new JButton("%");
percButton.setBounds(100, 200, 50, 50);
panel.add(percButton);
JButton clearButton = new JButton("ac");
clearButton.setBounds(150, 200, 50, 50);
panel.add(clearButton);
JButton divideButton = new JButton("÷");
divideButton.setBounds(150, 250, 50, 50);
panel.add(divideButton);
JButton timesButton = new JButton("X");
timesButton.setBounds(150, 300, 50, 50);
panel.add(timesButton);
JButton minusButton = new JButton("+");
minusButton.setBounds(150, 350, 50, 50);
panel.add(minusButton);
JButton plusButton = new JButton("-");
plusButton.setBounds(150, 400, 50, 50);
panel.add(plusButton);
JButton oneButton = new JButton("1");
oneButton.setBounds(0, 250, 50, 50);
oneButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent oneButton) {
System.out.println("Do Something Clicked");
}
});
panel.add(oneButton);
Upvotes: 0
Views: 114
Reputation: 736
You can add a pressed action to your number buttons and set the textFields text to the desired text.(Or append if necessary).
// Like this
textField.setText("1");
And I really recommend you the Eclipse Window Builder if you are going to use java Gui. It would save you a lot of time with some Gui codes that would never increase your Java knowledge. If you really want to increase your Java knowledge, you can check this book out:
Data Structures and Algorithms in Java 6e Michael T. Goodrich , Roberto Tamassia , Michael H. Goldwasser
Upvotes: 0
Reputation: 146
I guess you are looking for something like this:
oneButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent oneButton) {
if(StringUtils.isEmpty(textField)) {
// If its empty so just put 1 in the display
textField.setText("1");
} else {
// Get value on display and convert to a integer
// Plus one because its the button one
int result = 1 + Integer.parseInt(textField.getText());
// Set the result to the display
textField.setText((String) result);
}
}
});
Good luck on your studies!
Upvotes: 1