java - JTextField 无法正常工作

标签 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 的初学者,我可以知道造成这种情况的可能原因以及如何纠正吗?

最佳答案

您的代码中有几个问题,很难提供准确的答案。以下是一些需要努力的事情:

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

    }
}

关于java - JTextField 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16775443/

相关文章:

java - 圆形 float 菜单生成器错误

Java - 如何获取 JTextPane 的默认字体大小

java - 使用 OO 观察者模式而不更新从中发起更改的对象

java - JTextField 只接受有效的未签名短裤? (使用 key 适配器)

java - 显示 'Loading'帧直到后台进程完成

java - 取消对所有 JTextField 的关注

Java:如何在未修饰的 JFrame 周围绘制边框?

java - 将列表的元素添加到另一个列表后清除列表

java - 中断()不起作用

java - 带有彩色项目和焦点的彩色 jcombobox