[英]Problem painting JLabel class to another JPanel class
我创建了一个类,扩展了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的任何建议? 另外,这是让游戏随机在屏幕上浮动图片的好方法吗?
首先,不需要扩展JLabel即可做到这一点。
a)使用以下方法将图像添加到标签后,设置标签的大小:
label.setSize( label.getPreferredSize() );
b)您不需要draw()和所有的setter方法。 要移动标签,只需执行以下操作:
label.setLocation(...);
c)如果您想增加位置,则可以使用以下方法:
label.setLocation(label.getLocation()。x + 5,...);
设置标签的大小和位置后,可以将其直接添加到面板中。 确保您已完成:
panel.setPreferredSize()
当您将面板添加到框架的内容窗格时。
您的代码太含糊,无法给出具体建议。 如果您需要更多帮助,请发布SSCCE 。 您的问题可能是布局管理器的使用或您没有使用布局管理器的事实。
是的,您应该将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
我认为,主要问题是您并未真正将图像添加到构造函数中的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
与指定的图像。
解决构造函数问题后,可以从添加了它的JPanel
上的Head
对象上调用setLocation()。 这样便可以随机移动它。
另外,在要添加Head
的JPanel
中,需要确保将LayoutManaer
设置为null,以便可以自己将组件手动放置在面板上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.