我设置JTextField
“rfid”至setEnabled(false)
在 MainGUI 类中并创建方法 setRfidEnabled
能够从另一个名为 CardLayout 的类启用文本字段。
当我尝试通过按钮事件监听器从 CardLayout 调用它时,它什么也不做,我的意思是文本字段,因为 System.out.print("LOL");
工作正常。 MainGUI 包含 JFrame 并通过按钮调用 CardLayout 类中的另一个 JFrame。
当我初始化 MainGUI 类时,它有 Thread[Thread-2,6,main]
,但是当我调用 CardLayout 时,它变成 Thread[AWT-EventQueue-0,6,main]
,与 CardLayout 本身相同。我尝试使“rfid”不稳定,但没有成功。
---编辑后的代码---
主GUI:
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 类:
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 类:
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上面的评论所述
每次单击手动
按钮时,您都会创建一个新的MainGUI()
。
您需要在构造函数或 ActionListener 中创建一个实例,并询问您是否已经拥有它的实例(即 Singleton)并使用它。
如果您决定使用第一个,请将 gui
声明为全局变量:
MainGUI gui = new MainGUI();
并且在您的 ActionListener
上将其更改为:
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(currentThread());
gui.setRfidEnabled();
//frame.dispose();
}
然后你就有了它的一个实例。
正如 @Sergiy 所说,你并不真正需要所有这些线程
以下是一些有关如何使用 ActionListeners
的示例:
- I'm trying to make a button to count characters in a text field
- AppletViewer bugged and trying to involve a timer
- Calculator returns 0.0 to all questions asked
- Java - My action event doesn't work
- Drawing shapes on a JForm java
- Animated Sprites with Java Swing该线程包含一个
计时器
(另一个处理动画但不阻止 EDT 的线程)
正如您在上述所有示例中所看到的,它们都不需要另一个线程来处理操作,使用线程的示例仅用于执行动画而不对用户点击使用react。
推荐教程:How to use Actions
关于java - 从第一个 JFrame 调用的方法在第二个 JFrame 中不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56113513/