So I am trying to make a calculator in Java and I have it so that when you clikc the 123456789 or . buttons it will add it to the text field. The problem is that once I add the 6 character it will show "null". Here is my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
public class Calculator {
private JFrame frame;
private JTextField textField;
//Variables
private double plus;
private double minus;
private double divide;
private double multiply;
//Check for button clicks
private int plusClick;
private int munisClick;
private int multiplyClick;
private int divideClick;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Calculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 242, 315);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton equalsButton = new JButton("=");
equalsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
equalsButton.setBounds(185, 202, 41, 74);
frame.getContentPane().add(equalsButton);
JButton additionButton = new JButton("+");
additionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
additionButton.setBounds(185, 163, 41, 38);
frame.getContentPane().add(additionButton);
JButton subtractionButton = new JButton("-");
subtractionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
subtractionButton.setBounds(185, 124, 41, 38);
frame.getContentPane().add(subtractionButton);
JButton divideButton = new JButton("/");
divideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
divideButton.setBounds(185, 86, 41, 38);
frame.getContentPane().add(divideButton);
JButton multiplyButton = new JButton("x");
multiplyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
multiplyButton.setBounds(185, 49, 41, 37);
frame.getContentPane().add(multiplyButton);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(0, 0, 226, 49);
frame.getContentPane().add(textField);
textField.setColumns(10);
final JButton zeroButton = new JButton("0");
zeroButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+zeroButton.getText());
}
});
zeroButton.setBounds(10, 231, 137, 34);
frame.getContentPane().add(zeroButton);
final JButton one = new JButton("1");
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + one.getText());
}
});
one.setBounds(10, 193, 41, 34);
frame.getContentPane().add(one);
final JButton two = new JButton("2");
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + two.getText());
}
});
two.setBounds(56, 193, 41, 34);
frame.getContentPane().add(two);
final JButton three = new JButton("3");
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + three.getText());
}
});
three.setBounds(106, 193, 41, 34);
frame.getContentPane().add(three);
final JButton four = new JButton("4");
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + four.getText());
}
});
four.setBounds(10, 153, 41, 34);
frame.getContentPane().add(four);
final JButton five = new JButton("5");
five.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + five.getText());
}
});
five.setBounds(56, 153, 41, 34);
frame.getContentPane().add(five);
final JButton six = new JButton("6");
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getSelectedText() + six.getText());
}
});
six.setBounds(106, 153, 41, 34);
frame.getContentPane().add(six);
final JButton seven = new JButton("7");
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + seven.getText());
}
});
seven.setBounds(10, 116, 41, 34);
frame.getContentPane().add(seven);
final JButton eight = new JButton("8");
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + eight.getText());
}
});
eight.setBounds(56, 116, 41, 34);
frame.getContentPane().add(eight);
final JButton nine = new JButton("9");
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + nine.getText());
}
});
nine.setBounds(106, 116, 41, 34);
frame.getContentPane().add(nine);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
clearButton.setBounds(10, 67, 87, 38);
frame.getContentPane().add(clearButton);
final JButton decimalButton = new JButton(".");
decimalButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
textField.setText(textField.getText() + decimalButton.getText());
}
});
decimalButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
decimalButton.setBounds(106, 67, 41, 38);
frame.getContentPane().add(decimalButton);
}
}
How to I get it to display all the numbers without giving me "null".
Thanks!
final JButton six = new JButton("6");
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getSelectedText() + six.getText());
}
});
should be
final JButton six = new JButton("6");
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + six.getText());
}
});
As no text is selected when the button is pushed, it returns null
, which is then converted to a string and then has "6" appended to it.
Simple. If you just compare your 6 button to the rest of your buttons, you'll notice you're doing something different in the ActionListener
. Change the following:
textField.setText(textField.getSelectedText() + six.getText());
to
textField.setText(textField.getText() + six.getText());
Also, I highly recommend that you go through A Visual Guide to Layout Managers before you run into problems using a null layout.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.