我尝试显示另一个类 ABC 中名为 testView 的类的窗口。窗口包含一个按钮。我想在单击该按钮时关闭窗口。我怎样才能关闭它?
public class testView extends JFrame {
protected JButton closeButton = new JButton("Close");
testView(){
this.setSize(1000,700);
this.setTitle("Test");
Container window = getContentPane();
window.setLayout(new FlowLayout());
this.setResizable(false);
window.add(closeButton);
}
}
public class ABC{
public static void main(String[] args) {
testView View = new testView();
View.setVisible(true);
}
}
窗口是从另一个类 ABC 显示的。如何在单击按钮时关闭窗口?
最佳答案
你可以这样做:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
frameToClose.dispose();
}
});
此代码添加一个按钮操作监听器,然后当该按钮有操作时它会告诉框架关闭。希望这有帮助:)
关于java - 单击按钮时关闭容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43336920/