更新 jLabel
Updating a jLabel
我有一个简单的 GUI,它有一个等待用户输入内容的 jTextField。单击按钮后,程序:
- 读取输入,将其保存在字符串变量中;
- 打开一个新的 GUI(在单独的 class 文件中),其中包含一个空的 jLabel,并将 String 变量传递给它,将 jLabel 文本更改为它。
问题是无论我多么努力地尝试重新配置代码,添加 repaint()、revalidate() 等内容,第二个 GUI 中的 jLabel 仍然是空的。使用 System.out.println(jLabel.getText()) 显示文本值确实已更改,但未显示。我如何 "refresh" 这个 jLabel 才能显示我想要的内容?我知道我可以添加一个事件,但我不希望用户单击任何内容来刷新 GUI,这些值应该在它启动时就在那里。我已经阅读了几篇类似的帖子,但发现这些解决方案对我不起作用。
第一个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;
}
}
第二个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);
}
非常欢迎任何回复!如有需要,请向我询问有关代码的更多信息,我将确保尽快回复!
最佳解决方案:更改 WordScreen 的构造函数以接受两个感兴趣的字符串:
来自这里:
public void run(String Search, String Transl) {
WordScreen init = new WordScreen(); //initialise the second GUI;
init.setVisible(true);
activeword.setText(Search);
translation.setText(Transl);
}
对此:
public void run(String search, String transl) {
WordScreen init = new WordScreen(search, transl);
init.setVisible(true);
}
然后在 WordScreen 构造函数中,在需要的地方使用这些字符串:
public WordScreen(String search, String transl) {
JLabel someLabel = new JLabel(search);
JLabel otherLabel = new JLabel(transl);
// put them where needed
}
请注意,如果您没有发表像样的文章,我无法创建一个全面的答案 MRE
顺便说一句,您会想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。
我有一个简单的 GUI,它有一个等待用户输入内容的 jTextField。单击按钮后,程序:
- 读取输入,将其保存在字符串变量中;
- 打开一个新的 GUI(在单独的 class 文件中),其中包含一个空的 jLabel,并将 String 变量传递给它,将 jLabel 文本更改为它。
问题是无论我多么努力地尝试重新配置代码,添加 repaint()、revalidate() 等内容,第二个 GUI 中的 jLabel 仍然是空的。使用 System.out.println(jLabel.getText()) 显示文本值确实已更改,但未显示。我如何 "refresh" 这个 jLabel 才能显示我想要的内容?我知道我可以添加一个事件,但我不希望用户单击任何内容来刷新 GUI,这些值应该在它启动时就在那里。我已经阅读了几篇类似的帖子,但发现这些解决方案对我不起作用。
第一个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;
}
}
第二个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);
}
非常欢迎任何回复!如有需要,请向我询问有关代码的更多信息,我将确保尽快回复!
最佳解决方案:更改 WordScreen 的构造函数以接受两个感兴趣的字符串:
来自这里:
public void run(String Search, String Transl) {
WordScreen init = new WordScreen(); //initialise the second GUI;
init.setVisible(true);
activeword.setText(Search);
translation.setText(Transl);
}
对此:
public void run(String search, String transl) {
WordScreen init = new WordScreen(search, transl);
init.setVisible(true);
}
然后在 WordScreen 构造函数中,在需要的地方使用这些字符串:
public WordScreen(String search, String transl) {
JLabel someLabel = new JLabel(search);
JLabel otherLabel = new JLabel(transl);
// put them where needed
}
请注意,如果您没有发表像样的文章,我无法创建一个全面的答案 MRE
顺便说一句,您会想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。