我正在使用两个框架。在第一帧中,我有一个打开第二帧的按钮。在第二帧中还有一个按钮,但这个按钮用于关闭第二帧。但我不知道该怎么做,我正在寻找一些帮助来解决这个问题?
图形用户界面1
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);
}
}
图形用户界面2
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 ??
}
}
}