代码之家  ›  专栏  ›  技术社区  ›  SamHoque

摆动窗口间歇性空白

  •  0
  • SamHoque  · 技术社区  · 7 年前

    Swing GUI断断续续地空白这里是一张图片,它看起来像是我做了一个循环来执行代码来显示GUI 5次,最后一个是空白我正在使用InvokeLater来执行GUI,我尝试了没有它,它仍然是相同的结果。这些代码足以让GUI启动并运行

        public class Main_UI extends JFrame {
    
        public Main_UI() {
            JPanel gui = new JPanel(new BorderLayout());
            setUndecorated(true);
            setLocationRelativeTo(null);
            setVisible(true);
            setSize(329, 256);
            setResizable(false);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            ThemeUtils.setTopBar(gui, this);
        }
    }
    

    这是你的密码 ThemeUtils.setTopBar

        public static void setTopBar(JPanel jPanel, JFrame frame) {
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton exitButton = new JButton("X");
        JButton minimizeButton = new JButton("-");
        topPanel.add(minimizeButton);
        topPanel.add(exitButton);
        jPanel.add(BorderLayout.NORTH, topPanel);
        new DragFeatures(topPanel, frame).setupDragFeatures();
    }
    

    最后呢 DragFeatures 类别代码

    public class DragFeatures {
        private Point compCoords;
        private JPanel panel;
        private JFrame frame;
    
        public DragFeatures(JPanel panel, JFrame frame) {
            this.panel = panel;
            this.frame = frame;
        }
    
        public void setupDragFeatures() {
            compCoords = null;
            panel.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent e) {
                    compCoords = null;
                }
    
                public void mousePressed(MouseEvent e) {
                    compCoords = e.getPoint();
                }
            });
            panel.addMouseMotionListener(new MouseMotionListener() {
                public void mouseMoved(MouseEvent e) {
                }
    
                public void mouseDragged(MouseEvent e) {
                    Point currCoords = e.getLocationOnScreen();
                    frame.setLocation(currCoords.x - compCoords.x, currCoords.y - compCoords.y);
                }
            });
        }
    }
    

    这应该足以再现错误。

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  0
  •   SamHoque    7 年前

    好吧,听着,好像没人想回答这个问题,所以我会继续,让你们知道是什么解决了这个问题。我打电话来了 setVisible 在添加我的所有视图之后,感谢@Madprogrammer