Java JTabbedPane,更新其他选项卡JLabel值?

发布于 2024-09-15 14:13:51 字数 1364 浏览 6 评论 0原文

我有 2 个 JTabbedPane。我无法刷新数据。请帮忙,这是我的代码:

pane1:

//.. some codes...
// This is the ButtonListener
private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent event)
    {
      userInput = tf.getText(); // tf is JTextField
      //System.out.println("the input is "+ finalInput);
      pane2.updateData(userInput);
    }
} 

pane2:

public void updateData(String s){
    System.out.println("Update data function is called");
    labelUser.setFont(new Font("Arial", Font.BOLD, 30));
    labelUser.setText("Updated text here " + s);
}   

这是我的主类:

import java.awt.*;
import javax.swing.*;

public class Main {
public static Pane2 p2 = new Pane2();
    public static void main(String[] args) {

        JFrame f= new JFrame ("My Frame");
        f.setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE);

        JTabbedPane tp = new JTabbedPane();
        p2 = new Pane2();

        tp.addTab("Pane1", new PaneFirst(p2));
        tp.addTab("Pane2", new PaneSecond());

        f.add(tp);
        f.pack();
        f.setVisible(true);
   }
}

labelUser 永远不会更新,但我跟踪 updateData 函数,它被调用。为什么 labelUser 中的文本没有更新?

编辑:

labelUser”来自pane2.java类。

I have 2 JTabbedPane. I am unable to refresh the data. PLease help, here is my code:

pane1:

//.. some codes...
// This is the ButtonListener
private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent event)
    {
      userInput = tf.getText(); // tf is JTextField
      //System.out.println("the input is "+ finalInput);
      pane2.updateData(userInput);
    }
} 

pane2:

public void updateData(String s){
    System.out.println("Update data function is called");
    labelUser.setFont(new Font("Arial", Font.BOLD, 30));
    labelUser.setText("Updated text here " + s);
}   

Here is my main class:

import java.awt.*;
import javax.swing.*;

public class Main {
public static Pane2 p2 = new Pane2();
    public static void main(String[] args) {

        JFrame f= new JFrame ("My Frame");
        f.setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE);

        JTabbedPane tp = new JTabbedPane();
        p2 = new Pane2();

        tp.addTab("Pane1", new PaneFirst(p2));
        tp.addTab("Pane2", new PaneSecond());

        f.add(tp);
        f.pack();
        f.setVisible(true);
   }
}

The labelUser never updates, but I trace the updateData function, its being called. Why is the text in labelUser not being updated?

EDIT:

"labelUser" come from pane2.java class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雄赳赳气昂昂 2024-09-22 14:13:51

注意:显然这并没有解决问题。

要尝试的一件事是:

public void updateData(String s){
    System.out.println("Update data function is called");
    labelUser.setFont(new Font("Arial", Font.BOLD, 30));
    labelUser.setText("Updated text here " + s);
    repaint(); // add this line to tell your pane to repaint itself
}  

您的面板有可能没有重新粉刷。

Note: Apparently this didn't fix the problem.

One thing to try would be:

public void updateData(String s){
    System.out.println("Update data function is called");
    labelUser.setFont(new Font("Arial", Font.BOLD, 30));
    labelUser.setText("Updated text here " + s);
    repaint(); // add this line to tell your pane to repaint itself
}  

There is a chance that your panel is just not getting repainted.

伏妖词 2024-09-22 14:13:51

可能是一个拼写错误,但在 actionPerformed() 中,您将文本字段的内容存储在 userInput 中,但使用 finalInput 来更新 pane2。

Might be a typo but - in actionPerformed() you store the content of the textfield in userInput but use finalInput to update pane2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文