我的repaint方法没有从另一个类调用paintComponent

问题描述

我从另一个扩展了JPanel的类中创建了一个盒子,事实是,在我从另一个类中进行repaint()之后,paintComponent()没有得到调用。我不知道重新粉刷的工作原理,我正在尝试练习一些工作。

这是我的JFrame:

package Main;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

import KeyActions.keyActions;
import Objects.Piece;

public class createWindow extends JFrame {
    
    public createWindow() {
        super();
        myPanel panel = new myPanel();
        keyActions actions = new keyActions();
        KeyListener key = new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                
                
                
            }

            @Override
            public void keypressed(KeyEvent e) {
                
                keyAction(e.getKeyCode());
                
            }

            @Override
            public void keyreleased(KeyEvent e) {
                
                
                
            }
            public void keyAction(int keycode) {
                switch (keycode) {
                    case KeyEvent.VK_UP:
                        actions.keyUP();
                        break;
                    case KeyEvent.VK_RIGHT:
                        
                        break;
                    case KeyEvent.VK_DOWN:
                        
                        break;
                    case KeyEvent.VK_LEFT:
                        
                        break;
                }
            }
            
        };
        
        add(panel);
        addKeyListener(key);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setResizable(false);
        setLocationRelativeto(null);
        setVisible(true);
        
    }
    
}

这是我来自JPanel的代码

package Main;

import java.awt.Basicstroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import Objects.Piece;

public class myPanel extends JPanel {
    
    //main character
    public Piece innerLayer = new Piece(25,25,50,new Color(255,255,0),"self");
    
    public myPanel() {
        super();
        setBackground(Color.gray);
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D)g;
        
        g2d.setColor(innerLayer.color);
        g2d.setstroke(new Basicstroke(15));
        g2d.drawRect(innerLayer.x,innerLayer.y,innerLayer.width,innerLayer.height);
        System.out.println("I am get called");
    }
    
    
}

这是我的关键动作:

package KeyActions;

import Main.myPanel;

public class keyActions {
    
    myPanel panel = new myPanel();
    
    public void keyUP() {
        int x = panel.innerLayer.getX();
        int y = panel.innerLayer.getY();
        y--;
        
        panel.innerLayer.addPosVal(x,y);
        panel.repaint();
        System.out.println("keyUp is called");
    }
    
    
}

调用keyUP之后,repaint()没有调用paintComponent(),它应该打印出“我被调用”,但没有。

这是我来自Piece对象的代码

package Objects;

import java.awt.*;
import java.util.Arrays;

import javax.swing.*;

public class Piece {
    
    public int x;
    public int y;
    public int width;
    public int height;
    public Color color;
    public String type;
    
    public Piece(int x,int y,int width,int height,Color color,String type) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
        this.type = type;
    }
    
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }
    
    
    public void addPosVal(int x,int y) {
        this.x += x;
        this.y += y;
    }
    
    
    public int getX() {
        return this.x;
    }
    public int getY() {
        return this.y;
    }
    
    
    public int getWidth() {
        return this.width;
    }
    public int getHeight() {
        return this.height;
    }
    
    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)