0

我正在尝试使用 Java 制作一个简单的计算器。我用于创建 GUI 的代码如下。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {

    private JButton one, two, three, four, five, six, seven, eight, nine, zero, plus, minus,
            multiply, divide, equalTo, point;
    private JPanel panelForResult, panelForKeys;
    private JLabel Result;
    private JTextField result;

    public Calculator() {
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
    }

    public static void main(String... args) {


        Calculator calcFrame = new Calculator();
        calcFrame.setSize(330, 400);
        calcFrame.setVisible(true);
        calcFrame.setResizable(false);
        calcFrame.createCalcGUI();

    }

    private void createCalcGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);



        Container window = getContentPane();
        window.setBackground(Color.blue);
        window.setSize(400, 400);

        FlowLayout windowLayout = new FlowLayout();
        windowLayout.setHgap(50);
        window.setLayout(windowLayout);


        panelForKeys = new JPanel();
        panelForKeys.setBackground(Color.CYAN);
        panelForKeys.setPreferredSize(new Dimension(200, 250));

        FlowLayout buttonLayout = new FlowLayout();
       // buttonLayout.setAlignOnBaseline(true);
        panelForKeys.setLayout(buttonLayout);

        panelForResult = new JPanel();
        panelForResult.setBackground(Color.CYAN);
        panelForResult.setPreferredSize(new Dimension(200, 50));
        panelForResult.setLayout(new FlowLayout());


        Result = new JLabel("=");
        result = new JTextField();

        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");
        zero = new JButton("0");
        plus = new JButton("+");
        minus = new JButton("-");
        multiply = new JButton("*");
        divide = new JButton("÷");
        equalTo = new JButton("=");
        point = new JButton(". ");




        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        nine.addActionListener(this);
        zero.addActionListener(this);
        plus.addActionListener(this);
        minus.addActionListener(this);
        divide.addActionListener(this);
        multiply.addActionListener(this);
        equalTo.addActionListener(this);
        point.addActionListener(this);

        panelForKeys.add(one);
        panelForKeys.add(two);
        panelForKeys.add(three);
        panelForKeys.add(four);
        panelForKeys.add(five);
        panelForKeys.add(six);
        panelForKeys.add(seven);
        panelForKeys.add(eight);
        panelForKeys.add(nine);
        panelForKeys.add(zero);
        panelForKeys.add(minus);
        panelForKeys.add(plus);
        panelForKeys.add(multiply);
        panelForKeys.add(divide);
        panelForKeys.add(equalTo);
        panelForKeys.add(point);


         window.add(panelForResult);
         window.add(this.panelForKeys);

      panelForResult.add(Result);
       panelForResult.add(result);


    }
}

每当我创建实例JTextField并将其添加panelForResult到整个容器窗口中时,都会变成蓝色。如果我注释掉JTextField它就可以了。我只是Java的初学者,我可以知道造成这种情况的可能原因以及如何纠正它吗?

4

2 回答 2

3

您的代码中有几个问题,很难提供准确的答案。这里有几件事情要做:

  • JFrame.setVisible(true)应该是您的最后一次通话(在调用之前设置 UI,或者确保在pack()之后调用)
  • 不要强迫preferredSize(永远不要打电话setPreferredSize)。这可能是您的问题的原因。始终使用适当的LayoutManager.
  • 不要依赖FlowLayout执行组件包装。
  • JTextField通过设置列数提供大小提示
  • 在 EDT 上调用所有与 Swing 相关的代码(使用SwingUtilities.invokeLater()

这是您的代码的一个工作示例(虽然不确定它是否符合您的想法):

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Calculator extends JFrame implements ActionListener {

    private JButton one, two, three, four, five, six, seven, eight, nine, zero, plus, minus, multiply, divide, equalTo, point;
    private JPanel panelForResult, panelForKeys;
    private JLabel Result;
    private JTextField result;

    public Calculator() {
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Calculator calcFrame = new Calculator();
                calcFrame.createCalcGUI();
                calcFrame.setResizable(false);
                calcFrame.pack();
                calcFrame.setVisible(true);
            }
        });

    }

    private void createCalcGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container window = getContentPane();
        window.setBackground(Color.blue);

        BoxLayout windowLayout = new BoxLayout(window, BoxLayout.PAGE_AXIS);
        window.setLayout(windowLayout);

        panelForKeys = new JPanel(new GridLayout(0, 3, 5, 5));
        panelForKeys.setBackground(Color.CYAN);

        panelForResult = new JPanel();
        panelForResult.setBackground(Color.CYAN);
        panelForResult.setLayout(new FlowLayout());

        Result = new JLabel("=");
        result = new JTextField(12);

        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");
        zero = new JButton("0");
        plus = new JButton("+");
        minus = new JButton("-");
        multiply = new JButton("*");
        divide = new JButton("÷");
        equalTo = new JButton("=");
        point = new JButton(". ");

        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        nine.addActionListener(this);
        zero.addActionListener(this);
        plus.addActionListener(this);
        minus.addActionListener(this);
        divide.addActionListener(this);
        multiply.addActionListener(this);
        equalTo.addActionListener(this);
        point.addActionListener(this);

        panelForKeys.add(one);
        panelForKeys.add(two);
        panelForKeys.add(three);
        panelForKeys.add(four);
        panelForKeys.add(five);
        panelForKeys.add(six);
        panelForKeys.add(seven);
        panelForKeys.add(eight);
        panelForKeys.add(nine);
        panelForKeys.add(zero);
        panelForKeys.add(minus);
        panelForKeys.add(plus);
        panelForKeys.add(multiply);
        panelForKeys.add(divide);
        panelForKeys.add(equalTo);
        panelForKeys.add(point);

        window.add(panelForResult);
        window.add(this.panelForKeys);

        panelForResult.add(Result);
        panelForResult.add(result);

    }
}
于 2013-05-27T15:02:47.447 回答
1

这很可能发生,因为您在主线程而不是Event Dispatch Thread上制作 GUI 。

Swing 组件只能在 Event Dispatch Thread 上访问。这可以通过调用来实现SwingUtilities.invokeLater

在您的情况下,将您的主要方法更改为:

public static void main(String... args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Calculator calcFrame = new Calculator();
            calcFrame.setSize(330, 400);
            calcFrame.setVisible(true);
            calcFrame.setResizable(false);
            calcFrame.createCalcGUI();
        }
    });
}

编辑:在 Java 中,有一种叫做线程的东西。通过使用多个线程,您可以同时运行多段代码,从而提高效率。

但是,并非所有代码都可以同时在不同的线程上安全地运行。例如,Swing 组件。Swing 组件只能在称为事件调度线程的特定线程上访问。

您可以在此处阅读有关线程的更多信息。

于 2013-05-27T14:55:21.430 回答