Java swing应用程序,关闭一个窗口,单击按钮时打开另一个窗口

我有一个netbeans Java应用程序,应该在启动应用程序时显示一个JFrame(类StartUpWindow扩展JFrame)和一些选项,然后用户单击一个按钮,应该关闭JFrame并打开一个新的(MainWindow类)。

那么我该如何正确地做到这一点。 我显然在StartupWindow的按钮上设置了一个单击处理程序但是我在这个处理程序中放了什么以便我可以关闭StartUpWindow并打开MainWindow? 似乎线程也会进入这个,因为每个窗口似乎都有自己的线程……或者是由JFrames自己处理的线程问题……

这是一个例子:

在此处输入图像描述

在此处输入图像描述

StartupWindow.java

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class StartupWindow extends JFrame implements ActionListener { private JButton btn; public StartupWindow() { super("Simple GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn = new JButton("Open the other JFrame!"); btn.addActionListener(this); btn.setActionCommand("Open"); add(btn); pack(); } @Override public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if(cmd.equals("Open")) { dispose(); new AnotherJFrame(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { new StartupWindow().setVisible(true); } }); } } 

AnotherJFrame.java

 import javax.swing.JFrame; import javax.swing.JLabel; public class AnotherJFrame extends JFrame { public AnotherJFrame() { super("Another GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new JLabel("Empty JFrame")); pack(); setVisible(true); } } 

您可以在当前窗口上调用dispose() ,并在要显示的窗口上调用setVisible(true)

这显然是您应该使用CardLayout的情况 。 在这里,您可以做的只是使用CardLayout更改JPanel,而不是打开两个JFrame。

负责创建和显示GUI的代码应该在SwingUtilities.invokeLater(…)中; 它的方法是线程安全。 有关更多信息,您必须阅读有关Swing中的并发性的信息 。

但是,如果您想坚持自己的方法,这里有一个帮助示例代码。

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TwoFrames { private JFrame frame1, frame2; private ActionListener action; private JButton showButton, hideButton; public void createAndDisplayGUI() { frame1 = new JFrame("FRAME 1"); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setLocationByPlatform(true); JPanel contentPane1 = new JPanel(); contentPane1.setBackground(Color.BLUE); showButton = new JButton("OPEN FRAME 2"); hideButton = new JButton("HIDE FRAME 2"); action = new ActionListener() { public void actionPerformed(ActionEvent ae) { JButton button = (JButton) ae.getSource(); /* * If this button is clicked, we will create a new JFrame, * and hide the previous one. */ if (button == showButton) { frame2 = new JFrame("FRAME 2"); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame2.setLocationByPlatform(true); JPanel contentPane2 = new JPanel(); contentPane2.setBackground(Color.DARK_GRAY); contentPane2.add(hideButton); frame2.getContentPane().add(contentPane2); frame2.setSize(300, 300); frame2.setVisible(true); frame1.setVisible(false); } /* * Here we will dispose the previous frame, * show the previous JFrame. */ else if (button == hideButton) { frame1.setVisible(true); frame2.setVisible(false); frame2.dispose(); } } }; showButton.addActionListener(action); hideButton.addActionListener(action); contentPane1.add(showButton); frame1.getContentPane().add(contentPane1); frame1.setSize(300, 300); frame1.setVisible(true); } public static void main(String... args) { /* * Here we are Scheduling a JOB for Event Dispatcher * Thread. The code which is responsible for creating * and displaying our GUI or call to the method which * is responsible for creating and displaying your GUI * goes into this SwingUtilities.invokeLater(...) thingy. */ SwingUtilities.invokeLater(new Runnable() { public void run() { new TwoFrames().createAndDisplayGUI(); } }); } } 

输出将是:

帧1式2

  final File open = new File("PicDic.exe"); if (open.exists() == true) { if (Desktop.isDesktopSupported()) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { try { Desktop.getDesktop().open(open); } catch (IOException ex) { return; } } }); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { //DocumentEditorView.this.getFrame().dispose(); System.exit(0); } }); } else { JOptionPane.showMessageDialog(this.getFrame(), "Desktop is not support to open editor\n You should try manualy"); } } else { JOptionPane.showMessageDialog(this.getFrame(), "PicDic.exe is not found"); } 

//您可以使用它启动其他应用程序,并可以在许多应用程序中切割整个项目。 它会很有用

使用this.dispose将当前窗口关闭,使用next_window.setVisible(true)显示按钮属性ActionPerformed后面的下一个窗口,示例如下图所示为您提供帮助。

在此处输入图像描述

在调用打开新窗口的方法后调用下面的方法,这将关闭当前窗口。

 private void close(){ WindowEvent windowEventClosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowEventClosing); } 

同样在JFrame的属性中,确保将defaultCloseOperation设置为DISPOSE

您可以隐藏包含您想要在另一个JFrame上使用的swing控件的JFrame的一部分。

当用户点击Jbutton时,JFrame宽度增加,当他点击另一种相同类型的Jbutton时,JFrame达到默认大小。

  JFrame myFrame = new JFrame(""); JButton button1 = new JButton("Basic"); JButton button2 = new JButton("More options"); // actionPerformed block code for button1 (Default size) myFrame.setSize(400, 400); // actionPerformed block code for button2 (Increase width) myFrame.setSize(600, 400);