Java 更新jLabel
我有一个简单的GUI,它有一个jTextField,等待用户输入一些东西。单击按钮后,程序将:Java 更新jLabel,java,swing,refresh,jlabel,Java,Swing,Refresh,Jlabel,我有一个简单的GUI,它有一个jTextField,等待用户输入一些东西。单击按钮后,程序将: 读取输入,将其保存在字符串变量中 打开一个新的GUI(位于单独的类文件中),其中包含一个空的jLabel,并将字符串变量传递给它,将jLabel文本更改为它 问题是,无论我多么努力地重新配置代码,添加诸如repaint()、revalidate()等内容,第二个GUI中的jLabel仍然是空的。使用System.out.println(jLabel.getText())可以发现文本值确实已更改,但未显
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;
}
}
欢迎任何回复!如有必要,请询问我有关代码的更多信息,我将确保尽快回复 最佳解决方案:更改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
}
请注意,如果你不发表一篇像样的文章,我无法给出一个全面的答案
作为旁白,你需要学习和使用。变量名都应该以小写字母开头,而类名应该以大写字母开头。了解并遵循这一点将使我们更好地理解代码,并允许您更好地理解他人的代码。请考虑创建一个非常小的、自包含的、功能完备的程序(A-请阅读链接)来演示您的问题和代码,编译和运行的代码,并在问题中作为代码格式文本发布。作为旁白,你需要学习和使用。变量名都应该以小写字母开头,而类名应该以大写字母开头。遵循这一点可以让我们更好地理解您的代码。最好的解决方案可能是更改WordScreen的构造函数以接受两个感兴趣的字符串:
WordScreen init=newWordScreen(Search,Transl)代码>,然后将这些参数传递到需要的位置
public WordScreen(String search, String transl) {
JLabel someLabel = new JLabel(search);
JLabel otherLabel = new JLabel(transl);
// put them where needed
}