1

例えば:

で [表示] をJButton1クリックしたとき、 および で[閉じて表示] をクリックしたとき。JInternalFrame1JDesktopPaneJButton2JInternalFrame1JInternalFrame2JDesktopPane

前にthx

編集:コメントからのコードで

if (JInternalFrame1 == null) { 
    JInternalFrame1 = new FJInternalFrame(); 
    Desktop.add(JInternalFrame1); 
    JInternalFrame1.toFront();
} else { 
    JInternalFrame1.dispose();
}
4

1 に答える 1

6

この例を見てください。新しいフレームを作成するたびに異なるタイトルを持つカスタム JInternalFrame を作成しました。ボタンをクリックすると、新しいものが作成され、古いものは消えます

これがあなたを助けるかもしれない重要なコードです。デスクトップ サイズが 0 の場合は新しいフレームを追加し、それ以外の場合は前のフレームを削除して新しいフレームを追加し、再検証します。

   button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (desktop.getAllFrames().length == 0) {
                desktop.add(new MyInternalFrame());

            } else {
                desktop.remove(0);
                desktop.add(new MyInternalFrame());
                revalidate();
                repaint();
            }
        }
    });

これが完全なコードです。2 つの異なるファイルです。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class InternalFrameDemo1 extends JPanel {

    JDesktopPane desktop;
    JButton button;

    public InternalFrameDemo1() {
        desktop = new JDesktopPane();
        button = new JButton("Get Next Frame");

        setLayout(new BorderLayout());
        add(desktop, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (desktop.getAllFrames().length == 0) {
                    desktop.add(new MyInternalFrame());

                } else {
                    desktop.remove(0);
                    desktop.add(new MyInternalFrame());
                    revalidate();
                    repaint();
                }
            }
        });
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new InternalFrameDemo1());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

import javax.swing.JInternalFrame;

public class MyInternalFrame extends JInternalFrame {
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;

    public MyInternalFrame() {
        super("Document #" + (++openFrameCount),
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable

        setSize(300,300);
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
        setVisible(true);
    }
}

ここに画像の説明を入力

于 2013-12-25T17:48:35.393 に答える