Java为什么我的方法paintComponent()不起作用?

时间:2016-10-28 19:18:11

标签: java swing

package work;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.util.Timer;
import java.util.TimerTask;



class NewJPanel extends JPanel{
    //private MoveButton button;
    static MoveButton button;
    JTextField textfield;

    NewJPanel(){
        setLayout(null);
        textfield=new JTextField();
        textfield.setBounds(600, 50, 100, 40);
        button=new MoveButton();
        //button.setBounds(0, 0, 50, 50);
        //add(button);
        add(textfield);
        //repaint();
        System.out.println("Newpanel construct");
        //System.out.println("1");
        //button=new MoveButton();
        //button.addActionListener(button);
        //add(button);
    }

    public static void main(String[] args){
        JFrame gui=new JFrame();
        gui.setSize(800, 800);
        gui.setVisible(true);

        NewJPanel panel=new NewJPanel();
        //panel.setLayout(null);
        panel.add(button);
        gui.add(panel);

    }

    @Override public void paintComponent(Graphics g){
        System.out.println("drawing");
        super.paintComponent(g);
        for(int i=0;i<400;i+=100)
            for(int j=0;j<400;j+=100)
                g.drawOval(i, j, 100, 100);
        //button.setBounds(button.getx(),button.gety(),50,50);
        textfield.setText(Integer.toString(button.get()));
    }
}

class MoveButton extends JButton implements ActionListener{

    private Random randomgenerator=new Random();
    private int indexX=0;
    private int indexY=0;
    private int score=0;

    public MoveButton(){
        setBounds(0, 0, 50, 50);
        addActionListener(this);
        System.out.println("movebutton construct");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        //setBounds(200,0,100,100);
        indexX=100*randomgenerator.nextInt(4);
        indexY=100*randomgenerator.nextInt(4);
        score+=1;
        //System.out.println(indexX);
        setBounds(indexX, indexY, 50, 50);
        repaint();
    }

    public int getx(){
        return indexX;
    }

    public int gety(){
        return indexY;
    }

    public int get(){
        return score;
    }
} 

class TimerTaskTest extends java.util.TimerTask{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("Start!");
    }

}

这是我的代码,我尝试在JPanel中绘制圆圈,我真的不明白为什么paintComponent不起作用。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您的代码可行 - 如果您可以在面板上触发h事件。

问题是您是在主线程而不是EDT上初始化GUI。因此,在您向其添加repaint()之前,会显示JFrame。只需添加具有固定布局的NewJPanel不会触发重绘事件。

您应该将主要方法更改为

NewJPanel

或者,如果你不能使用Java8:

public static void main(String[] args){
    SwingUtilities.invokeLater(() -> {
        JFrame gui=new JFrame();
        gui.setSize(800, 800);
        gui.setVisible(true);

        NewJPanel panel=new NewJPanel();
        //panel.setLayout(null);
        panel.add(button);
        gui.add(panel);
    });
}