我有一个扩展的JFrame,但无法使用frame.setVisible(false);关闭它。我不想关闭整个程序,但想在另一个类中打开我的其他扩展 JFrame 之一。怎么办呢?我的另一个类称为 Genders,当我按下另一个扩展 JFrame 上的按钮(btnNewGame)时,它必须打开。
这是我的类打开下一个 JFrame 的代码,但我无法关闭当前的 JFrame:
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):
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);
}
}
最佳答案
为了处理当前的类(表单)并打开另一个类(表单),您可以使用以下代码。要退出您的应用程序,请使用 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);
}
}
});
此代码将在您要关闭的类中使用(例如Class1)
关于java - 如何关闭当前的扩展JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39182797/