Java JTabbedPane,更新其他选项卡JLabel值?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:显然这并没有解决问题。
要尝试的一件事是:
您的面板有可能没有重新粉刷。
Note: Apparently this didn't fix the problem.
One thing to try would be:
There is a chance that your panel is just not getting repainted.
可能是一个拼写错误,但在
actionPerformed()
中,您将文本字段的内容存储在userInput
中,但使用finalInput
来更新 pane2。Might be a typo but - in
actionPerformed()
you store the content of the textfield inuserInput
but usefinalInput
to update pane2.