简体   繁体   English

如何关闭当前的扩展JFrame

[英]How to close current Extended JFrame

I have an extended JFrame, but can't use the frame.setVisible(false); 我有一个扩展的JFrame,但不能使用frame.setVisible(false); to close it. 关闭它。 I don't want to close the whole program but want to open one of my other extended JFrames in another class. 我不想关闭整个程序,但想在另一个类中打开我的其他扩展JFrame。 How do do that? 怎么办? My other class is called Genders and it must open when I press a button(btnNewGame) on my other extended JFrame. 我的另一个类叫做Genders,当我在我的另一个扩展JFrame上按下按钮(btnNewGame)时,它必须打开。

Here is the code for my classes to open the next JFrame, but I can't close the current one: 这是我的类打开下一个JFrame的代码,但我无法关闭当前的JFrame:

Class1 (the JFrame I want to close) : Class1(我要关闭的JFrame)

    import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import com.sun.glass.events.WindowEvent;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;

public class ProfileHome extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {

                try {
                    ProfileHome profileHome = new ProfileHome();
                    profileHome.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ProfileHome() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        try {
             // Open an audio input stream.           
              File soundFile = new File("C:/My Awesome Stuff/Personal/Carman/My Eclipse Programs/Game/nervous_testpilot - Frozen Synapse Red - Parting Shots.flac"); //you could also get the sound file with a URL
              AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);              
             // Get a sound clip resource.
             Clip clip = AudioSystem.getClip();
             // Open audio clip and load samples from the audio input stream.
             clip.open(audioIn);
             clip.start();
          } catch (UnsupportedAudioFileException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          } catch (LineUnavailableException e) {
             e.printStackTrace();
          }


        JLabel lblWelcomeWarrior = new JLabel("Welcome, Warrior!");
        lblWelcomeWarrior.setForeground(new Color(255, 255, 255));
        lblWelcomeWarrior.setFont(new Font("Perpetua Titling MT", Font.PLAIN, 20));
        lblWelcomeWarrior.setBounds(117, 11, 215, 23);
        contentPane.add(lblWelcomeWarrior);

        JButton btnNewGame = new JButton("New Game");
        btnNewGame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Genders().setVisible(true);

            }
        });
        btnNewGame.setBounds(324, 35, 100, 23);
        contentPane.add(btnNewGame);

        JLabel label = new JLabel("");
        label.setIcon(new ImageIcon("C:\\Users\\WhiteFringe\\Pictures\\Wallpapers\\9gag\\Gifs\\gixdfcbvehy.gif"));
        label.setLabelFor(label);
        label.setBounds(0, 0, 434, 261);
        contentPane.add(label);
    }
}

Class2 (JFrame I'm opening) : Class2(JFrame我正在打开)

    import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class Genders extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) throws IOException{
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Genders frame = new Genders();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    /*String[] imageList = 
            {
                    "female warrior.jpg", "male warrior.jpg"
            };
    */

    public Genders() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        ImageIcon maleicon = new ImageIcon("male warrior.png");

        JLabel Avatar = new JLabel();
        Avatar.setBounds(0, 33, 227, 261);
        contentPane.add(Avatar);

        ImageIcon femaleicon = new ImageIcon("female warrior2.png");

        JButton btnFelame = new JButton("Female");
        btnFelame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ex) {

            Avatar.setIcon(femaleicon);
            }
        });
        btnFelame.setBounds(335, 11, 89, 23);
        contentPane.add(btnFelame);

            JButton btnMale = new JButton("Male");
            btnMale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Avatar.setIcon(maleicon);
                }
            });
            btnMale.setBounds(335, 45, 89, 23);
            contentPane.add(btnMale);

            JButton btnOkay = new JButton("Okay");
            btnOkay.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });
            btnOkay.setBounds(335, 227, 89, 23);
            contentPane.add(btnOkay);

            JLabel Genderbg = new JLabel("");
            Genderbg.setIcon(new ImageIcon("C:\\Users\\WhiteFringe\\Pictures\\Wallpapers\\9gag\\Gifs\\llkokigiphy.gif"));
            Genderbg.setBounds(0, 0, 434, 261);
            contentPane.add(Genderbg);
    }
}

In order to dispose your current class(form) and open another one, you can use the following code. 为了处理当前的类(表单)并打开另一个类,您可以使用以下代码。 In order to exit your application, use System.exit(0) 要退出应用程序,请使用System.exit(0)

   java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            dispose();
            new Class2().setVisible(true);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Class2().class.getName()).log(Level.SEVERE, null, ex);
        }
    }
});

This code is to be used in the class that you want to close (egClass1) 此代码将在您要关闭的类中使用(egClass1)

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

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