我有一个扩展的JFrame,但不能使用frame.setVisible(false);关闭它。我不想关闭整个程序,但想在另一个类中打开我的其他扩展JFrame。怎么办?我的另一个类叫做Genders,当我在我的另一个扩展JFrame上按下按钮(btnNewGame)时,它必须打开。
以下是我的类打开下一个JFrame的代码,但我无法关闭当前的JFrame:
Class1(我要关闭的JFrame):
SCRIPT AS > CREATE TO > NEW QUERY EDITOR WINDOW.
Class2(JFrame I'开场):
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);
}
}
答案 0 :(得分: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);
}
}
});
此代码将在您要关闭的类中使用(例如,Class1)