Originariamente inviato da PazZII
e nel caso di un jinternaframe?
codice:
import java.awt.*;
import javax.swing.*;
public class TestFrame extends JFrame {
public TestFrame() {
super("Test");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500, 400);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.add(new IntFrame());
getContentPane().add(desktopPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestFrame().setVisible(true);
}
});
}
}
class IntFrame extends JInternalFrame {
private JTextArea textArea;
private JButton button1;
private JButton button2;
public IntFrame() {
super("Test", true, true, true, true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(300, 200));
button1 = new JButton("1");
button2 = new JButton("2");
JPanel buttonsPanel = new JPanel(new GridLayout(1, 2));
buttonsPanel.add(button1);
buttonsPanel.add(button2);
Container contentPane = getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.add(buttonsPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
Come puoi vedere, cambia relativamente poco. C'è solo di mezzo un JDesktopPane che ora occupa lui tutto il content pane del JFrame mentre tutto il setup di prima del JFrame l'ho spostato in una classe separata che ora estende JInternalFrame e in cui cambia davvero poco (i flag a true nella invocazione di super() per renderlo ridimensionabile/chiudibile ecc..., il default close operation che non può essere più EXIT_ON_CLOSE e il setVisible al fondo che invece serve).