Quick picture to show what is happening
如上图所示,JSpinner出现了两次。第(0,0)点的第一次出现不应该是可选的,可编辑的或没有微调器按钮可用。
这里奇怪的是每个其他组件都没有问题。只有jspinner。我正在使用Java 7并在Netbeans中开发(不是gui开发人员工具包)。这是java 7的错误吗?如果没有,我可以尝试在我指定的区域中仅使我的JSpinner油漆一次?
用于说明问题的代码:
我将它添加到JPanel的子类中,如下所示:
public class MyCustomGUI extends JPanel {
private JSpinner entrySpinner;
public MyCustomGUI () {
super(null);
this.setDoubleBuffered(true);
entrySpinner = new JSpinner(new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1));
add(entrySpinner);
....
我有一个方法可以给它一个位置:
public void resize() {
entrySpinner.setBounds((int) (this.getWidth() * .2), (int) (this.getHeight() * 0.38), (int) (this.getWidth() * 0.3), (int) (this.getHeight() * 0.1));
}
我在这里覆盖了paint方法:
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
.. draw shapes..
super.paintComponents(g);
super.validate();
Toolkit.getDefaultToolkit.sync();
g.dispose();
}
答案 0 :(得分:2)
paintComponent
两次,一次拨打super.paint
并手动拨打validate
方法中调用paint
,这只会导致组件重新开始重新绘制,反复...再次向您的CPU说再见.. Toolkit.getDefaultToolkit.sync();
,但我有理由相信你不应该在paint
方法中做到这一点paint
方法。执行自定义绘制的推荐方法是paintComponent
方法(由paint
调用)您可能希望阅读
使用示例更新
为我修好了......
左边是你的代码,右边是我的
public class TestPaintSpinner {
public static void main(String[] args) {
new TestPaintSpinner();
}
public TestPaintSpinner() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setDoubleBuffered(true);
setLayout(new GridBagLayout());
add(new JSpinner(new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1)));
JPanel panel = new JPanel();
panel.add(new JLabel("Subpanel"));
add(panel);
}
// public void paint(Graphics g) {
// super.paint(g);
//
// Graphics2D g2d = (Graphics2D) g;
// Point2D sPoint = new Point2D.Float(0, 0);
// Point2D ePoint = new Point2D.Float(this.getWidth(), this.getHeight());
//
// g2d.setRenderingHint(
// RenderingHints.KEY_TEXT_ANTIALIASING,
// RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// super.paintComponents(g);
// super.validate();
// Toolkit.getDefaultToolkit().sync();
// g.dispose();
// }
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
Graphics2D g2d = (Graphics2D) g;
Point2D sPoint = new Point2D.Float(0, 0);
Point2D ePoint = new Point2D.Float(this.getWidth(), this.getHeight());
// Note, this will effect every component painted after this one!!
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
}
}