小编典典

paintComponent()永远不会在JFrame上执行

java

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;
import java.util.*;

public class test1 extends JFrame implements MouseListener  {
    private JPanel JP = new JPanel();

    public test1() {
        JP.setBorder(BorderFactory.createLineBorder(Color.black)); 
        JP.addMouseListener(this);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); 
        this.add(JP);
        this.pack();
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                    test1 frame = new test1();
                    frame.setSize(400,400);
                    frame.setVisible(true);
              }
        });
    }

    public void mouseClicked(MouseEvent e) {
        //drawCircle(e.getX(), e.getY());
        //repaint();
        ballball ball;
        ball = new ballball();
        //ball.paintComponent(Graphics g);
        System.out.println("ballball");

    }

    public void mouseExited(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {
        //this.mouseX=e.getX();
        //this.mouseY=e.getY();
    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }    
}

class ballball extends test1 implements Runnable {

    private int squareX = 50;
    private int squareY = 50;
    private int squareW = 100;
    private int squareH = 100;
    public boolean draw;

    private Vector<Object> v = new Vector<Object>();

    public ballball() {

        /*addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                draw = true;
                //Thread thread1 = new Thread(this.moveSquare(50, 50));
                repaint();
                //moveSquare(e.getX(),e.getY());
            }
        });*/


        /*addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                moveSquare(e.getX(),e.getY());
            }
        });*/
        System.out.println("ball created");
        this.repaint();

    }

    public void run() {

    }

    private void moveSquare(int x, int y) {
        int OFFSET = 1;
        if ((squareX!=x) || (squareY!=y)) {
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
            squareX=x;
            squareY=y;
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        } 
    }

    public void paint(Graphics g) {
        g.drawString("abcasdfasffasfas", 10, 10);
    }

    //@Override
    public void paintComponent(Graphics g) {
        //if (draw) {
        // existing code
        System.out.println("paint");
        //super.paintComponent(g);

        //g.drawString("This is my custom Panel!",10,20);
        //g.setColor(Color.RED);
        //g.fillRect(squareX,squareY,squareW,squareH);
        //g.setColor(Color.BLACK);
        //g.drawRect(squareX,squareY,squareW,squareH);
        Shape circle = new Ellipse2D.Float(squareX,squareY,100f,100f);
        Graphics2D ga = (Graphics2D)g;
        ga.draw(circle);
        //}
    }  
}

该程序的目的是单击以创建圆,ballball类扩展了test1,当test1检测到鼠标单击时,即创建了ballball对象。但是paint /
paintComponent方法永远不会执行。在我的程序结构中,是否可以将圆圈绘制到超类JPanel?


阅读 287

收藏
2020-09-23

共1个答案

小编典典

JFrame不是JComponent,它没有paintComponent可以覆盖的方法。相反,您可以扩展a JPanel并将其添加到框架中。

2020-09-23