Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示为空白的GUI_Java_User Interface - Fatal编程技术网

Java 显示为空白的GUI

Java 显示为空白的GUI,java,user-interface,Java,User Interface,这是我的问题。我只是想为一个朋友的生日写一个漂亮的小申请。问题是,当我运行程序时,最初会得到一个空白的GUI。但是,如果我调整窗口的边界,哪怕是最轻微的一点,问题也会像预期的那样出现 这只发生在我输入JTextArea之后。只需显示文本即可。它不会用于输入文本。对我做错了什么有什么建议吗?我正在越过JFrame的边界 谢谢你的帮助 import javax.swing.*; import java.awt.*; public class Birthday { private JFrame

这是我的问题。我只是想为一个朋友的生日写一个漂亮的小申请。问题是,当我运行程序时,最初会得到一个空白的GUI。但是,如果我调整窗口的边界,哪怕是最轻微的一点,问题也会像预期的那样出现

这只发生在我输入
JTextArea
之后。只需显示文本即可。它不会用于输入文本。对我做错了什么有什么建议吗?我正在越过
JFrame
的边界

谢谢你的帮助

import javax.swing.*;
import java.awt.*;

public class Birthday {

  private JFrame mainFrame;
  private JPanel mainPanel;
  private JPanel labelPanel;
  private JPanel buttonPanel;
  private JButton story;
  private JButton misc;
  private JButton next;
  private JLabel mainLabel;

  private JFrame miscFrame;
  private JPanel miscPanel;
  private JLabel miscLable;

  private JTextArea text;

  public Birthday(){
    gui();
  }

  public static void main(String [] args){
    Birthday b = new Birthday();

  }

  public void gui(){
    mainFrame = new JFrame("The Buttercup Project"); //main window
    mainFrame.setVisible(true);
    mainFrame.setSize(550,650);
    //mainFrame.setResizable(false);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    text = new JTextArea(30,35);

    mainPanel = new JPanel(); //displays content in window
    mainPanel.setBackground(Color.YELLOW);
    mainPanel.setVisible(true);
    mainPanel.add(text);

    labelPanel = new JPanel();
    labelPanel.setVisible(true);
    labelPanel.setBackground(Color.LIGHT_GRAY);

    buttonPanel = new JPanel();
    buttonPanel.setVisible(true);
    buttonPanel.setBackground(Color.LIGHT_GRAY);


    story = new JButton("Story"); //button
    misc = new JButton("?");
    next = new JButton ("Next Story");
    mainLabel = new JLabel("The Buttercup Project"); //label


    labelPanel.add(mainLabel); //adds buttons to panel
    buttonPanel.add(story, BorderLayout.CENTER);
    buttonPanel.add(misc, BorderLayout.CENTER);
    buttonPanel.add(next, BorderLayout.CENTER);

    mainFrame.add(labelPanel,BorderLayout.NORTH );
    mainFrame.add(buttonPanel, BorderLayout.AFTER_LAST_LINE);

    mainFrame.add(mainPanel,BorderLayout.CENTER );  //put panel inside of frame

  }

}

调用
gui()
方法末尾的
mainFrame.setVisible(true)
。删除所有其他引用。

不需要在每个面板上调用setVisible()。对于包含它们的JFrame,只需执行一次;您还应该在添加所有组件后执行此操作。另外,一定要调用mainFrame.pack(),以便计算出正确的大小。

“为朋友的生日编写一个漂亮的小应用程序”送给她鲜花。说真的,这不是浪漫的友谊。她真的很喜欢我给她讲那些愚蠢的虚构故事。只是想为她做点好事。这就是全部。更不用说,她实际上也可以编写代码,因此更有意义D