import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame
{
    //Setting out the variables
    JTextField answerResponse;
    JButton one,two,three,four,five,six,seven,eight,nine,zero,add,subtract,multiply,divide,equals;
    String var1, var2, tanswer;
    Double answer;
    JPanel contentpanel;
    public GUI()
    {
        //JFrame Window
        JFrame frame = new JFrame();
        frame.setSize(250, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setTitle("Calculator");
        frame.setLocationRelativeTo(null);

        //Buttons
        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"); add = new JButton("+"); divide = new JButton("/");
        multiply = new JButton("*"); subtract = new JButton("-"); equals = new JButton("=");

        //Dimensions
        Dimension dim = new Dimension(75, 25);
        one.setPreferredSize(dim); two.setPreferredSize(dim); three.setPreferredSize(dim);
        four.setPreferredSize(dim); five.setPreferredSize(dim); six.setPreferredSize(dim);
        seven.setPreferredSize(dim); eight.setPreferredSize(dim); nine.setPreferredSize(dim);
        zero.setPreferredSize(new Dimension(225, 25)); add.setPreferredSize(new Dimension(113, 25)); subtract.setPreferredSize(new Dimension(113, 25));
        multiply.setPreferredSize(new Dimension(113, 25)); divide.setPreferredSize(new Dimension(113, 25)); equals.setPreferredSize(new Dimension(225, 25));

        //Content Panel
        JPanel contentPanel = new JPanel();
        frame.setContentPane(contentPanel);
        contentPanel.setLayout(new FlowLayout());

        //Adding to content panel
        contentPanel.add(one); contentPanel.add(two); contentPanel.add(three);
        contentPanel.add(four); contentPanel.add(five); contentPanel.add(six);
        contentPanel.add(seven); contentPanel.add(eight); contentPanel.add(nine);
        contentPanel.add(zero); contentPanel.add(add); contentPanel.add(subtract);
        contentPanel.add(multiply); contentPanel.add(divide); contentPanel.add(equals);

        //Declaring Variable Values
        answerResponse = new JTextField(null, 20);
        answerResponse.setEditable(false);
        contentPanel.add(answerResponse);
    }
}


我在尝试制作计算器时遇到问题。未显示名为“ answerResponse”的文本字段。其余的按钮等等,文本字段应该是顶部的第一件事。

最佳答案

frame.setVisible(true);放在最后一个contentPanel.add(answerResponse);下。

尽管没有证据表明存在其他建议,但是请确保您正在事件调度线程的上下文中执行UI代码,有关更多详细信息,请参见Initial Threads

还可以看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

您还应该习惯于不使用Windows的setSize,而应使用pack,因为这会考虑到平台和Windows边框之间出现的各种外观差异。

关于java - 文字栏位未显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25614814/

10-09 08:30