Add JInternalFrame to JDesktopPane in execution time
I have a problem with a JDesktopPane
, I add JInternalFrame
on it and then show it on a JFrame
.
The problem is when I try to add another JInternalFrame
in execution time.
I use the same method to add the same JInternalFrame
but its dosnt shows up.
public class Desktop extends JDeskto开发者_如何学JAVApPane {
(...)
public void addJInternalFrameBox(JInternalFrameBox jifb) {
this.add(jifb, desktop.CENTER_ALIGNMENT);
this.repaint();
this.validate();
}
}
JInternalFrameBox class:
public class JInternalFrameBox extends JInternalFrame {
(...)
public JInternalFrameBox(Integer id) {
this.id = id;
setUpFrame();
}
public void setUpFrame() {
JLabel lbl = new JLabel("test");
lbl.setVisible(true);
this.add(lbl);
this.setPreferredSize(INTERNAL_FRAME_SIZE);
this.setLocation(100, 100);
this.setIconifiable(true);
this.setClosable(true);
this.pack();
this.setVisible(true);
}
}
jButtonBox the button that open the JInternalFrameBox:
public class jButtonBox extends JButton implements MouseListener {
public void mouseReleased(MouseEvent e) {
JInternalFrameBox jifb = new JInternalFrameBox(id);
jifb.setVisible(true);
Desktop df = Desktop.getInstance();
df.addJInternalFrameBox(jifb);
}
(...)
}
Read the section from the Swing tutorial on How to Use Internal Frames for a working example.
Don't use a JPanel for your desktop, but rather use a JDesktopPane. That's specifically what its for.
you have to set both location and size of the internal frame, as in
setSize(INTERNAL_FRAME_SIZE); // instead of setPref
setLocation(100, 100);
hm ... maybe not (just saw the pack in your code) - no more guessing without an sscce, as others already stated
精彩评论