这是我的问题。我只是想为朋友的生日写一个漂亮的小应用程序。问题是当我运行程序时,我最初得到一个空白的 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
}
}