[英]Loading a separate JPanel/JInternalFrame into a separate JDesktopPane(in a JFrame)
假設我有一個NewMDIApplication extends javax.swing.JFrame
的類,其中包含JDesktopPane
和JButton
。 還要假設我還有另一個類NewJInternalFrame extends javax.swing.JInternalFrame
或NewJPanel extends javax.swing.JPanel
, NewJPanel extends javax.swing.JPanel
包含一些用於執行某些操作的“按鈕”和“文本框”。
我的意圖是當我單擊JButton
時,將NewMDIApplication
的對象或NewJPanel
的對象加載到NewJInternalFrame
的JDesktopPane
中。 有可能做到嗎? 如果是,該怎么做?
這是將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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.