如何在java中使用两个绘制方法?或者在基本涂料方法之外

YoA*_*oAv 3 java graphics swing jframe

我怎样才能创建两种绘画方法?当我试图使用两种涂料方法时,它们永远不会起作用.如果不能,我想在基本的油漆方法之外绘画,我不知道如何.例如:

public class test extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test frame = new test();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public void paint(Graphics g) {
    g.fillRect(100, 100, 100, 100);
}

public void pp(Graphics g) {
    g.fillRect(250, 100, 100, 100);
}

/**
 * Create the frame.
 */
public test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
}

}
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 5

当我试图使用两种涂料方法时,它们永远不会起作用.

paintComponent(...)不是JFrame的方法.每当您尝试覆盖一个方法时,您应该使用@Override注释,编译器会在您尝试覆盖不存在的方法时告诉您.

通常,对于其他Swing组件,该paint(...)方法负责调用该paintComponent(...)方法,因此您不应该覆盖该paint()方法.请参阅:详细了解Paint Mechanism了解更多信息.

无论如何,你不应该覆盖JFrame上的paint().阅读Performing Custom Painting教程链接中的整个部分,了解如何进行自定义绘制的工作示例.