如何覆盖paintComponent方法并与JPanel一起使用

时间:2018-02-02 15:49:24

标签: java swing jpanel paintcomponent

我有覆盖paintComponents方法,但是当我使用它并运行我的程序时,它不会显示任何圆和线。最近frakcool表示它不是paintComponents方法,而是paintComponent方法。我发现了,但我没有得到任何paintComponent方法。

这张照片显示我没有任何paintComponent方法:

this picture shows that i have not got any paintComponent method.

代码在这里。

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SecondActivity frame = new SecondActivity();
                frame.setBackground(Color.WHITE);
                frame.paintComponents(null);
              //  frame.paint(null);
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}

@Override
public void paintComponents(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponents(g);
    Graphics2D g2d = (Graphics2D) g;

    drawCircle(g2d,centerX,centerY,r);
    drawLineAzim(g2d);
    drawLineEle(g2d);

}

1 个答案:

答案 0 :(得分:0)

你压倒了错误的方法。摆脱" s"在方法名称的末尾。您需要覆盖paintComponent(...)

但是,您还需要覆盖paintComponent(...)的{​​{1}}方法,您只需将该方法添加到您的课程中并期望它可以正常工作。这是Java 101。

Custom Painting

上的Swing教程中找到的工作示例开始