Java 如何关闭当前扩展JFrame
我有一个扩展的JFrame,但不能使用frame.setVisible(false);关闭它。我不想关闭整个程序,但想在另一个类中打开我的另一个扩展JFrame。你是怎么做到的?我的另一个类叫做Genders,当我按下另一个扩展JFrame上的按钮(btnNewGame)时,它必须打开 下面是我的类打开下一个JFrame的代码,但我无法关闭当前JFrame: Class1(我要关闭的JFrame):Java 如何关闭当前扩展JFrame,java,button,jframe,Java,Button,Jframe,我有一个扩展的JFrame,但不能使用frame.setVisible(false);关闭它。我不想关闭整个程序,但想在另一个类中打开我的另一个扩展JFrame。你是怎么做到的?我的另一个类叫做Genders,当我按下另一个扩展JFrame上的按钮(btnNewGame)时,它必须打开 下面是我的类打开下一个JFrame的代码,但我无法关闭当前JFrame: Class1(我要关闭的JFrame): import java.awt.BorderLayout; import java.aw
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);
}
}
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);
}
}
Class2(我正在打开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);
}
}
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)
此代码将用于要关闭的类(例如Class1)要处理当前类(表单)并打开另一个类,可以使用以下代码。要退出应用程序,请使用System.exit(0)
此代码将用于要关闭的类(例如Class1)如果两个帧是同一程序的一部分,请不要在每个帧中放置
public static void main()
。同时删除带有JFrame的行。在关闭时退出(或者关闭帧时程序将退出)。如果两个帧是同一程序的一部分,请不要在每个帧中放置public static void main()
。同时用JFrame删除该行。在关闭时退出(或者当你关闭该帧时程序将退出)。非常感谢你,Peter!你结束了我长期的奋斗。我只需要一部分代码就可以让它顺利运行。@WhiteRiger请投票选出答案,如果你高兴的话,将其设置为你问题的答案。非常感谢你,Peter!你结束了我长期的奋斗。我只需要一部分代码就可以让它顺利运行。@WhiteRiger请投票选出答案,如果您满意,将其设置为您问题的答案。