我有一个netbeans Java应用程序,应该在启动应用程序时显示一个JFrame(类StartUpWindow扩展JFrame)和一些选项,然后用户单击一个按钮,JFrame应该关闭,一个新的(MainWindow类)应该被打开了。
那么我该如何正确地做到这一点。我显然在StartupWindow的按钮上设置了一个单击处理程序但是我在这个处理程序中放了什么以便我可以关闭StartUpWindow并打开MainWindow?看起来线程也会出现这种情况,因为每个窗口似乎都有自己的线程......或者JFrames自己自动处理线程问题......
答案 0 :(得分:25)
以下是一个例子:
<强> 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);
}
}
答案 1 :(得分:9)
您可以在当前窗口中调用dispose()
,在要显示的内容上调用setVisible(true)
。
答案 2 :(得分:9)
这显然是您应该使用CardLayout的情况。这里不是打开两个JFrame,而是使用CardLayout简单地更改JPanel。
负责创建和显示GUI的代码应该在SwingUtilities.invokeLater(...)中;它是线程安全的方法。有关详细信息,您必须阅读Concurrency in 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();
}
});
}
}
输出将是:
和
答案 3 :(得分:1)
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");
}
//您可以使用它启动其他应用,并可以在许多应用中分割整个项目。它 会工作很多
答案 4 :(得分:0)
答案 5 :(得分:0)
在调用打开新窗口的方法后立即调用以下方法,这将关闭当前窗口。
private void close(){
WindowEvent windowEventClosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowEventClosing);
}
同样在JFrame的属性中,确保 defaultCloseOperation 设置为 DISPOSE 。
答案 6 :(得分:0)
您可以隐藏JFrame的一部分,其中包含您想要在另一个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);