0

我正在测试一些正在使用的代码internal frame。所以我不想打开internal frame已经在我的程序中运行的新的。我的意思是当我单击一个菜单打开一些internal frame已经在我outerframe的 . 请。

4

1 回答 1

1

这是一个非常简单的例子,希望对你有所帮助。

我扩展了一个 JInternalFrame

import javax.swing.JInternalFrame;

public class WeatherIFrame extends JInternalFrame {

    public WeatherIFrame() {
        this.setTitle("Weather");
        this.setSize(300, 200);
        this.setVisible(true);
        this.setClosable(true);
        this.setResizable(true);        
    }
}

一个类,它生成 JFrame 并包含 JDesktopPane

import de.professional_webworkx.dashboard.internalframes.WeatherIFrame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

public class MainFrame extends JFrame {

    private JDesktopPane desktopPane;
    private WeatherIFrame weatherIFrame;

    /**
     * Menuitems
     */
    MenuItem openWeatherFrame;
    MenuItem exitApp;

    public MainFrame() {            
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Dashboad");
        this.setSize(1024, 768);
        this.getContentPane().add(initDesktopPane());
        this.setMenuBar(initMenu());
        this.setVisible(true);
    }

    private JDesktopPane initDesktopPane() {
        desktopPane = new JDesktopPane();
        return desktopPane;
    }

    private MenuBar initMenu() {
        MenuBar menuBar = new MenuBar();
        Menu fileMenu = new Menu("File");
        openWeatherFrame = new MenuItem("Show Weatherforcast");
        openWeatherFrame.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(weatherIFrame == null) {
                    weatherIFrame = new WeatherIFrame();
                    weatherIFrame.addInternalFrameListener(new InternalFrameListener() {

                        @Override
                        public void internalFrameOpened(InternalFrameEvent e) {
                        }

                        @Override
                        public void internalFrameClosing(InternalFrameEvent e) {
                        }

                        @Override
                        public void internalFrameClosed(InternalFrameEvent e) {
                            weatherIFrame = null;
                        }

                        @Override
                        public void internalFrameIconified(InternalFrameEvent e) {
                        }

                        @Override
                        public void internalFrameDeiconified(InternalFrameEvent e) {
                        }

                        @Override
                        public void internalFrameActivated(InternalFrameEvent e) {
                        }

                        @Override
                        public void internalFrameDeactivated(InternalFrameEvent e) {
                        }
                    });
                    desktopPane.add(weatherIFrame);
                }

            }
        });            
        fileMenu.add(openWeatherFrame);
        menuBar.add(fileMenu);
        return menuBar;
    }
}

现在,运行它。

import de.professional_webworkx.dashboard.frame.MainFrame;
import javax.swing.SwingUtilities;

public class Dashboard {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainFrame();
            }
        });
    }    
}
于 2014-02-21T08:59:12.627 回答