Java中的JFrame问题

时间:2014-03-20 21:32:04

标签: 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);  
}

由于

1 个答案:

答案 0 :(得分:2)

setVisible()完成设置之前致电JFrame,在setVisible()完成设置后尝试拨打JFramesetVisible()将调用重绘,并且重绘将绘制已经添加并且当前可见的任何内容(在您称之为JFrame本身的任何内容时)。调整框架大小会导致另一个重绘事件排队,然后将更多可见组件添加到现在并将其绘制。