【问题标题】:Updating JLabel when JButton Clicked on from another class从另一个类单击 JButton 时更新 JLabel
【发布时间】:2020-11-11 15:02:39
【问题描述】:

我想在单击 JButton 时更新 JLabel 的文本。 问题是他们在不同的班级。

我尽可能地最小化了我的代码,所以这段代码并不包含我实际拥有的所有代码。
下面是第一个面板的代码,其中包含一个将触发文本更新方法的按钮。

public class CultureCategorySelectPanel extends JPanel {


    public CultureCategorySelectPanel(JFrame mf) {
        setVisible(true);
        setLayout(null);
        setSize(1000, 600);

        JButton bookCategoryBtn = new JButton("Book");

        // When Clicking on JButton
        bookCategoryBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                mf.getContentPane().removeAll(); 

                CultureListPanel myPanel = new CultureListPanel(mf);
                mf.getContentPane().add(myPanel);

                String text = “This text will be shown”;
                myPanel.updateLabel(text);
                
                mf.setVisible(true);
                mf.repaint();
            }
        });
      }
    }

下面是第二个面板的代码,其中有一个 textLabel 将被更新。

private JLabel plzSelectLabel;

public CultureListPanel(JFrame mf) {
        setVisible(true);
        setLayout(null);
        setSize(1000, 600);

JLabel plzSelectLabel = new JLabel(“This text soon be changed”);
        plzSelectLabel.setHorizontalAlignment(SwingConstants.CENTER);
        plzSelectLabel.setBounds(138, 89, 367, 34);
        rightPanel.add(plzSelectLabel);
// 'rightPanel' is on the top of CultureListPanel, I put plzSelectLabel on the panel called 'rightPanel'


}

public void updateLabel(String text) { 
        plzSelectLabel.setText(text);

    }

运行时,com.kh.mini_Project.view.CultureListPanel.updateLabel(CultureListPanel.java:169) 的线程“AWT-EventQueue-0”java.lang.NullPointerException 出现异常错误。
我也尝试过使用 getter/setter,但它显示了同样的错误。

编辑
下面的代码是MainFrame。


import javax.swing.JFrame;

public class MainFrame extends JFrame{
   public MainFrame() {
      this.setTitle("--");
      this.setSize(1000, 600);
      this.setLocationRelativeTo(null);
      this.setResizable(false);

     this.getContentPane().add(new WelcomPage(this));
      
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

编辑
Driver 类在这里。

public class Run {

    public static void main(String[] args) {
        new MainFrame();
   }
}

【问题讨论】:

  • CultureListPanel.java的第169行是>> plzSelectLabel.setText(text);
  • 是的。错误在 plzSelectLabel 上显示 NullPointerException。
  • 请注意minimal reproducible example中的RCultureCategorySelectPanel 在哪里初始化? rightPanel 在哪里初始化?为什么CultureListPanel 构造一个JFrame (JFrame mf = new JFrame()) 并且还在构造函数中接收到一个JFrame 的引用?
  • 查看可运行的example
  • 我很困惑,因为我认为 plzSelectLabel 没有问题。当我运行该应用程序时,每个组件都运行良好,直到我点击 bookCategoryBtn 并找到没有更改的标签。我只是想知道为什么 plzSelectLabel 保持为空,尽管我通过方法发送文本。

标签: java swing


【解决方案1】:

您的构造函数定义了一个变量plzSelectLabel,它具有本地(构造函数)范围。因此,类级别变量(具有相同名称)将不会被初始化,因此是 NPE。

提示:阅读一些关于变量作用域的信息 ;-)

【讨论】:

  • 我在第二个面板的字段中声明了private JLabel plzSelectLabel;,它仍然是一个局部变量吗?
  • 你的构造函数 JLabel plzSelectLabel 也是如此...此语句不初始化类变量,仅初始化构造函数中声明的变量。
  • 我去掉了方法中的JLabel,在字段区域同时声明和初始化,就可以了。谢谢。现在我可以睡觉了,这里是凌晨 1 点 T.T
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
  • 2013-07-09
  • 1970-01-01
相关资源
最近更新 更多