Cj1*_*j1m 1 java graphics swing paint jpanel
我尝试使用paint(Graphics g)代码时出错.你能帮忙解决代码,以便有一个带有3d矩形的窗口.谢谢!
private static void paint(Graphics g){
g.draw3DRect(10, 10, 50, 50, true);
Run Code Online (Sandbox Code Playgroud)
然后走向瓶底:
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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在Java中,覆盖时不能减少方法的可见性.同样,不能进行实例方法static
.它必须是
@Override
public void paint(Graphics g){
super.paint(g);
g.draw3DRect(10, 10, 50, 50, true);
}
Run Code Online (Sandbox Code Playgroud)
在Swing中,不要在顶级窗口中进行自定义绘制,例如a 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);
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记将新组件的实例添加到JFrame
:
frame.add(new MyComponent());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
227 次 |
最近记录: |