JTextField无法正常工作

时间:2013-05-27 14:35:55

标签: java swing jtextfield

我正在尝试使用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的初学者,我可能知道可能的原因以及如何纠正它吗?

2 个答案:

答案 0 :(得分:3)

您的代码中存在多个问题,很难提供准确的答案。以下是一些可以解决的问题:

  • JFrame.setVisible(true)应该是您的最后一次通话(在拨打之前设置用户界面,或确保之后再拨打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);

    }
}

答案 1 :(得分:1)

这很可能是因为您在主线程上创建了GUI,而不是Event Dispatch Thread

只能在Event Dispatch Thread上访问Swing组件。这可以通过调用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组件。只应在名为Event Dispatch Thread的特定线程上访问Swing组件。

您可以阅读有关线程here的更多信息。