repaint()不起作用

时间:2015-03-12 09:09:15

标签: java swing paintcomponent java-2d

我想绘制圆圈并将其与中心对齐,但当我呼叫repaint()时,没有任何反应。我尝试了几乎所有的东西,我已经改变了布局,对齐,但总是一样的。这是我的代码:

public class Frame extends JFrame {

    JButton button,dugme;
    JLabel lab;

    public Frame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(480,320);
        setResizable(false);
        setLocationRelativeTo(null);
        setLayout(new FlowLayout());
        setVisible(true);

        button = new JButton("Klikni me");
        button.setSize(75,75);
        add(button);
        button.setHorizontalAlignment(SwingConstants.RIGHT);

        dugme= new JButton("Klikni opet");
        dugme.setSize(75,75);
        add(dugme);
        dugme.setHorizontalAlignment(SwingConstants.LEFT);

        lab = new JLabel("Ovde je tekst koji se menja");
        add(lab);
        lab.setHorizontalAlignment(SwingConstants.CENTER);
        Handler handler = new Handler();
        Handler1 handler1= new  Handler1();
        repaint();

        button.addActionListener(handler);
        dugme.addActionListener(handler1);
    }

    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.fillOval(30, 30, 60, 75);
    }

     public class Handler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            repaint();
            lab.setText("New text");
        }
    } 

     public class Handler1 implements ActionListener{
         public void actionPerformed(ActionEvent e){
             lab.setText("Same text again ");
                repaint();
         }
     }
}

1 个答案:

答案 0 :(得分:3)

public void paintComponent(Graphics g){
    super.paintComponents(g);

打破画颜链,它应该是:

public void paintComponent(Graphics g){
    super.paintComponent(g); // no S in method name..