在Eclipse中运行应用程序时,JFrame将无法打开

时间:2017-10-15 01:22:21

标签: java eclipse swing

我正在尝试创建一个简单的GUI,它接受两个值,并根据用户选择的按钮减去/添加/除。我对我的代码很满意,但由于某种原因,当我运行我的程序时,我没有得到任何可见的框架,我也没有得到任何错误。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calc {

private JFrame frame;
private JTextField firstNumber;
private JTextField secondNumber;
private JTextField textField_2;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Calc window = new Calc();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Calc() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setTitle("Simple Calculator");
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    firstNumber = new JTextField();
    firstNumber.setBounds(28, 41, 156, 26);
    frame.getContentPane().add(firstNumber);
    firstNumber.setColumns(10);

    secondNumber = new JTextField();
    secondNumber.setBounds(28, 145, 156, 26);
    frame.getContentPane().add(secondNumber);
    secondNumber.setColumns(10);

    JLabel lblNewLabel = new JLabel("First Number");
    lblNewLabel.setBounds(28, 27, 110, 16);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Second Number");
    lblNewLabel_1.setBounds(28, 131, 110, 16);
    frame.getContentPane().add(lblNewLabel_1);

    JLabel answerLabel = new JLabel("ANSWER:");
    answerLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
    answerLabel.setBounds(216, 46, 190, 16);
    frame.getContentPane().add(answerLabel);

    JButton plusButton = new JButton("Add");
    plusButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                int number1 = Integer.parseInt(firstNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad first number.");
                return;
            }
            try{
                int number2 = Integer.parseInt(secondNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad second number.");
                return;
            }
            int number1 = 0, number2 = 0;
            int answer = number1 + number2;
            answerLabel.setText("The answer is: "+answer);
        }
    });
    plusButton.setToolTipText("Adds first from second number");
    plusButton.setBounds(28, 183, 117, 29);
    frame.getContentPane().add(plusButton);

    JButton subtractButton = new JButton("Subtract");
    subtractButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                int number1 = Integer.parseInt(firstNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad first number.");
                return;
            }
            try{
                int number2 = Integer.parseInt(secondNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad second number.");
                return;
            }
            int number1 = 0, number2 = 0;
            int answer = number1 - number2;
            answerLabel.setText("The answer is: "+answer);
        }
    });
    subtractButton.setToolTipText("Subtracts first from second number");
    subtractButton.setBounds(157, 183, 117, 29);
    frame.getContentPane().add(subtractButton);

    JButton divideButton = new JButton("Divide");
    divideButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                int number1 = Integer.parseInt(firstNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad first number.");
                return;
            }
            try{
                int number2 = Integer.parseInt(secondNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad second number.");
                return;
            }
            int number1 = 0, number2 = 0;
            int answer = number1 / number2;
            answerLabel.setText("The answer is: "+answer);
        }
    });
    divideButton.setToolTipText("Divides first from second number");
    divideButton.setBounds(289, 183, 117, 29);
    frame.getContentPane().add(divideButton);

    textField_2 = new JTextField();
    textField_2.setBounds(216, 94, 130, 26);
    frame.getContentPane().add(textField_2);
    textField_2.setColumns(10);
}
}

1 个答案:

答案 0 :(得分:1)

啊!这是我之前遇到过的一个问题。这个提示解决了这个问题。这是如何做到的。

确保你已经建造了它。或者您可以启用项目 - >自动构建。

设置上述选项后,尝试清理工作区。项目 - >清理并清理您导入的所有项目。

现在重启eclipse,你可以启动你的程序。

ps:如果不能正常工作,请用控制台发送日食图片,这样我们就可以看到运行移民的日志和图片。