【问题标题】:how to modify a JFrame by another JFrame?如何通过另一个 JFrame 修改一个 JFrame?
【发布时间】:2014-12-22 14:13:38
【问题描述】:

我有两个 JFrame A 和 B。B 有一个 textArea。我想在 B 中的 TextArea 中添加一些文本 单击 A 中的按钮。这是我的代码:

JButton btn = new JButton("Text");
btnButton_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        b.textArea.setText("Button clicked");
    }
});
btnButton_1.setBounds(10, 45, 89, 23);
frmA.getContentPane().add(btnButton_1);
  • b 是 B 类的对象
  • textArea 是 B 类中的 JTextArea 变量名

【问题讨论】:

  • 在向 JTextArea 添加文本后尝试在 b 中调用 repaint()
  • 不要重绘任何东西,以获得更好的帮助,尽快发布一个简短、可运行、可编译的 SSCCE/MCVE
  • 你可以使用 b.textArea.append("your text");
  • 你的 textArea 是公开的吗?
  • 你能解释一下你的答案吗

标签: java swing button jframe textarea


【解决方案1】:

使用组件的 repaint() 方法。

JButton btn = new JButton("Text");
        btnButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.textArea.setText("Button clicked");
                b.frameB.repaint();
            }
        });
        btnButton_1.setBounds(10, 45, 89, 23);
        frmA.getContentPane().add(btnButton_1);

【讨论】:

  • 什么是 repaint() 方法。按 (.) 时不显示?
  • @user3417593 - 请参阅我更新的帖子。你应该重新粉刷框架本身。
  • 我重新粉刷了框架,但同样的事情不起作用。我还尝试重新绘制 textArea。再次不起作用。你试过这个吗?
  • @user3417593 - 你能发布两个java代码吗?但不仅仅是要点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 2014-03-12
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多