我无法从另一个班级设置JLabel的文本

时间:2014-08-02 13:30:03

标签: java swing

我有一个JLabel,我需要从另一个类中设置文本。我们只需称之为JLabel soupLabel。

soupLabel位于JPanel类(类扩展为JPanel)中,称为SoupPage。 SoupPage实际上是mainGUI类的卡片。

我有另一个名为Confirmation Class的类(类扩展了JFrame)。按下Soup Class中的按钮后,该类会弹出。

我尝试在确认类中设置文本soupLabel(在Soup类中)但无效。

MainGUI

public class mainGUI extends JFrame{
    private CardLayout card;

    public mainGUI(){
       card = new CardLayout();
       setLayout(card);
    }

    private void createSoupPage(){
    SoupPage sp = new SoupPage(this);
    add(sp, "Soup Page");
    }
}

已编辑:弹出确认的SoupPage

public class SoupPage extends JPanel{
    mainGUI gui;
    public JLabel soupLabel;
    public JButton cfmBtn;

    public SoupPage(mainGUI gui){
        this.gui = gui;

        soupLabel = new JLabel("blabla");

        cfmBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Confirmation cfmPopUp = new Confirmation();
            }
        });
    }
}

确认

public class Confirmation extends JDialog{
    JButton clrBtn;

    public Confirmation(){
         SoupPage sp = new SoupPage();

         clrBtn = new JButton("Clear");
         clrBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            sp.soupLabel.setText("some other text bla bla");
            }
         });
    }
}

这是它不起作用的部分。

2 个答案:

答案 0 :(得分:1)

这永远不会奏效。这是因为您正在尝试设置从不添加到mainGUI的不同SoupPage对象的soupLabel文本。你需要做的是设置在mainGUI类中声明的SoupPage类实例的方块的文本。你应该做什么?
1. Tou永远不要使用JFrame弹出窗口。将其更改为JDialog。当任务栏中出现两个图标时,看起来很奇怪,也可能存在焦点问题。
2.现在接下来解决你的实际问题在mainGUI中声明私有方法之外的SoupPage实例。然后像这样重新定义您的确认类

  public class Confirmation extends JDialog{
    JButton clrBtn;

    public Confirmation(SoupPage sp){

         clrBtn = new JButton("Clear");
         clrBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            sp.soupLabel.setText("some other text bla bla");
            }
         });
    }
}

现在从mainGUi调用Confirmation时,只传递在那里声明的实例。 的更新
尝试使用此代码,我已经修改了所有三个类并进行了测试,这些类将根据您的需要完美地运行

class mainGUI extends JFrame{
    private CardLayout card;
    private SoupPage sp;

    mainGUI(){
       card = new CardLayout();
       setLayout(card);
    }

    private void createSoupPage(){
      sp = new SoupPage(this);
      add(sp,"Soup Page");
    }

    public void setSoupPageText(String text){
      sp.soupLabel.setText(text);
    }


}

class SoupPage extends JPanel{
    mainGUI gui;
    JLabel soupLabel;
    JButton cfmBtn;

    SoupPage(mainGUI gui){
        this.gui = gui;

        soupLabel = new JLabel("blabla");

        cfmBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Confirmation cfmPopUp = new Confirmation(gui);
            }
        });
    }
}



class Confirmation extends JDialog{
    JButton clrBtn;

    Confirmation(mainGUI gui){

         clrBtn = new JButton("Clear");
         clrBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            gui.setSoupPageText("some other text bla bla");
            }
         });
    }
}

答案 1 :(得分:0)

它无法正常工作,因为您要在尚未添加到mainGUI的新SoupPage实例上设置动作侦听器。您必须将mainGUI创建的SoupPage实例传递给Confirmation的构造函数:

public Confirmation(SoupPage sp){
     clrBtn = new JButton("Clear");
     clrBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        sp.soupLabel.setText("some other text bla bla");
        }
     });
}