在我的 Swing 应用程序中,我有带有“添加客户”按钮的 MainFrame。当我单击“添加客户”按钮时,我希望在 MainFrame 消失时出现客户表单。客户表单只有 JTabbedPane。AddCustomerPanel 是一个单独的类,它只有取消按钮。AddCustomerPanel 已作为选项卡添加到 Customer 表单的 JTabbedPane。当我单击取消按钮时,我希望客户框架消失并再次出现大型机。我尝试使用 setVisible() 方法。但它没有用。请帮助我做到这一点。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MainFrame extends JFrame{
private JButton btnMain;
MainFrame(){
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
btnMain = new JButton("Add Customer");
btnMain.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Customer().setVisible(true);
this.setVisible(false); // Not working
}
});
add(btnMain);
}
public static void main(String args[]){
new MainFrame();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Customer extends JFrame{
private JTabbedPane tabMain;
Customer(){
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
tabMain = new JTabbedPane();
tabMain.setPreferredSize(new Dimension(490,290));
tabMain.add("Add Customer",new AddCustomerPanel());
add(tabMain);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class AddCustomerPanel extends JPanel{
private JButton btnCancel;
AddCustomerPanel(){
setSize(400,200);
setVisible(true);
setLayout(new FlowLayout());
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false); // Not working
new MainFrame().setVisible(true);
}
});
add(btnCancel);
}
}