java - Java 中的 JFrame 问题

标签 java jframe

有谁知道为什么我必须扩展 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/

相关文章:

java - 从像素坐标在图片上绘制矩形

java - 如何在 DefaultListModel 中而不是在 JList 中添加操作监听器

java - 辅导日志,混淆 JButton 的代码

java - 如何保存 JFrame java 1.6 的状态

java - 添加了新的 Pushpixal 主题,但没有变化

java - 扩展 JFrame 创建新的 JFrame 对象

java - jstack 的输出缺少 tid 和 nid 信息

java - 两个正整数乘以long的结果为负值

用于选择性类或方法的 Java CGlib 增强器

java - 如何检查 JFrame 是否存在