我有一个简单的GUI,它有一个jTextField,可以等待用户输入一些东西。单击按钮后,程序:
问题是,无论我多么努力地尝试重新配置代码,添加诸如重新绘制()、重新验证()等内容,第二个图形用户界面中的jLabel仍然是空的。使用System.out.println(jLabel.getText())显示文本值确实发生了更改,但没有显示。我如何“刷新”这个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的代码(活动词和翻译是给我带来麻烦的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);
}欢迎任何回复!如有必要,请向我询问更多有关代码的信息,我会尽快回复!
发布于 2020-03-14 14:25:05
最佳解决方案:更改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命名约定。变量名都应以小写字母开头,而类名应以大写字母开头。学习并遵循这一点将使我们更好地理解您的代码,并将允许您更好地理解他人的代码。
https://stackoverflow.com/questions/60683695
复制相似问题