2

我创建了一个扩展 JLabel 的类,以用作我的对象在 JPanel 周围移动以进行游戏。

import javax.swing.*;

public class Head extends JLabel {

 int xpos;
 int ypos;

 int xvel;
 int yvel;

 ImageIcon chickie = new ImageIcon(
        "C:\\Users\\jjpotter.MSDOM1\\Pictures\\clavalle.jpg");
 JLabel myLabel = new JLabel(chickie);

 public Head(int xpos, int ypos, int xvel, int yvel){

  this.xpos = xpos;
  this.ypos = ypos;
  this.xvel = xvel;
  this.yvel = yvel;
 }

 public void draw(){
  myLabel.setLocation(xpos, ypos);
 }

 public double getXpos() {
  return xpos;
 }

 public double getYpos() {
  return ypos;
 }

 public int getXvel() {
  return xvel;
 }

 public int getYvel() {
  return yvel;
 }

 public void setPos(int x, int y){

  xpos = x;
  ypos = y;

 }

}

然后我试图将它添加到我的 JPanel 上。从这里我将随机让它增加它的 x 和 y 坐标以使其在屏幕上浮动。我无法让它自己绘制到 JPanel 上。我知道我在这里缺少一个关键概念,即在不同的面板上绘制组件。这是我在 GamePanel 课程中的内容

import java.awt.Dimension;
import java.util.Random;
import javax.swing.*;


public class GamePanel extends JPanel {

 Random myRand = new Random();
 Head head = new Head(20,20,0,0);

 public GamePanel(){

  this.setSize(new Dimension(640, 480));
  this.add(head);

 }

}

有关如何将其添加到 JPanel 的任何建议?另外,这是让游戏画面随机漂浮在屏幕上的好方法吗?

4

3 回答 3

2

首先,不需要扩展 JLabel 来执行此操作。

a) 在将图像添加到标签后,您可以使用以下方法设置标签的大小:

label.setSize( label.getPreferredSize() );

b) 你不需要 draw() 和所有的 setter 方法。要移动标签,您只需使用:

label.setLocation(...);

c)如果你想增加你会使用类似的位置:

label.setLocation(label.getLocation().x + 5, ...);

设置标签的大小和位置后,您可以直接将其添加到面板中。确保您已完成:

panel.setPreferredSize() 

当您将面板添加到框架的内容窗格时。

您的代码太模糊,无法给出具体建议。如果您需要更多帮助,请发布您的SSCCE。您的问题可能是布局管理器的使用或您没有使用布局管理器的事实。

于 2010-06-09T15:09:06.460 回答
2

是的,您应该将 JPanel ( GamePanel ) 的布局管理器设置为 null 以告知系统:

不要给我放,我手动做

编辑

我想如果我给你一个运行演示会更清楚。

请参阅此示例。正如 camickr 指出的那样,您不必对组件进行子类化。

import javax.swing.*;
import java.util.Timer;
import java.util.*;

class FloatingDemo {
    public static void main( String [] args ){
        // create the panel         
        JPanel panel = new JPanel();
        panel.setLayout(null);

        // create the label with an image
        final JLabel label = new JLabel(new ImageIcon("StackOverflowLogo.png"));
        label.setSize(label.getIcon().getIconWidth(), 
                      label.getIcon().getIconHeight());
        panel.add( label );

        // create the frame containing both 
        JFrame frame = new JFrame();
        frame.add( panel );
        frame.setSize(800, 600 );
        frame.setVisible( true );

        // move it randomly every second  
        Timer timer = new Timer();
        final Random random = new Random();
        timer.schedule( new TimerTask() {
           public void run(){
                 label.setLocation( random.nextInt(800-label.getWidth()), 
                                    random.nextInt(600-label.getHeight()));       
           } 
        }, 0, 1000 );

    }
}

顺便说一句,不将布局管理器设置为 null 也可以,但是如果您调整窗口大小,jpanel 会自动为您设置位置。

运行演示 http://img444.imageshack.us/img444/2567/capturadepantalla201006c.png

于 2010-06-09T15:11:05.770 回答
1

我认为的主要问题是您并没有真正将图像添加到Head构造函数中。

您需要做的是ImageIcon像您正在做的那样创建一个新的,并在您的构造函数中做一些事情;

public Head(int xpos, int ypos, int xvel, int yvel){
    // calls the JLabel constructor to create a label with an image
    super(new ImageIcon("C:\\Users\\jjpotter.MSDOM1\\Pictures\\clavalle.jpg"))
    this.xpos = xpos;
    this.ypos = ypos;
    this.xvel = xvel;
    this.yvel = yvel;
}

这将Head使用指定的图像创建您的。

解决构造函数问题后,您可以HeadJPanel已添加对象的对象上调用 setLocation() 。这就是你可以随机移动它的方式。

此外,在JPanel您添加Headto 时,您需要确保将 设置LayoutManaer为 null,以便您可以自己手动将组件放置在面板上。

于 2010-06-09T15:08:13.973 回答