下面是添加JInternalFrame
實例爲JDesktopPane
的工作示例。請注意,您需要設置一個大小或它們不會出現。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class FrameWithInternalFrames {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
gui.setPreferredSize(new Dimension(400, 100));
gui.setBackground(Color.WHITE);
final JDesktopPane dtp = new JDesktopPane();
gui.add(dtp, BorderLayout.CENTER);
JButton newFrame = new JButton("Add Frame");
ActionListener listener = new ActionListener() {
private int disp = 10;
@Override
public void actionPerformed(ActionEvent e) {
JInternalFrame jif = new JInternalFrame();
dtp.add(jif);
jif.setLocation(disp, disp);
jif.setSize(100,100); // VERY important!
disp += 10;
jif.setVisible(true);
}
};
newFrame.addActionListener(listener);
gui.add(newFrame, BorderLayout.PAGE_START);
JFrame f = new JFrame("Demo");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
您可能不應該擴展'JFrame','JInternalFrame' **或**'JPanel'。回答你的問題,是的,這是可能的。至於如何,你有什麼問題?用特定問題發佈當前代碼的[SSCCE](http://sscce.org/)。 –
其實我使用'netbeans'拖放功能自動創建代碼來設計'gui'。所以,如果刪除擴展部分會很麻煩。 'NewMDIApplication'和'NewJPanel'都是獨立的類。我想知道應該使用哪種方法將'NewJPanel'的對象加載到'JDesktopPane'中。 這裏是兩個類的代碼: public class NewJFrame extends javax.swing.JFrame { – Ritesh
*「其實我在使用netbeans設計gui」*這聽起來像Netbeans正在做'做',你是它的奴隸。我建議你停止使用Netbeans並學習如何編寫Java。事實上,我懷疑任何人都可以給你'神奇的咒語'來讓Netbeans這樣做。 –