Swing - 处理框架
Swing - Dispose a frame
我的目标是让动作侦听器在用户点击 JButton 退出时关闭特定的 JFrame。
总的来说,当程序启动时,一个大的 JFrame 打开,然后在前面打开一个小的......在我的代码中,用户在这个小的 JFrame 中输入一些细节并点击提交(为了简单起见,我省略了这个在此处编写代码并将提交替换为退出)
所以当这个退出按钮被按下时。我希望这个小 JFrame 能够关闭。我似乎无法弄清楚这一点。不同 class 中的动作侦听器,我尝试创建实例但没有成功。我在尝试解决此问题时注释掉了下面尝试的代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test
{
public static void main(String Args[])
{
makeGUI m = new makeGUI();
}
}
class makeGUI
{
JButton close = new JButton("CLOSE ME");
makeGUI()
{
frame f1 = new frame();
JFrame smallframe = new JFrame(); //want to close this one
JPanel jp = new JPanel(new FlowLayout());
smallframe.setSize(300,300);
smallframe.setLocationRelativeTo(null);
smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
close.addActionListener(new action());
jp.add(close);
smallframe.add(jp);
smallframe.setVisible(true);
}
class action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//makeGUI s1 = new makeGUI();
if (e.getSource () == close)
{
//s1.smallframe.dispose();
System.out.println("gotcha");
}
}
}
}
class frame extends JFrame
{
frame ()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("big one");
setVisible(true);
}
}
如果您想模拟某人按下 [X] 按钮,那么您可以使用此代码以编程方式触发此事件:
smallFrame.dispatchEvent(new WindowEvent(smallFrame, WindowEvent.WINDOW_CLOSING));
除此之外,您的代码无法正常工作,因为您没有关闭小型 window 的实例,而是创建了另一个实例并处理它。在您的关闭事件中,您应该关闭 smallFrame 实例。
您可以通过将 JFrame 传递给 ActionListener 的构造函数或将 smallFrame 设为 class 变量来实现。
您似乎在使用小型 JFrame 作为弹出窗口来获取信息或显示信息。如果是这样,您可能需要查看为 "Dialogue Boxes".
制作的 JOptionPane class
文档:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
首先,用小写字母命名 class 不是一个好习惯,因此请尝试重命名为 MakeGUI
而不是 makeGUI
。
您的注释代码的问题在于,它会在每次 单击按钮并调用动作侦听器时创建一个 makeGUI
的新实例。结果是,当您单击关闭按钮时,会创建一个新框架,然后创建一个内部框架,而这个内部框架会立即关闭。您唯一要做的就是创建越来越多的框架。您应该将实例保持为状态,例如作为 class 成员:
class MakeGUI {
JFrame smallframe;
JButton close = new JButton("CLOSE ME");
MakeGUI() {
frame f1 = new frame();
smallframe = new JFrame(); //want to close this one
JPanel jp = new JPanel(new FlowLayout());
smallframe.setSize(300, 300);
smallframe.setLocationRelativeTo(null);
smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
close.addActionListener(new action());
jp.add(close);
smallframe.add(jp);
smallframe.setVisible(true);
}
class action implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == close) {
// use this instead of dispose
smallframe.dispatchEvent(new WindowEvent(smallframe, WindowEvent.WINDOW_CLOSING));
System.out.println("gotcha");
}
}
}
}
我的目标是让动作侦听器在用户点击 JButton 退出时关闭特定的 JFrame。
总的来说,当程序启动时,一个大的 JFrame 打开,然后在前面打开一个小的......在我的代码中,用户在这个小的 JFrame 中输入一些细节并点击提交(为了简单起见,我省略了这个在此处编写代码并将提交替换为退出)
所以当这个退出按钮被按下时。我希望这个小 JFrame 能够关闭。我似乎无法弄清楚这一点。不同 class 中的动作侦听器,我尝试创建实例但没有成功。我在尝试解决此问题时注释掉了下面尝试的代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test
{
public static void main(String Args[])
{
makeGUI m = new makeGUI();
}
}
class makeGUI
{
JButton close = new JButton("CLOSE ME");
makeGUI()
{
frame f1 = new frame();
JFrame smallframe = new JFrame(); //want to close this one
JPanel jp = new JPanel(new FlowLayout());
smallframe.setSize(300,300);
smallframe.setLocationRelativeTo(null);
smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
close.addActionListener(new action());
jp.add(close);
smallframe.add(jp);
smallframe.setVisible(true);
}
class action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//makeGUI s1 = new makeGUI();
if (e.getSource () == close)
{
//s1.smallframe.dispose();
System.out.println("gotcha");
}
}
}
}
class frame extends JFrame
{
frame ()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("big one");
setVisible(true);
}
}
如果您想模拟某人按下 [X] 按钮,那么您可以使用此代码以编程方式触发此事件:
smallFrame.dispatchEvent(new WindowEvent(smallFrame, WindowEvent.WINDOW_CLOSING));
除此之外,您的代码无法正常工作,因为您没有关闭小型 window 的实例,而是创建了另一个实例并处理它。在您的关闭事件中,您应该关闭 smallFrame 实例。
您可以通过将 JFrame 传递给 ActionListener 的构造函数或将 smallFrame 设为 class 变量来实现。
您似乎在使用小型 JFrame 作为弹出窗口来获取信息或显示信息。如果是这样,您可能需要查看为 "Dialogue Boxes".
制作的 JOptionPane class文档:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
首先,用小写字母命名 class 不是一个好习惯,因此请尝试重命名为 MakeGUI
而不是 makeGUI
。
您的注释代码的问题在于,它会在每次 单击按钮并调用动作侦听器时创建一个 makeGUI
的新实例。结果是,当您单击关闭按钮时,会创建一个新框架,然后创建一个内部框架,而这个内部框架会立即关闭。您唯一要做的就是创建越来越多的框架。您应该将实例保持为状态,例如作为 class 成员:
class MakeGUI {
JFrame smallframe;
JButton close = new JButton("CLOSE ME");
MakeGUI() {
frame f1 = new frame();
smallframe = new JFrame(); //want to close this one
JPanel jp = new JPanel(new FlowLayout());
smallframe.setSize(300, 300);
smallframe.setLocationRelativeTo(null);
smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
close.addActionListener(new action());
jp.add(close);
smallframe.add(jp);
smallframe.setVisible(true);
}
class action implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == close) {
// use this instead of dispose
smallframe.dispatchEvent(new WindowEvent(smallframe, WindowEvent.WINDOW_CLOSING));
System.out.println("gotcha");
}
}
}
}