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);
}
});
}
}
这应该足以重现错误。
白衣染霜花
相关分类