JPanel 与 JTextField 或 JLabel 未更新

发布于 2025-01-04 19:18:35 字数 2160 浏览 2 评论 0原文

我试图找到答案,但我找不到答案。我对java相当陌生。我有 4 个类(1 个主类带有 JFrame 和 3 个 JPanel)。 主类创建 JFrame 并向其中添加 3 个面板。 我想要完成的是从另一个面板(panelB)中的 ActionEvent 更新 1 个面板(panelA)中的 JLabel 或 JTextField。 panelB 中的 ActionEvent 运行 panelA 中的一个方法,该方法运行 setText() 方法和 repaint() 方法。我无法使用新文本更新 JLabel 或 JTextField。

这是我的代码:

App.java

public class App {
    public static void main(String[] args) {
        JFrame nameFrame = new JFrame("Name Form");
        nameFrame.setLayout(new BorderLayout());
        nameFrame.setSize(300,150);
        nameFrame.setLocationRelativeTo(null);
        nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MiddlePanel middlePanel = new MiddlePanel();
        nameFrame.add(middlePanel, BorderLayout.CENTER);
        BottomPanel bottomPanel = new BottomPanel();
        nameFrame.add(bottomPanel, BorderLayout.SOUTH);
        nameFrame.setVisible(true);
    }
}

BottomPanel.java

import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BottomPanel extends JPanel {
    private JLabel welcomeLabel = new JLabel("Old Text");

    BottomPanel() {
        super(new FlowLayout());
        add(welcomeLabel);
    }
    public void setText(String text) {
        welcomeLabel.setText(text);
        repaint();
    }
}

MiddlePanel.java

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.Graphics;

public class MiddlePanel extends JPanel {
    MiddlePanel() {
        super(new BorderLayout());

        JButton okButton = new JButton("OK");
        okButton.setSize(20, 20);
        OKButtonListener okButtonListener = new OKButtonListener();
        okButton.addActionListener(okButtonListener);

        add(okButton, BorderLayout.EAST);
    }
    class OKButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            BottomPanel bottomPanel = new BottomPanel();
            bottomPanel.setText("New Text");
        }
    }
}

感谢您的帮助。

I have tried to find the answer but I'm coming up short. I am fairly new to java. I have 4 classes (1 main with a JFrame and 3 JPanels).
The main class creates the JFrame and adds the 3 panels to it.
What I am trying to accomplish is update a JLabel or JTextField in 1 panel (panelA) from an ActionEvent in another panel (panelB).
The ActionEvent in panelB runs a method in panelA that runs the setText() method and the repaint() method. I cannot get the JLabel or JTextField to update with the new text.

Here is my code:

App.java

public class App {
    public static void main(String[] args) {
        JFrame nameFrame = new JFrame("Name Form");
        nameFrame.setLayout(new BorderLayout());
        nameFrame.setSize(300,150);
        nameFrame.setLocationRelativeTo(null);
        nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MiddlePanel middlePanel = new MiddlePanel();
        nameFrame.add(middlePanel, BorderLayout.CENTER);
        BottomPanel bottomPanel = new BottomPanel();
        nameFrame.add(bottomPanel, BorderLayout.SOUTH);
        nameFrame.setVisible(true);
    }
}

BottomPanel.java

import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BottomPanel extends JPanel {
    private JLabel welcomeLabel = new JLabel("Old Text");

    BottomPanel() {
        super(new FlowLayout());
        add(welcomeLabel);
    }
    public void setText(String text) {
        welcomeLabel.setText(text);
        repaint();
    }
}

MiddlePanel.java

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.Graphics;

public class MiddlePanel extends JPanel {
    MiddlePanel() {
        super(new BorderLayout());

        JButton okButton = new JButton("OK");
        okButton.setSize(20, 20);
        OKButtonListener okButtonListener = new OKButtonListener();
        okButton.addActionListener(okButtonListener);

        add(okButton, BorderLayout.EAST);
    }
    class OKButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            BottomPanel bottomPanel = new BottomPanel();
            bottomPanel.setText("New Text");
        }
    }
}

Thanks for any help.

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

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

发布评论

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

评论(2

蓝海似她心 2025-01-11 19:18:36

在 OKButtonListener 中,您创建了一个新的 BottomPanel 实例,它与添加到 JFrame 中的 BottomPanel 无关。
您将需要在 JFrame 中添加的 BottomPanel 的实际引用。

In your OKButtonListener you create a new instance of BottomPanel which has nothing to do with the BottomPanel you added to your JFrame.
You will need the actual reference of the BottomPanel that you added in your JFrame.

山色无中 2025-01-11 19:18:36

d1rk 成功了。这是实现该效果的一种方法。

最终结果

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class App {
    public static void main(String[] args) {
        JFrame nameFrame = new JFrame("Name Form");
        nameFrame.setLayout(new BorderLayout());
        nameFrame.setSize(300,150);
        nameFrame.setLocationRelativeTo(null);
        nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        BottomPanel bottomPanel = new BottomPanel();

        MiddlePanel middlePanel = new MiddlePanel(bottomPanel);
        nameFrame.add(middlePanel, BorderLayout.CENTER);
        nameFrame.add(bottomPanel, BorderLayout.SOUTH);
        nameFrame.setVisible(true);
    }
}

class BottomPanel extends JPanel {
    private JLabel welcomeLabel = new JLabel("Old Text");

    BottomPanel() {
        super(new FlowLayout());
        add(welcomeLabel);
    }
    public void setText(String text) {
        welcomeLabel.setText(text);
        repaint();
    }
}

class MiddlePanel extends JPanel {

    private BottomPanel bottomPanel;

    MiddlePanel(BottomPanel bottomPanel) {
        super(new BorderLayout());

        this.bottomPanel = bottomPanel;

        JButton okButton = new JButton("OK");
        okButton.setSize(20, 20);
        OKButtonListener okButtonListener = new OKButtonListener();
        okButton.addActionListener(okButtonListener);

        add(okButton, BorderLayout.EAST);
    }
    class OKButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //BottomPanel bottomPanel = new BottomPanel();
            bottomPanel.setText("New Text");
        }
    }
}

d1rk nailed it. Here is one way to achieve that effect.

End Result

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class App {
    public static void main(String[] args) {
        JFrame nameFrame = new JFrame("Name Form");
        nameFrame.setLayout(new BorderLayout());
        nameFrame.setSize(300,150);
        nameFrame.setLocationRelativeTo(null);
        nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        BottomPanel bottomPanel = new BottomPanel();

        MiddlePanel middlePanel = new MiddlePanel(bottomPanel);
        nameFrame.add(middlePanel, BorderLayout.CENTER);
        nameFrame.add(bottomPanel, BorderLayout.SOUTH);
        nameFrame.setVisible(true);
    }
}

class BottomPanel extends JPanel {
    private JLabel welcomeLabel = new JLabel("Old Text");

    BottomPanel() {
        super(new FlowLayout());
        add(welcomeLabel);
    }
    public void setText(String text) {
        welcomeLabel.setText(text);
        repaint();
    }
}

class MiddlePanel extends JPanel {

    private BottomPanel bottomPanel;

    MiddlePanel(BottomPanel bottomPanel) {
        super(new BorderLayout());

        this.bottomPanel = bottomPanel;

        JButton okButton = new JButton("OK");
        okButton.setSize(20, 20);
        OKButtonListener okButtonListener = new OKButtonListener();
        okButton.addActionListener(okButtonListener);

        add(okButton, BorderLayout.EAST);
    }
    class OKButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //BottomPanel bottomPanel = new BottomPanel();
            bottomPanel.setText("New Text");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文