如何在运行时在Swing中添加JLabel? [英] How to add JLabel at run time in Swing?
本文介绍了如何在运行时在Swing中添加JLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Java制作窗口应用程序. 我想在运行时在Swing应用程序中添加标签名称.如何使用Java Swing执行此操作?
I am making am window application on Java. I want to add label name at run time in my Swing application. How can I do this using Java Swing?
public class Component1 extends JPanel {
Component1() {
JLabel label = new JLabel("dd");
label.setBounds(370, 340, 150, 20);
// label.setText("labeVVl");
add(label);
}
public static void main(String[] args)
{
// create frame
JFrame frame = new JFrame();
final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 600;
// set frame attributes
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("My Frame");
frame.setVisible(true);
Component1 Com = new Component1();
Component add = frame.add(Com);
}
}
推荐答案
-
此代码应该通过添加
revalidate()
和repaint()
作为LayoutManager
不要使用 NullLayout
,使用为 FlowLayout
AP中的href ="http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html" rel ="nofollow noreferrer"> JPanel
我也这样做
don't to use NullLayout
, use default FlowLayout
implemented for JPanel
in API do that the same way
请参见初始线程
这篇关于如何在运行时在Swing中添加JLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文