在JavaEclipse中无法解析txtdisplay

jtoj6r0c  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(318)

由于某些原因,现在正在制作计算器,txtdisplay不起作用。当我运行程序并点击7号。我收到错误信息。我该怎么修?
这是密码。自从我把eclipse下载到一台新电脑上后,我最近就一直遇到问题。我已经找到了一个解决问题的方法,并尝试了eclipse的建议和类似问题的答案,但现在仍然不起作用。

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);
  }
}
n7taea2i

n7taea2i1#

txtdisplay尚未在给定代码中的任何位置初始化。
但您可能引用了textfield对象?

JButton btn7 = new JButton("7");
        btn7.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
//                String EnterNumber = txtDisplay.getText() + btn7.getText();
//                txtDisplay.setText(EnterNumber);
                textField.setText(textField.getText() + btn7.getText());

            }
        });

我注解掉了使用txtdisplay对象的代码。
请记住,每次按“7”按钮时,文本字段中将写入7。

相关问题