Java 如何使一个类对另一个类中按下的按钮作出反应?

Java 如何使一个类对另一个类中按下的按钮作出反应?,java,swing,button,Java,Swing,Button,嗨,我是一个编程新手,我一直在努力解决问题。提前谢谢你的帮助 我试着在一个班上做一个按钮,当按下按钮时,另一个班就知道了 这里是第一个类,它包含我想在另一个类中调用的testWindow方法 import javax.swing.*; import java.awt.event.*; public class TestWindow { public static void testWindow() { JFrame frame = new JFrame("test")

嗨,我是一个编程新手,我一直在努力解决问题。提前谢谢你的帮助

我试着在一个班上做一个按钮,当按下按钮时,另一个班就知道了

这里是第一个类,它包含我想在另一个类中调用的testWindow方法

import javax.swing.*;
import java.awt.event.*;    
public class TestWindow {
    public static void testWindow() {
      JFrame frame = new JFrame("test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JLabel text = new JLabel("this is a test!",SwingConstants.CENTER);
      text.setBounds(0,30,300,50);

      JButton button = new JButton("Start");
      button.setBounds(100,100,100,40);

      frame.add(text);
      frame.add(button);
      frame.setSize(300,200);
      frame.setLayout(null);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            //I don't know what to put here
         }
      }); 
   }
}
这里是第二个类,我想在其中使用testWindow方法

public class MainTest extends TestWindow {
   public static void main(String[] arg){
      testWindow();
      //other stuff that happens when "start" is pressed
   } 
}
当我运行MainTest类时,testWindow会按它应该的样子出现。但当按下“开始”按钮时,我想关闭该帧,然后在main方法中执行其他操作。我该怎么做呢

当我运行MainTest类时,testWindow会按它应该的样子出现。但当按下“开始”按钮时,我想关闭该帧,然后在main方法中执行其他操作。我该怎么做呢

您需要模态对话框的功能,这是一个窗口,在处理完之前停止程序流。在这种情况下,您不应该使用不允许这种模态的JFrame,而应该使用Swing模态对话框,例如创建、生成模态并显示的JOptionPane或JDialog。然后GUI程序流停止,直到对话框窗口不再可见

如果这样做,按钮的动作侦听器所要做的就是关闭保存它的对话框窗口,就是这样


旁注:您在这里误用了继承,因为您的MainTest类应该是从TestWindow类扩展而来的。虽然这在这个简单的代码中可能并不重要,但它可以并且将在将来的代码中引起问题


e、 g


frame.dispose()
应该可以工作。@inavda:nope。它不会阻止主方法代码在窗口显示时运行。右-捕捉得好。@Nicole:通过在类外显式调用静态方法,如我上面的示例所示:
TestWindow.TestWindow()非常感谢您的帮助。我真的很感激。初始化eb后创建的面板的目的让我有点困惑。你为什么要这么做?@Nicole:你有“坏代码”™ 在您发布的代码中,您正在设置边界并使用空布局,您应该避免像瘟疫这样的事情。创建JPanel是为了简化向GUI中添加组件的过程,而不必求助于空布局混乱。学习并使用布局管理器。
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;

public class TestWindow {
    public static void testWindow() {
        // JFrame frame = new JFrame("test");
        final JDialog frame = new JDialog((JFrame) null, "Test", ModalityType.APPLICATION_MODAL);
        frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel text = new JLabel("this is a test!", SwingConstants.CENTER);
        // text.setBounds(0, 30, 300, 50);

        JButton button = new JButton("Start");
        // button.setBounds(100, 100, 100, 40);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });

        int eb = 15;
        JPanel panel = new JPanel(new BorderLayout(eb, eb));
        panel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
        panel.add(text, BorderLayout.PAGE_START);
        panel.add(button, BorderLayout.CENTER);

        frame.add(panel);
        frame.pack();
        // frame.setSize(300, 200);
        // frame.setLayout(null);

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
import javax.swing.SwingUtilities;

public class TestTestWindow {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            TestWindow.testWindow();
            System.out.println("Called after test window no longer visible");
        });
    }
}