编程语言
首页 > 编程语言> > java – 自定义组件未显示

java – 自定义组件未显示

作者:互联网

当我将组件的大小设置为超出组件位置的x和y时,它们都会显示出来,否则它们就不会出现.
这是我的代码:

public class AdamakPogram{

  public static void main(String[] args) {
     PlayGround playGround = new PlayGround();
     Adamak ad1 = new Adamak();
     Adamak ad2 = new Adamak();
     ad1.setLocation(100,100);
     ad2.setLocation(150, 100);
     ad1.addMouseListener(new FocusListener(ad1));
     ad1.addKeyListener(new ArrowListener(ad1));
     playGround.addPerson(ad1);
     ad2.addMouseListener(new FocusListener(ad2));
     ad2.addKeyListener(new ArrowListener(ad2));
     playGround.addPerson(ad2);
 }  
}

我的PlayGround课程:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class PlayGround extends JFrame{

   private static final long serialVersionUID = 1L;
   private JPanel zone;

public PlayGround() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 600);
    setResizable(false);
    zone = new JPanel();
    zone.setBorder(new EmptyBorder(5, 5, 5, 5));
    zone.setBounds(100, 100, 800, 600);
    setContentPane(zone);
    zone.setLayout(null);
    setVisible(true);
}

public void addPerson(Adamak adamak){
    zone.add(adamak);
    revalidate();
    repaint();
}

public JPanel getZone() {
    return zone;
}

public void setZone(JPanel zone) {
    this.zone = zone;
}

}

我的adamak(自定义组件)类:

import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JComponent;

public class Adamak extends JComponent {

   private static final long serialVersionUID = 1L;

   @Override
   protected void paintComponent(Graphics arg0) {
     super.paintComponent(arg0);
     Point point = getLocation();
     arg0.drawRect(point.x-10, point.y-10, 20, 50);//drawing stomach
     arg0.drawLine(point.x+10,point.y, point.x+20, point.y+50);//drawing right hand
     arg0.drawLine(point.x-10, point.y, point.x-20, point.y+50);//drawing left hand
     arg0.drawLine(point.x, point.y-10, point.x, point.y-20);//drawing neck
     arg0.drawLine(point.x-5, point.y+40, point.x-5, point.y+100);//drawing left leg
     arg0.drawLine(point.x+5, point.y+40, point.x+5, point.y+100);//drawing right leg
     arg0.drawOval(point.x-10, point.y-40, 23, 20);//drawing head
     arg0.drawRect(point.x-20, point.y-40, 40, 140);
}

public void moveRight(){
    Point point = getLocation();
    if((point.x+1-20) < 710){
        setLocation(new Point(point.x+1, point.y));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}

public void moveLeft(){
    Point point = getLocation();
    if((point.x-1-20) > 0){
        setLocation(new Point(point.x-1,point.y));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}

public void moveUp(){
    Point point = getLocation();
    if((point.y-1-40) > 0){
        setLocation(new Point(point.x,point.y-1));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}

public void moveDown(){
    Point point = getLocation();
    if((point.y+1-100) < 461){
        setLocation(new Point(point.x,point.y+1));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}
}

任何帮助,将不胜感激.

解决方法:

将子组件添加到空布局(绝对定位)时,应准确指定组件的位置和尺寸.

使用setbounds(x,y,width,height)方法就可以做到.

因为你设置adamax组件的位置setSize()就足够了.

Adamak ad1 = new Adamak();
ad1.setSize(300, 300);
ad1.setLocation(100, 100);

或使用setBounds()

Adamak ad1 = new Adamak();
ad1.setBounds(100,100,300,300);

编辑

这是给你的评论

是的,让我解释一下你正在绘制的矩形.

主要问题是这段代码

Point point = getLocation();

在这种情况下,这会将Adamakrelative的位置返回到它的父容器

ad1.setLocation(100,100); 

并从您调用的paintComponent方法

 Point point = getLocation();

现在point.x = 100,po​​int.y = 100
然后使用绘制外框

arg0.drawRect(point.x-20, point.y-40, 40, 140);

与…一样
     arg0.drawRect(80,60,40,140);

但你希望你的组件宽度为40,这怎么可能?
它从80的组件的x开始绘制,但你的面板的宽度为40.所以你什么也看不到.至少(80 40)宽度组件需要显示在矩形的右边,但是边框线有一些厚度所以你需要120宽度组件来显示它.

你想要的是改变这一点
例如

Point point = new Point(20, yyy);
arg0.drawRect(point.x-20, point.y-40, 40, something);

现在40宽度组件很好.因为这是相同的
    arg0.drawRect(0,point.y-40,40,something);
它从右上角开始绘制,它是0,宽度是40,组件的宽度是40,同样是y和高度

这是输出

如果你想移动你的精神而不是移动jcomponents这是不高效使用paintComponent方法重绘它们.这是很好的例子https://www3.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html

标签:java,swing,jcomponent
来源: https://codeday.me/bug/20190708/1400047.html