有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编辑来自另一个类的JLabel文本

所以我要做的是编辑来自另一个类的JLabel文本。我是用标签做的。setText(“bla-bla”);但这不会影响我的JLabel

GUI类中的代码如下所示:

public class GUI {

    JFrame f1 = new JFrame("GUI");
    JLabel l1 = new JLabel("Output");
    JTextField tf1 = new JTextField("");

    public run(){  // main calls this method.

    Listener listener = new Listener();

    f1.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    f1.setBounds(450, 170, 400, 400);
    f1.setVisible(true);
    f1.setResizable(false);
    f1.setLayout(null);

    l1.setBounds(8, 8, 200, 30);
    listener.listen(tf1);

    f1.add(l1);
    }
}

然后我有一个侦听器类,它应该根据用户输入对JLabel进行更改。代码如下:

public class Listener {

    GUI gui = new GUI();

    public void listen(final JTextField textfield) {

        textfield.getDocument().addDocumentListener(new DocumentListener() {

            public void changedUpdate(DocumentEvent e) {
                test();
            }

            public void removeUpdate(DocumentEvent e) {
                test();
            }

            public void insertUpdate(DocumentEvent e) {
                test();
            }

            public void test() {
                if (gui.everythingOK()) { // everythingOK is just a boolean that test if input is valid number and returns true if it is.
                    gui.l1.setText("No errors."); // this code is supposed to change JLabels text.
                } else {
                    gui.l1.setText("Error."); // this code is supposed to change JLabels text.
                }
            }
        });
    }
}

方法一切都很完美,你只要相信我就行了。如果我将JLabel定义为static,它就可以工作,但它只在第一次工作。在第一次更改之后,JLabel中不再出现任何更改,因此将其定义为静态对我没有帮助。我希望有人知道这个代码有什么问题。不介意是否有明显的错误,因为我只考虑了非常长的代码中最重要的部分

提前谢谢

以下是我的everythingOK代码:

public boolean everythingOK() {
        if (hasInt(tf1) && isValid(tf1)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean hasInt(JTextField textfield) {
        try {
            Integer.parseInt(textfield.getText());
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public boolean isValid(JTextField textfield) {
        if (hasInt(textfield)) {
            if (Integer.parseInt(textfield.getText()) >= minValue && Integer.parseInt(textfield.getText()) <= maxValue) {
                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }

    }

共 (1) 个答案

  1. # 1 楼答案

    侦听器类创建另一个Gui实例

    GUI gui = new GUI();
    

    Listener中的代码。test()更改其Gui实例中的l1标签,而不是显示的Gui中的l1标签

    您必须向侦听器提供对真实Gui对象的引用

    您可能还需要将设置新标签的代码包装为SwingUtilities。调用器,以便从事件调度线程执行

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          <guiInstance>.l1.setText("Query: " + queryNo);
        }
      });
    

    更新: 下面是实现您所需的示例代码。你可以照原样去玩。看看GUI类在创建它自己的实例(新的Listener)时是如何将它自己的实例提供给它的。如果文本字段包含文本,则标签打印“无错误”,否则在中打印“错误”

    猪的胆量。在这种情况下,调用器部分实际上不是必需的。但如果你进一步开发你的程序,并开始添加想要更新UI的后台线程,那么你需要这样做。只是给以后的警告…;-)

    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import java.awt.*;
    
    public class GUI {
    
        JFrame f1 = new JFrame("GUI");
        JLabel l1 = new JLabel("Output");
        JTextField tf1 = new JTextField("");
    
        public void run(){  // main calls this method.
            f1.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            f1.setBounds(450, 170, 400, 400);
            f1.setVisible(true);
            f1.setResizable(false);
            f1.setLayout(new GridLayout(2,1));
    
            f1.add(l1);
            f1.add(tf1);
    
            f1.pack();
    
            Listener listener = new Listener(this);
            listener.listen(tf1);
        }
    
        public static void main(String[] args) {
            new GUI().run();
        }
    
        public boolean everythingOK() {
            return tf1.getText().length() > 0;
        }
    
        class Listener {
            private GUI gui;
    
            public Listener(GUI gui) {
                this.gui = gui;
            }
    
            public void listen(final JTextField textfield) {
    
                textfield.getDocument().addDocumentListener(new DocumentListener() {
    
                    public void changedUpdate(DocumentEvent e) { test(); }
                    public void removeUpdate(DocumentEvent e) { test(); }
                    public void insertUpdate(DocumentEvent e) { test(); }
    
                    public void test() {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                if (gui.everythingOK()) { // everythingOK is just a boolean that test if input is valid number and returns true if it is.
                                    gui.l1.setText("No errors."); // this code is supposed to change JLabels text.
                                } else {
                                    gui.l1.setText("Error."); // this code is supposed to change JLabels text.
                                }
                            }
                        });
                    }
                });
            }
        }
    }