简体   繁体   English

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

[英]Java Why my method paintComponent() does not work?

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!");
    }

}

Here is my code, I try to draw circles in a JPanel, I really don't understand why paintComponent doesn't work. 这是我的代码,我尝试在JPanel中绘制圆,我真的不明白为什么paintComponent不起作用。 Could anyone please help me? 谁能帮我吗?

Your code would work - if you could trigger a repaint() event on your panel. 您的代码可以正常工作-如果您可以在面板上触发repaint()事件。

The problem is that you are initializing your GUI on the main thread instead of the EDT. 问题是您正在主线程而不是EDT上初始化GUI。 Therefore the JFrame is shown before you can add the NewJPanel to it. 因此,在将NewJPanel添加到JFrame之前,先显示JFrame。 And just adding the NewJPanel with a fixed layout does not trigger a repaint event. 并且仅添加具有固定布局的NewJPanel不会触发重新绘制事件。

You should change your main method to 您应该将主要方法更改为

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);
    });
}

Or, if you cannot use Java8: 或者,如果您不能使用Java8:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM