当我尝试使用paint(Graphics g)代码时出现错误。你可以帮助解决代码,以便有一个三维矩形的窗口。谢谢!
private static void paint(Graphics g){
g.draw3DRect(10, 10, 50, 50, true);然后走向底部:
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
paint();
}
});
}
}发布于 2013-05-28 23:17:22
在Java中,重写时不能降低方法的可见性。同样,实例方法也不能成为static。它必须是
@Override
public void paint(Graphics g){
super.paint(g);
g.draw3DRect(10, 10, 50, 50, true);
}在Swing中,不要在顶级窗口(如JFrame )中进行自定义绘制。相反,创建JComponent的子类并覆盖paintComponent,并确保调用super.paintComponent(g)。
class MyComponent extends JComponent {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.draw3DRect(10, 10, 50, 50, true);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
}不要忘记将新组件的实例添加到JFrame中
frame.add(new MyComponent());https://stackoverflow.com/questions/16795371
复制相似问题