How I add a JLabel to a JFrame on another class?(如何将JLabel添加到另一个类的JFrame中?)
本文介绍了如何将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(标签),但不起作用。:/
推荐答案
OK,这对任何变量或方法都有效,而不仅仅是JLabel。让我们首先将这些方法称为method one和method Two。Method One将在一个名为One的类中(我不会费心写出类主体,我是在我的iPod中写的)
JLabel jl;
public void methodOne(){
jl = // blah blah blah;
}
好的,现在您希望在第二个方法中使用JL。因此,以下是要做的事情:
public void methodTwo(){
One o = new One();
JFrame j = // instance of JFrame;
j.add(o.jl);
}
记住:JLabel变量在类One中,所以我实例化了一个。
希望能有所帮助
这篇关于如何将JLabel添加到另一个类的JFrame中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:如何将JLabel添加到另一个类的JFrame中?
基础教程推荐
猜你喜欢
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
