【问题标题】:I can't set text of JLabel from another class我无法从另一个类中设置 JLabel 的文本
【发布时间】:2014-08-02 13:30:03
【问题描述】:

我有一个 JLabel,我需要从另一个类中设置它。我们就叫这个 JLabel 汤标签吧。

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

我有另一个名为 Confirmation Class 的类(类扩展了 JFrame)。这个类是在汤类中按下按钮后弹出的。

我尝试在确认类中设置文本soupLabel(在汤类中)但无济于事。

主界面

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");
            }
         });
    }
}

这是它不起作用的部分。

【问题讨论】:

  • 在您的示例中,文本已经是 "blabla" 并再次按下按钮将其设置为 "blabla"。你期待什么效果?
  • 嗯,我的错。这不是布拉布拉。我只是想设置另一个文本。
  • 但是您每次使用不同的 SoupPage 时,您认为这会如何影响所有其他实例?
  • 如何从现有实例中传递该实例?

标签: java swing


【解决方案1】:

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

  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");
            }
         });
    }
}

【讨论】:

  • 是的,它运行良好。不过 Eclipse 要求我在实例前面放置一个 final 修饰符。
【解决方案2】:

它不起作用,因为您在尚未添加到 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");
        }
     });
}

【讨论】:

  • 其实我已经试过了。现在的问题是我在尝试创建弹出窗口时出错
  • 需要将SoupPage实例传入构造函数:new Confirmation(sp);
  • 问题在于构建您的程序,以便您能够做到这一点。
  • Confirmation popUp = new Confirmation() 在 SoupPage 类中。
  • 确认弹出=新确认(此);试过也没用-.-"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多