有谁知道为什么我必须扩展 JFrame 然后将其缩小才能显示文本和按钮?这是我遇到问题的 JFrame 之一的示例。
//method that develops 'WelcomeFrame'
public void WelcomeFrame()
{
//creates new frame with 'choiceFrame' variable
welcomeFrame = new JFrame("Welcome!");
//sets 'welcomeFrame' to visible
welcomeFrame.setVisible(true);
//sets size of frame
welcomeFrame.setSize(330,115);
//frame will close when you hit close button
welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//displays frame in middle of page
welcomeFrame.setLocationRelativeTo(null);
//initializes labels and what they will display
instructions = new JLabel("<html>Welcome to Moe's Original BBQ Application!<br>"
+"Please click CONTINUE to proceed to the home page!</html>");
//creates new panel, hold all components and displays them on frame
JPanel welcomePanel = new JPanel();
//adds label to panel
welcomePanel.add(instructions);
//sets panel background to light gray
welcomePanel.setBackground(Color.lightGray);
//sets 'continueButton' background/foreground to light gray
continueButton.setBackground(Color.lightGray);
continueButton.setForeground(Color.darkGray);
//adds button to panel
welcomePanel.add(continueButton);
//adds panel to frame
welcomeFrame.add(welcomePanel);
}
谢谢
最佳答案
您调用setVisible()
在您的JFrame
之前设置完成,尝试调用setVisible()
当你的JFrame
已经完全设置完毕。 setVisible()
将调用重绘,重绘将绘制已添加且当前可见的任何内容(对于您而言,您除了 JFrame
本身之外什么都没有调用它)。调整框架大小会导致另一个重绘事件排队,此时更多可见组件已被添加到其中,现在将被绘制。
关于java - Java 中的 JFrame 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22545566/