[英]I cant get a JFrame to close and another open (in a different class) with a click off a JButton
So far all i have managed to get is the JButton to close the first JFrame (frame) but the other will not open. 到目前为止,我设法获得的就是关闭第一个JFrame(框架)的JButton,但另一个按钮无法打开。
Frame class code 框架类代码
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
}
} }
On the (frame) action listener i have left out the second line to open the other JFrame as my attempts wont work 在(frame)动作侦听器上,我遗漏了第二行以打开其他JFrame,因为我的尝试无法正常工作
Game class code 游戏类代码
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);
}
}
If anyone has any suggestions on how I can sort this I will be very thankful. 如果有人对如何进行分类提出任何建议,我将非常感谢。
You're disposing the JFrame and setting its default close operation to JFrame.EXIT_ON_CLOSE
which closes the JVM, shutting down your entire program. 您正在处置JFrame,并将其默认关闭操作设置为
JFrame.EXIT_ON_CLOSE
,这将关闭JVM,从而关闭整个程序。
setVisible(false)
setVisible(false)
使您的JFrame不可见 dispose()
, make sure that you set its default close operation to anything but what you're currently doing: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dispose()
,请确保将其默认关闭操作设置为当前正在执行的操作以外的任何操作: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
as this also closes the JVM.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.