我是新来的编码,我有这个问题:我想添加一个JLabel
到JFrame
我创建在不同的类。这是代码。我不明白如何正确地做到这一点,但如果他们采用相同的方法,我会知道该怎么做。如何将JLabel添加到另一个类的JFrame上?
地图:
import javax.swing.JFrame;
public class Map {
public static void main(String[] args) {
map();
}
private static void map() {
JFrame window = new JFrame("Run Kitty Run!");
window.setVisible(true);
window.setSize(1000, 500);
}
}
猫:
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Cat
{
//instance variables
ImageIcon pic;
public Cat()
{
//constructor
pic = new ImageIcon("/Users/dell/Desktop/runKittyRun/cat.png");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void draw()
{
JPanel panel = new JPanel();
JLabel label = new JLabel(pic);
panel.add(label);
panel.setVisible(true);
}
}
我试图做windows.add(标签),并没有工作..:/
这对你来说太过先进,因为你刚刚开始。所需要的只是Map的一个实例。 –