Launching JFrame from a button
I've got the following button...
public void actionPerformed(ActionEvent arg0) {
Contacts contact = new Contacts();
contact.setVisible(true);
}
Contacts is just a simple JApplet...
public class Contacts extends JApplet {
private JPanel jContentPane = null;
public Contacts() {
super();
}
public void init() {
this.setSize(500, 260);
this.setContentPane(getJContentPane());
}
private JPanel getJContentPane() {
if (jCo开发者_运维百科ntentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJList(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
}
return jContentPane;
}
}
Why is what I'm doing not working? How would I launch this JFrame?
Like JFrame
and JDialog
, JApplet
is a top-level container. You can't put one inside the other. Instead, do something like this:
class Contacts extends JFrame { ... }
...
Contacts contact = new Contacts();
contact.setVisible(true);
If you have an existing JApplet
that you want to display in a JFrame
, you can create a hybrid, as shown in the examples examined here.
精彩评论