关于Swing中JFrame或JPanel重写paint()方法后再在JFrame或JPanel中添加其他组件出现按钮等组件消失不见只有当鼠标扫过时才会出现的问题
  TEZNKK3IfmPf 2023年11月13日 22 0

异常

关于Swing中JFrame或JPanel重写paint()方法后,再在JFrame或JPanel中添加其他组件出现按钮等组件消失不见只有当鼠标扫过时才会出现的问题。

场景重现:

public class TestFrame extends JFrame {

    public TestFrame() {
        setTitle("测试");
        setLocation(120, 120);
        setSize(300, 300);

        setLayout(new BorderLayout());
        JPanel buttonPanel = new JPanel();
        JButton abutton, bbutton;
        abutton = new JButton("a");
        buttonPanel.add(abutton);
        bbutton = new JButton("b");
        buttonPanel.add(bbutton);
        JButton cbutton = new JButton("c");
        buttonPanel.add(cbutton);
        JButton dbutton = new JButton("d");
        buttonPanel.add(dbutton);
        setContentPane(buttonPanel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new TestFrame();
    }

    @Override
    public void paint(Graphics g) {
        g.drawString("重写paint方法", 50, 250);
    }
}

运行的结果如下,在JFrame中添加了四个按钮,但是只显示了一个按钮,其他三个按钮呢?

关于Swing中JFrame或JPanel重写paint()方法后再在JFrame或JPanel中添加其他组件出现按钮等组件消失不见只有当鼠标扫过时才会出现的问题

当把鼠标放到按钮的位置后,这些按钮才会出现。

关于Swing中JFrame或JPanel重写paint()方法后再在JFrame或JPanel中添加其他组件出现按钮等组件消失不见只有当鼠标扫过时才会出现的问题

原因

可能是重写paint()方法后覆盖了添加了组件的界面。

解决1

在重写paint()方法上面调用

super.paint(g);

关于Swing中JFrame或JPanel重写paint()方法后再在JFrame或JPanel中添加其他组件出现按钮等组件消失不见只有当鼠标扫过时才会出现的问题

能够成功显示

关于Swing中JFrame或JPanel重写paint()方法后再在JFrame或JPanel中添加其他组件出现按钮等组件消失不见只有当鼠标扫过时才会出现的问题

解决2

就是不要在JPanel或JFrame中同时进行paint()重写和添加组件,分别使用两个JPanel,其中一个JPanel用来添加组件,另一个JPanel用来重写paint()方法,然后将两个JPanel添加到JFrame。这样就可以避免这种情况的发生。示例代码如下:

public class TestFrame extends JFrame {
    // 继承JFrame后只添加组件,进行逻辑功能处理,不重写paint()方法绘制
    public TestFrame() {
        setTitle("测试");
        setLocation(120, 120);
        setSize(300, 300);

        JPanel buttonPanel = new JPanel();
        JButton abutton, bbutton;
        abutton = new JButton("a");
        buttonPanel.add(abutton);
        bbutton = new JButton("b");
        buttonPanel.add(bbutton);
        JButton cbutton = new JButton("c");
        buttonPanel.add(cbutton);
        JButton dbutton = new JButton("d");
        buttonPanel.add(dbutton);

        Container contentPane = getContentPane();
        contentPane.add(buttonPanel, BorderLayout.NORTH);
        contentPane.add(new MyPanel(), BorderLayout.CENTER);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new TestFrame();
    }
}

// JPanel只重写paint()方法绘制页面,不添加组件
class MyPanel extends JPanel {
    @Override
    public void paint(Graphics g) {
        g.drawString("重写paint方法", 50, 150);
    }
}

关于Swing中JFrame或JPanel重写paint()方法后再在JFrame或JPanel中添加其他组件出现按钮等组件消失不见只有当鼠标扫过时才会出现的问题

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

TEZNKK3IfmPf