我使用兩個框架。在第一幀我有一個按鈕來打開第二幀。在第二個框架中還有一個按鈕,但是這個按鈕用於關閉第二個框架。但我不知道該怎麼做,我正在尋求一些幫助來解決這個問題?關閉JFrame窗口?
GUI1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 extends JFrame implements ActionListener{
JButton btn1;
Container contentPane;
public GUI1()
{
setTitle("GUI 1");
setResizable(false);
setSize(600,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
btn1 = new JButton("Open GUI 2 frame");
contentPane.add(btn1);
btn1.setFocusable(false);
btn1.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == btn1)
{
GUI2 frame2 = new GUI2();
frame2.setVisible(true);
}
}
public static void main(String[] args) {
GUI1 frame = new GUI1();
frame.setVisible(true);
}
}
GUI2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI2 extends JFrame implements ActionListener {
Container contentPane;
JButton btn2;
public GUI2()
{
setTitle("GUI 2");
setResizable(false);
setSize(400,200);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
btn2 = new JButton("Close GUI 2 frame");
contentPane.add(btn2);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if(event.getSource() == btn2)
{
// Close GUI2 ??
}
}
}
感謝您的幫助! (Det funkade!) –