Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

txtDisplay cannot be resolved in java eclipse

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

txtDisplay has not been initialized anywhere in the given code.

You might be referring to your textField object though?

        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());

            }
        });

I commented out the code where you use your txtDisplay object.

Keep in mind that every time you press the "7" Button, 7 will be written in the textField.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...