RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 806223
Accepted
Rick Petrev
Rick Petrev
Asked:2020-03-29 04:57:38 +0000 UTC2020-03-29 04:57:38 +0000 UTC 2020-03-29 04:57:38 +0000 UTC

JPanel 继承

  • 772

你好,为什么不能继承面板?

 settingButt.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            PanelSett panel2 = new PanelSett();
            if(onClickSett == false) {
                panel.add(panel2);
                System.out.println("1");
                panel.repaint();
                onClickSett = true;
            }
            else{
                panel.remove(panel2);
                panel.revalidate();
                panel.repaint();
                onClickSett = false;
            }
        }
    });

1 在控制台中成功显示,但是面板没有出现

PanelSett 类

public class PanelSett extends JPanel{
public void paintComponent(Graphics g){
    this.setSize(100, 100);
    this.setBackground(Color.red);
    this.setVisible(true);
    System.out.println("2");
}} // В консоле даже 2 нет
java
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    ezhov_da
    2020-03-29T13:06:49Z2020-03-29T13:06:49Z

    根据您的示例查看我的示例,它在单击按钮时显示和隐藏面板:

        import javax.swing.*;
        import java.awt.*;
    
        public class PanelTest {
            public boolean onClickSett;
    
            public static void main(String[] args) {
                PanelTest panelTest = new PanelTest();
                panelTest.start();
            }
    
            private void start() {
                SwingUtilities.invokeLater(() -> {
                    PanelSett panel2 = new PanelSett();
                    JPanel panel = new JPanel(new BorderLayout());
                    JFrame frame = new JFrame();
    
                    JButton button = new JButton("Push me");
                    panel.add(button, BorderLayout.NORTH);
    
                    frame.add(panel);
                    button.addActionListener(e -> {
                        if (onClickSett == false) {
                            panel.add(panel2, BorderLayout.CENTER);
                            panel.revalidate();
                            panel.repaint();
                            onClickSett = true;
                        } else {
                            panel.remove(panel2);
                            panel.revalidate();
                            panel.repaint();
                            onClickSett = false;
                        }
                    });
    
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
    
                });
            }
        }
    
        class PanelSett extends JPanel {
            public PanelSett() {
                setBorder(BorderFactory.createLineBorder(Color.RED));
                this.setBackground(Color.red);
                this.setVisible(true);
            }
        }
    

    根据您的代码:
    paintComponent - 不被调用,您不应该在其中设置属性,它们在初始化期间设置一次,除了背景,但最好通过单独的方法更改它。大小还取决于布局管理器,但在大多数情况下不使用。

    另外,每次点击都会创建一个新面板,因此旧面板不会被删除,因为它不等于新面板,这就是 remove() 时调用的方法。

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5