从第一个 JFrame 调用的方法在第二个 JFrame 中什么都不做

Method called from first JFrame does nothing in second

我在 MainGUI class 中将 JTextField "rfid" 设置为 setEnabled(false) 并创建了方法 setRfidEnabled 以便能够从另一个 class 称为 CardLayout。 当我尝试通过按钮事件侦听器从 CardLayout 调用它时,它什么也不做,我的意思是文本字段,因为 System.out.print("LOL"); 工作正常。 MainGUI 包含 JFrame 并通过按钮调用 CardLayout class 中的另一个 JFrame。

当我初始化 MainGUI class 时,它有 Thread[Thread-2,6,main],但是当我调用 CardLayout 时它变成 Thread[AWT-EventQueue-0,6,main],与 CardLayout 本身一样。我试图让 "rfid" 不稳定,但没有成功。

---编辑代码---

主界面:

public class MainGUI {
    JTextField rfid;
    JButton button;
    final JFrame frame;
    final JPanel pane;
    LayoutChanger layout = new LayoutChanger();
    public MainGUI() {
        rfid = new JTextField("", 10);
        button = new JButton("CardLayoutSwitch");
        frame = new JFrame("Main GUI Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout(5,5));
        pane = new JPanel(new GridLayout(5, 5));
        frame.add(pane,BorderLayout.CENTER);
        pane.add(rfid);
        pane.add(button);
        rfid.setEnabled(false);
        button.setEnabled(true);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed (ActionEvent e){
                layout.changeLayout(1);
            }
        });
    }
    public void setRfidEnabled() {
                System.out.println("LOL");
                rfid.setEnabled(true);
                button.setEnabled(false);
    }
}

LayoutChanger class:

public class LayoutChanger {
    public static void main(String[] args) {
    MainGUI gui = new MainGUI();
    }

    public void changeLayout(int i){
        if (i == 1) {
            CardLayout card = new CardLayout();
        }
    }
}

CardLayout class:

public class CardLayout {
    JFrame frame;
    JButton manual;
    final JPanel pane;
    MainGUI gui = new MainGUI();
  public CardLayout() {
            manual = new JButton("UID MANUAL");
            frame = new JFrame("Card Scan Panel");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setLayout(new BorderLayout(5, 5));
            pane = new JPanel(new BorderLayout(5, 5));
            manual.setPreferredSize(new Dimension(50, 25));
            frame.add(pane, BorderLayout.CENTER);
            pane.add(manual);
            frame.pack();
            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
            manual.addActionListener(new ActionListener() {
             @Override
                public void actionPerformed (ActionEvent e){
                    gui.setRfidEnabled();
              }
          });
        }
}

如@matt

上面的评论所述

每次单击 manual 按钮时,都会创建一个 new MainGUI()

您需要在构造函数或 ActionListener 中创建一个实例,并询问您是否已经拥有它的实例(即单例)并使用它。

如果您决定使用第一个,请将 gui 声明为全局变量:

MainGUI gui = new MainGUI();

并且在你的 ActionListener 上更改为:

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println(currentThread());
    gui.setRfidEnabled();
    //frame.dispose();
}

那么你就有了它的一个实例。

另外,正如@Sergiy 所说,您实际上并不需要所有这些线程

这里有一些关于如何使用的例子 ActionListeners:

  • AppletViewer bugged and trying to involve a timer
  • Calculator returns 0.0 to all questions asked
  • 这个包含一个 Timer(另一个处理动画但不阻塞 EDT 的线程)

正如您在上述所有示例中看到的,其中 none 需要另一个线程来处理操作,使用线程的线程仅用于执行动画而不对用户点击做出反应。

推荐教程:How to use Actions