2012-09-19 73 views
3

这是我的问题。我只是想为她的生日写一个好的小应用程序给一位朋友。问题是当我运行程序时,我最初得到一个空白的GUI。但是,如果我调整窗口的边界,即使是最细微的一点,问题也会出现,就像它应该的那样。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 

    } 

} 
+2

*“为她的生日写一封很好的小朋友申请给朋友”*给她鲜花。认真。 –

+0

这不是一个浪漫的友谊。当我告诉她愚蠢的故事时,她真的很享受。只是想为她做点好事。就这样。更何况,她实际上可以编码,所以它更有意义。 :D – pzyren

回答

4

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

4

打一个电话,到mainFrame.setVisible(true)gui()方法结束。删除所有其他事件。

+0

这工作很好。十亿感谢:D – pzyren