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);
}
});
}
}
这应该足以再现错误。