更新jLabel

时间:2020-03-14 14:17:44

标签: java swing refresh jlabel

我有一个简单的GUI,其中包含一个jTextField,它等待用户放入内容。单击按钮后,该程序:

  1. 读取输入,并将其保存在String变量中;
  2. 打开一个新的GUI(位于单独的类文件中),其中包含一个空的jLabel,并将String变量传递给它,从而将jLabel文本更改为它。

问题是,无论我多么努力地重新配置代码,添加诸如repaint(),revalidate()等内容,第二个GUI中的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的代码(活动单词和翻译是给我带来麻烦的jLabel。)

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

任何答复都非常欢迎!如有必要,请询问我有关代码的更多信息,我将确保尽快答复!

1 个答案:

答案 0 :(得分:1)

最佳解决方案:更改WordScreen的构造函数以接受两个感兴趣的字符串:

从此:

Container(
        height: double.infinity,
        width: double.infinity,
        padding: const EdgeInsets.only(top: 15, bottom: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            Container(
              width: (mediaQuery.size.width * 53 / 100) - mediaQuery.size.width * 0.14,
              padding: EdgeInsets.only(right: 15),
              height: 15,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    '23. feb 2020',
                    style: TextStyle(
                      fontSize: 11,
                      fontFamily: 'Red Hat Text',
                      fontWeight: FontWeight.bold,
                      color: AppColors.lightGrey,
                    ),
                  ),
                  Text(
                    '21.00',
                    style: TextStyle(
                      fontSize: 11,
                      fontFamily: 'Red Hat Text',
                      fontWeight: FontWeight.bold,
                      color: AppColors.lightGrey,
                    ),
                  )
                ],
              ),
            ),
            SizedBox(
              height: 5,
            ),
            Container(
              width: (mediaQuery.size.width * 57 / 100) - mediaQuery.size.width * 0.14,
              padding: EdgeInsets.only(right: 15),
              height: 20,
              child: FittedBox(
                alignment: Alignment.centerLeft,
                fit: BoxFit.scaleDown,
                child: Text(
                  'item title name',
                  style: TextStyle(
                      fontSize: 18,
                      fontFamily: 'Red Hat Text',
                      fontWeight: FontWeight.bold,
                      color: AppColors.white),
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            Container(
              width: (mediaQuery.size.width * 61 / 100) - mediaQuery.size.width * 0.14,
              padding: EdgeInsets.only(right: 15),
              height: 15,
              child: Text(
                'address',
                style: TextStyle(
                  fontSize: 11,
                  fontFamily: 'Red Hat Text',
                  fontWeight: FontWeight.bold,
                  color: AppColors.lightGrey,
                ),
              ),
            ),
            Expanded(
              child: Container(),
            ),
            Container(
              width: (mediaQuery.size.width * 3.7 / 5) - mediaQuery.size.width * 0.14,
              padding: EdgeInsets.only(right: 15),
              height: 32,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    'GOING',
                    style: TextStyle(
                      fontSize: 24,
                      fontFamily: 'Red Hat Text',
                      fontWeight: FontWeight.bold,
                      color: AppColors.green,
                    ),
                  ),
                  Container(
                    alignment: Alignment.bottomRight,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Icon(
                          AppIcons.guests,
                          size: 15,
                          color: AppColors.white,
                        ),
                        SizedBox(
                          width: 15,
                        ),
                        Text(
                          '24',
                          style: TextStyle(
                              fontSize: 20,
                              fontFamily: 'Red Hat Text',
                              fontWeight: FontWeight.bold,
                              color: AppColors.white),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),

对此:

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(search, transl); 
    init.setVisible(true);
}

请注意,如果您没有发布体面的MRE

,就无法创建全面的答案

顺便说一句,您将要学习和使用Java naming conventions。变量名都应以小写字母开头,而类名应以大写字母开头。学习并遵循此规则将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。