更新面板

发布于 2024-08-24 01:13:27 字数 87 浏览 8 评论 0原文

我的框架上有一个面板。通过单击一个按钮,我想删除旧面板并创建另一个面板并将该面板添加到我的框架中。(我也使用 netbeans) 你能帮我一下我该怎么做吗?谢谢

I have a panel on my frame .and by clicking on a button I want to delete the old panel and make the other panel and add that panel to my frame.(also I use netbeans)
would you please help me that how can i do that?thanks

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

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

发布评论

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

评论(2

慕烟庭风 2024-08-31 01:13:27
JFrame frame = new JFrame();
final JPanel origPanel = new JPanel();
frame.add(origPanel, BorderLayout.CENTER);

MouseListener ml = new MouseAdapter() {
  public void mouseClicked(MouseEvent evt) {
    // Mouse clicked on panel so remove existing panel and add a new one.
    frame.remove(origPanel);
    frame.add(createNewPanel(), BorderLayout.CENTER);

    // Revalidate frame to cause it to layout the new panel correctly.
    frame.revalidate();

    // Stop listening to origPanel (prevent dangling reference).
    origPanel.removeMouseListener(this);
  }
}

origPanel.addMouseListener(ml);
JFrame frame = new JFrame();
final JPanel origPanel = new JPanel();
frame.add(origPanel, BorderLayout.CENTER);

MouseListener ml = new MouseAdapter() {
  public void mouseClicked(MouseEvent evt) {
    // Mouse clicked on panel so remove existing panel and add a new one.
    frame.remove(origPanel);
    frame.add(createNewPanel(), BorderLayout.CENTER);

    // Revalidate frame to cause it to layout the new panel correctly.
    frame.revalidate();

    // Stop listening to origPanel (prevent dangling reference).
    origPanel.removeMouseListener(this);
  }
}

origPanel.addMouseListener(ml);
就像说晚安 2024-08-31 01:13:27

这边走:

    final JFrame frame = new JFrame();
    frame.setSize(200, 200);

    final JPanel panelA = new JPanel();
    final JPanel panelB = new JPanel();
    JButton button = new JButton("Switch");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.remove(panelA);
            frame.add(panelB);
            frame.show();
        }
    });
    JLabel label = new JLabel("This is panel B. Panel A is gone!");
    panelB.add(label);
    panelA.add(button);
    frame.add(panelB);
    frame.add(panelA);
    frame.show();

This way:

    final JFrame frame = new JFrame();
    frame.setSize(200, 200);

    final JPanel panelA = new JPanel();
    final JPanel panelB = new JPanel();
    JButton button = new JButton("Switch");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.remove(panelA);
            frame.add(panelB);
            frame.show();
        }
    });
    JLabel label = new JLabel("This is panel B. Panel A is gone!");
    panelB.add(label);
    panelA.add(button);
    frame.add(panelB);
    frame.add(panelA);
    frame.show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文