我正在测试一些使用internal frame
的代码。所以我不想打开已在我的程序中运行的新internal frame
。我的意思是当我点击一个菜单打开已在internal frame
中打开的outerframe
时,内部框架可能会在我的框架中重复,我该如何解决这种情况。请。
答案 0 :(得分: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);
}
}
一个Class,它生成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();
}
});
}
}