自定义组件未显示

时间:2015-06-26 10:44:21

标签: java swing jcomponent

当我将组件的大小设置为超出组件位置的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();
    }
}
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

将子组件添加到null布局(absolute positioning)时,您应该准确指定组件的位置和尺寸。

使用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();

这会返回Adamak相对于它的父容器的位置

ad1.setLocation(100,100); 

并从您调用的paintComponent method

 Point point = getLocation();

现在point.x=100point.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和高度

这是输出

enter image description here

<强>但

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