Making a calculator right now and for some reason, txtdisplay is not working. When I run the program and click number 7. I get error messages. how do i fix it?
Here is the code. I've recently been having problems with eclipse ever since I downloaded it on a new computer. I have looked for a solution to the problem and tried what eclipse suggsted and what the answers to a similar question asked and it's still not working right now.
package Calculators;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class Calculator {
private JFrame frame;
private JTextField textField;
/**
* 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.setBounds(100, 100, 266, 337);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(10, 11, 211, 32);
frame.getContentPane().add(textField);
textField.setColumns(10);
//row one
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = txtDisplay.getText() + btn7.getText();
txtDisplay.setText(EnterNumber);
}
});
btn7.setBackground(Color.CYAN);
btn7.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn7.setBounds(10, 54, 50, 50);
frame.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.setBackground(Color.YELLOW);
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btn8.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn8.setBounds(70, 54, 50, 50);
frame.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.setBackground(Color.ORANGE);
btn9.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn9.setBounds(130, 54, 50, 50);
frame.getContentPane().add(btn9);
JButton btnPlus = new JButton("+");
btnPlus.setBackground(Color.PINK);
btnPlus.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnPlus.setBounds(190, 54, 50, 50);
frame.getContentPane().add(btnPlus);
// row two
JButton btn4 = new JButton("4");
btn4.setBackground(Color.CYAN);
btn4.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn4.setBounds(10, 114, 50, 50);
frame.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.setBackground(Color.YELLOW);
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btn5.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn5.setBounds(70, 114, 50, 50);
frame.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.setBackground(Color.ORANGE);
btn6.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn6.setBounds(130, 114, 50, 50);
frame.getContentPane().add(btn6);
JButton btnMinus = new JButton("-");
btnMinus.setBackground(Color.PINK);
btnMinus.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnMinus.setBounds(190, 114, 50, 50);
frame.getContentPane().add(btnMinus);
// row three
JButton btn1 = new JButton("1");
btn1.setBackground(Color.CYAN);
btn1.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn1.setBounds(10, 174, 50, 50);
frame.getContentPane().add(btn1);
JButton btn2 = new JButton("2");
btn2.setBackground(Color.YELLOW);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btn2.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn2.setBounds(70, 174, 50, 50);
frame.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.setBackground(Color.ORANGE);
btn3.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn3.setBounds(130, 174, 50, 50);
frame.getContentPane().add(btn3);
JButton btnTimes = new JButton("x");
btnTimes.setBackground(Color.PINK);
btnTimes.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnTimes.setBounds(190, 174, 50, 50);
frame.getContentPane().add(btnTimes);
// row four
JButton btn0 = new JButton("0");
btn0.setBackground(Color.CYAN);
btn0.setFont(new Font("Tahoma", Font.PLAIN, 18));
btn0.setBounds(10, 234, 50, 50);
frame.getContentPane().add(btn0);
JButton btndot = new JButton(".");
btndot.setBackground(Color.YELLOW);
btndot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btndot.setFont(new Font("Tahoma", Font.PLAIN, 18));
btndot.setBounds(70, 234, 50, 50);
frame.getContentPane().add(btndot);
JButton btnPM = new JButton("±");
btnPM.setBackground(Color.ORANGE);
btnPM.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnPM.setBounds(130, 234, 50, 50);
frame.getContentPane().add(btnPM);
JButton btnEquals = new JButton("x");
btnEquals.setBackground(Color.PINK);
btnEquals.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnEquals.setBounds(190, 234, 50, 50);
frame.getContentPane().add(btnEquals);
}
}