更新时间:2023-12-05 18:47:04
创建JPanel
的实例时,(假设您这样做),请执行以下操作:
When you create your instance of JPanel
, (assuming you're doing it this way), do this:
JPanel panel = new JPanel(){
@Override
public void paintComponent(Graphics g){
// paint code
}
};
另一种替代方法是创建扩展 JPanel
的private class
.
例如:
The other alternative is to create a private class
which extends JPanel
.
For example:
public class OuterClass{
// fields, constructors, methods etc..
private class MyPanel extends JPanel{
// fields, constructors, methods etc..
@Override
public void paintComponent(Graphics g){
// paint code
}
}
}