在applet中看不到GUI组件?

时间:2018-02-11 01:18:45

标签: java swing user-interface graphics applet

由于某种原因,我的GUI组件没有显示在我的applet中。我设置了一个单独的类,我创建了所有GUI组件并将它们添加到我创建的面板中。之后,我将带有创建的面板的对象添加到applet,但由于某种原因,它没有显示在我的applet上。我确保在添加保存我的组件的对象之后将我的applet上的可见性设置为true,但这也没有帮助。

public class CreatePannel extends JPanel{

JPanel panel=new JPanel();

public CreatePanel()  {//This is my constructor for the object that I created to create my components and add them to my panel called leftPanel that I set as an instance variable at the start of this class.  
  JButton button=new JButton();
  panel.add(button);
  }
}

 public class GUI extends JApplet{

  public void init() {// In a separate class that extends Applet
   setLayout(new BorderLayout());
   CreatePanel test=new CreatePanel();
   add(test);//TRYING TO ADD GUI COMPONENTS TO MY APPLET
   setVisible(true); 
   }

 }

1 个答案:

答案 0 :(得分:-2)

您永远不会将组件添加到GUI。这个构造函数:

public CreatePanel()  {
    JButton button=new JButton();
    panel.add(button);
}

创建一个JButton并将其添加到面板JPanel,但随后不对这些人做任何事情,因为它们从未添加到包含JPanel类中。你需要更多,特别是:

public CreatePanel()  {
    JButton button=new JButton();
    panel.add(button);
    this.add(panel);  // the "this" is not actually needed but there for emphasis
}

此代码现在将面板JPanel添加到包含的CreatePannel对象中,然后最终将其添加到JApplet。

相关问题