【问题标题】:GUI not displaying content till resized or minimisedGUI 在调整大小或最小化之前不显示内容
【发布时间】:2013-07-19 16:21:56
【问题描述】:

好的......我的问题相当直截了当,所以我怀疑不需要添加任何代码,但如果需要的话我会的。

每当我创建一个 GUI 框架并向其中添加几个面板并运行我的应用程序时,内容不会显示,直到我重新调整窗口大小或在工具栏上将其最小化然后恢复它。可能是什么原因造成的,我该如何解决?

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public final class Calculator extends JFrame  
{
    //initialise various variables for use within the program
    //BUTTONS
    private final JButton additionButton = new JButton("+"); 
    private final JButton subtractionButton = new JButton("-");
    private final JButton divisionButton = new JButton("/");
    private final JButton multiplicationButton = new JButton("*");    

    //PANELS
    private JPanel operatorPanel;
    private JPanel operandPanel;

    //LABELS
    private JLabel operationLabel;    

    //constructor to initialise the frame and add components into it
    public Calculator()
    {
        super("Clancy's Calculator");
        setLayout(new BorderLayout(5, 10));
        setSize(370, 200);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        //create a message label to display the operation that has just taken place
        operationLabel = new JLabel("YOU HAVE PERFORMED SOME OPERATION",SwingConstants.CENTER);

        add(getOperatorPanel(), BorderLayout.NORTH);
        add(getOperandPanel(), BorderLayout.CENTER);
        add(operationLabel, BorderLayout.SOUTH);
    }

    //setter method for the operator panel
    public void setOperatorPanel()
    {
        operatorPanel = new JPanel();
        operatorPanel.setLayout(new FlowLayout());

        operatorPanel.add(additionButton);
        operatorPanel.add(subtractionButton);
        operatorPanel.add(multiplicationButton);
        operatorPanel.add(divisionButton);
    }
    //getter method for the operator panel
    public JPanel getOperatorPanel()
    {
        setOperatorPanel();
        return operatorPanel;
    }

    //setter method for operands panel
    public void setOperandPanel()
    {
        operandPanel = new JPanel();
        operandPanel.setLayout(new GridLayout(3, 2, 5, 5));

        //LABELS
        JLabel operandOneLabel = new JLabel("Enter the first Operand: ");
        JLabel operandTwoLabel = new JLabel("Enter the second Operand: ");
        JLabel answerLabel = new JLabel("ANSWER: ");

        //TEXT FIELDS
        JTextField operandOneText = new JTextField();   //retrieves one operand
        JTextField operandTwoText = new JTextField();   //retrieves another operand
        JTextField answerText = new JTextField();   //displays answer

        answerText.setEditable(false);  //ensure the answer field is not editable

        operandPanel.add(operandOneLabel);
        operandPanel.add(operandOneText);
        operandPanel.add(operandTwoLabel);
        operandPanel.add(operandTwoText);
        operandPanel.add(answerLabel);
        operandPanel.add(answerText);

    }
    //getter method for operand panel
    public JPanel getOperandPanel()
    {
        setOperandPanel();
        return operandPanel;
    }

    /** main method */
    public static void main(String[] args)
    {
        new Calculator();
    }
}

【问题讨论】:

  • 原因是代码中的错误。我的水晶球说它在第 453 行,但不幸的是它没有说明是哪个文件。
  • @JBNizet 你真的需要得到水晶球 2.0,它可以告诉你你想知道的一切。
  • 你的水晶球在使用类固醇 JB 因为我只在第 100 行,但我想这意味着我应该添加一些代码并且很快就会这样做。
  • 在显示之前是否在框架上调用pack()?否则,请发布一些代码(@JBNizet +1 搞笑评论)
  • @c.s.有你可能的问题和它的解决方案 - 将所有组件添加到 GUI 之后调用 pack()setVisible(true)(如果这是 Swing)。下次,请附上您的问题的代码以及您正在使用的 GUI 库的名称。问题提供的信息越多,答案通常就越好。我们当然不想看到所有的代码,因为这可能会淹没我们在不相关的代码中,并且弄清楚要展示多少既是一门艺术,也是一门科学。通常最好的展示是sscce。请查看链接。

标签: java swing calculator


【解决方案1】:

我注意到您正在以编程方式设置新的布局管理器。每当您添加、删除或更改 java gui 时,您需要调用 invalidate()revalidate() 以使 java 重新创建 GUI。看看在setVisible(true) 之前调用invalidate() 是否可以解决问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2019-09-12
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多