摆动窗口间歇性为空白

时间:2018-09-26 22:07:26

标签: java swing

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 个答案:

答案 0 :(得分:0)

好吧,好像没人愿意回答这个问题,所以我会继续进行,让大家知道解决这个问题的原因。添加@Madprogrammer

后,我在添加所有视图的最后都打电话给setVisible