基本上,我想做的是JFrame
在同一个类中用一个按钮关闭 a 。我有 2 节课Class1
和Class2
. 当我点击“添加数据”按钮时Class1
,它会打开Class2
(有点像一个对话框),Class2
当我点击“完成”按钮时我想关闭它。
-------------------------------------------Class1------ -------------
public class Class1 extends JFrame{
private JPanel contentPane;
public Class1(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 355, 251);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setForeground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Add labels and other relevant content here
JButton addData = new JButton("Add Data");
addData.setBounds(32, 135, 130, 23);
contentPane.add(addData);
addData.addActionListener(new addDataActionListener());
}
class addDataActionListener implements ActionListener{
public void actionPerformed(ActionEvent arg5) {
Class2 co = new Class2();
co.setVisible(true);
//opening the Class2 JFrame
}
}
}
-------------------------------------------Class2------ -------------
public class Class2 extends JFrame {
private JPanel contentPane;
public Class2(){
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 415, 238);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Add labels and other relevant content here
JButton done = new JButton("Done");
done.setBounds(206, 164, 89, 23);
contentPane.add(done);
done.addActionListener(new doneActionListener());
}
class doneActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//add stuff that needs to be done
Class2 co2 = new Class2();
co2.setVisible(false);
co2.dispose();
}
但是,当我单击“完成”按钮时,它会执行所有其他操作,但不会关闭 Class2 中的框架。如果有人可以让我知道它是如何完成的,将不胜感激。
附言。我是 Java 新手(大约 4 个月前开始)。对不起,如果我不够清楚。提前致谢 :)