简体   繁体   English

更新 jLabel

[英]Updating a jLabel

I have a simple GUI that has a jTextField that waits for the user to put in something.我有一个简单的 GUI,它有一个 jTextField 等待用户输入一些东西。 After a button is clicked, the program:单击按钮后,程序:

  1. reads the input, saves it in a String variable;读取输入,将其保存在一个字符串变量中;
  2. opens a new GUI (that is in a separate class file), which contains an empty jLabel, and passes the String variable to it, changing the jLabel text to it.打开一个新的 GUI(位于单独的类文件中),其中包含一个空的 jLabel,并将 String 变量传递给它,将 jLabel 文本更改为它。

The problem is that no matter how hard I try to reconfigure the code, adding things like repaint(), revalidate(), etc., the jLabel in the second GUI stays empty.问题是,无论我多么努力地重新配置代码,添加诸如 repaint()、revalidate() 等内容,第二个 GUI 中的 jLabel 仍然是空的。 Using a System.out.println(jLabel.getText()) reveals that the text value is indeed changed, but not displayed.使用 System.out.println(jLabel.getText()) 表明文本值确实已更改,但未显示。 How do I "refresh" this jLabel, so it'd show what I want it to?我如何“刷新”这个 jLabel,以便它显示我想要的内容? I'm aware I could add an event, though I don't want the user to click anything to refresh the GUI, the values should be there as it's initiated.我知道我可以添加一个事件,虽然我不希望用户点击任何东西来刷新 GUI,但值应该在它启动时就在那里。 I've read trough several similar posts, but found that the solutions don't work for me.我已经阅读了几篇类似的帖子,但发现这些解决方案对我不起作用。

The code of first GUI's button click event:第一个GUI的按钮点击事件代码:

private void sbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    errortext.setText("");
    Search = sfield.getText();
    Transl = hashes.find(Search);
    if (Transl.equals("0")) errortext.setText("Word not found in database.");
    else {
        ws.run(Search, Transl); // <- this opens the second GUI, with two String parameters I want to display in the second GUI;
    }
}

The code of the second GUI (activeword and translation are the jLabels that are giving me trouble.):第二个 GUI 的代码(activeword 和 translation 是给我带来麻烦的 jLabels。):

public void run(String Search, String Transl) {
    WordScreen init = new WordScreen(); //initialise the second GUI;
    init.setVisible(true);
    activeword.setText(Search); 
    translation.setText(Transl);
}

Any reply is very welcome!非常欢迎任何回复! Please ask me for more information about the code if necessary, I will make sure to reply as soon as possible!如有需要,请向我索取更多有关代码的信息,我一定会尽快回复!

Best solution: change WordScreen's constructor to accept the two Strings of interest:最佳解决方案:更改 WordScreen 的构造函数以接受两个感兴趣的字符串:

From this:由此:

public void run(String Search, String Transl) {
    WordScreen init = new WordScreen(); //initialise the second GUI;
    init.setVisible(true);
    activeword.setText(Search); 
    translation.setText(Transl);
}

to this:对此:

public void run(String search, String transl) {
    WordScreen init = new WordScreen(search, transl); 
    init.setVisible(true);
}

Then in the WordScreen constructor use those Strings where needed:然后在 WordScreen 构造函数中,在需要的地方使用这些字符串:

public WordScreen(String search, String transl) {
    JLabel someLabel = new JLabel(search);
    JLabel otherLabel = new JLabel(transl);

    // put them where needed
}

Note that I cannot create a comprehensive answer without your posting a decent MRE请注意,如果您不发布体面的MRE ,我将无法创建全面的答案


As an aside, you will want to learn and use Java naming conventions .顺便说一句,您将需要学习和使用Java 命名约定 Variable names should all begin with a lower letter while class names with an upper case letter.变量名应全部以小写字母开头,而类名应以大写字母开头。 Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.学习这一点并遵循这一点将使我们能够更好地理解您的代码,并使您更好地理解其他人的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM