2012-09-19 77 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