且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在jframe中为jpanel调用paintcomponent

更新时间: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
    }
};

另一种替代方法是创建扩展 JPanelprivate 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
       }

    }
}