Java 我无法通过单击JButton来关闭JFrame和打开另一个JFrame(在另一个类中)
到目前为止,我所得到的只是关闭第一个JFrame(frame)的jb按钮,但另一个不会打开 帧类代码Java 我无法通过单击JButton来关闭JFrame和打开另一个JFrame(在另一个类中),java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,到目前为止,我所得到的只是关闭第一个JFrame(frame)的jb按钮,但另一个不会打开 帧类代码 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Frame { public static void main(String[] args) { // Frame - Labelled BrickFall f
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Frame {
public static void main(String[] args) {
// Frame - Labelled BrickFall
final JFrame frame = new JFrame();
frame.setSize(1290, 730);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("BrickFall");
frame.setLayout(null);
frame.setLocationRelativeTo(null);
// Start Button
JButton Start = new JButton("Start");
Start.setBounds(100, 300, 1080, 50);
frame.add(Start);
// Exit Button
JButton Exit = new JButton("Exit");
Exit.setBounds(369, 375, 540, 50);
frame.add(Exit);
// Closes when Exit Clicked
Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//New Frame opens when Start Clicked
Start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
// Background Image
JLabel background = new JLabel("");
background.setBounds(0, 0, 1280, 720);
background.setIcon(new ImageIcon(Frame.class.getResource("/resources/images/Title.png")));
frame.getContentPane().setLayout(null);
frame.getContentPane().add(background);
frame.setVisible(true);
}
protected static void dispose() {
// TODO Auto-generated method stub
}
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class game {
public static void main(String[] args) {
// Frame - Labelled BrickFall
JFrame game = new JFrame();
game.setSize(1290, 730);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setTitle("BrickFall");
game.setLayout(null);
game.setLocationRelativeTo(null);
// Background Image
JLabel GameBackground = new JLabel("");
GameBackground.setBounds(0, 0, 1280, 720);
GameBackground.setIcon(new ImageIcon(Frame.class.getResource("/resources/images/Fill In.png"))); //Change Picture When Required
game.getContentPane().setLayout(null);
game.getContentPane().add(GameBackground);
game.setVisible(true);
}
}
}
在(框架)动作监听器上,我省略了第二行来打开另一个JFrame,因为我的尝试不起作用
游戏类代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Frame {
public static void main(String[] args) {
// Frame - Labelled BrickFall
final JFrame frame = new JFrame();
frame.setSize(1290, 730);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("BrickFall");
frame.setLayout(null);
frame.setLocationRelativeTo(null);
// Start Button
JButton Start = new JButton("Start");
Start.setBounds(100, 300, 1080, 50);
frame.add(Start);
// Exit Button
JButton Exit = new JButton("Exit");
Exit.setBounds(369, 375, 540, 50);
frame.add(Exit);
// Closes when Exit Clicked
Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//New Frame opens when Start Clicked
Start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
// Background Image
JLabel background = new JLabel("");
background.setBounds(0, 0, 1280, 720);
background.setIcon(new ImageIcon(Frame.class.getResource("/resources/images/Title.png")));
frame.getContentPane().setLayout(null);
frame.getContentPane().add(background);
frame.setVisible(true);
}
protected static void dispose() {
// TODO Auto-generated method stub
}
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class game {
public static void main(String[] args) {
// Frame - Labelled BrickFall
JFrame game = new JFrame();
game.setSize(1290, 730);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setTitle("BrickFall");
game.setLayout(null);
game.setLocationRelativeTo(null);
// Background Image
JLabel GameBackground = new JLabel("");
GameBackground.setBounds(0, 0, 1280, 720);
GameBackground.setIcon(new ImageIcon(Frame.class.getResource("/resources/images/Fill In.png"))); //Change Picture When Required
game.getContentPane().setLayout(null);
game.getContentPane().add(GameBackground);
game.setVisible(true);
}
}
如果有人对我如何排序有任何建议,我将非常感谢。您正在处理JFrame并将其默认关闭操作设置为
JFrame。退出关闭,关闭JVM,关闭整个程序
- 解决方案1:通过
setVisible(false)
- 或者,如果确实使用了
dispose()
,请确保将其默认关闭操作设置为除当前操作之外的任何操作:frame.setDefaultCloseOperation(JFrame.EXIT\u ON\u close)代码>,因为这也会关闭JVM
- 解决方案2:更好的是,将关闭窗口设置为JDialog
- 解决方案3:更好的是,不要向用户吐出不同的窗口,而是通过交换带有CardLayout的JPanel来交换视图。有关详细信息,请查看
- 建议:不要像现在这样使用空布局,而是要学习使用不同的布局管理器
- 建议:退出主方法。不要让你的类只剩下大型静态主方法,基本上是1970年代的过程编程。学习使用并创建具有构造函数、非静态字段、非静态方法、状态和行为的OOP兼容类,并让这些类以令人愉快和有用的方式进行交互
参见