简体   繁体   English

我无法通过单击JButton来关闭JFrame并再次打开(在其他类中)

[英]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,从而关闭整个程序。

  • Solution 1: Make your JFrame invisible via setVisible(false) 解决方案1:通过setVisible(false)使您的JFrame不可见
  • or if you do use 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. 因为这也会关闭JVM。
  • Solution 2: better yet, make the closing window a JDialog. 解决方案2:更好的是,将关闭窗口设为JDialog。
  • Solution 3: Even better, don't spit different windows at the user, but rather swap views by swapping JPanels with a CardLayout. 解决方案3:更好的是,不要向用户吐出不同的窗口,而是通过将JPanels与CardLayout交换来交换视图 For details, please check out the CardLayout Tutorial . 有关详细信息,请查看CardLayout教程
  • Suggestion: don't use null layouts like you're doing, but rather learn to use the different layout managers. 建议:不要像您一样使用空布局,而要学会使用不同的布局管理器。
  • Suggestion: get out of the main method. 意见建议:摆脱主要方法。 Don't make your classes nothing but large static main methods, basically 1970's procedural programming. 除了大型静态main方法(基本上是1970年代的过程编程)之外,不要使您的类成为什么。 Learn to use and then create OOP-compliant classes with constructors, non-static fields, non-static methods, with states and behaviors, and have these classes interact in a pleasing and useful fashion. 学习使用构造函数,非静态字段,非静态方法以及状态和行为,然后使用它们创建符合OOP的类,并使这些类以令人愉悦和有用的方式进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM