[英]How to add multiple Strings to JLabel without creating a new object
How can I add multiple Strings in JLabel without create a new Object with mouseListener and I want it to display the list of Strinsg in block but it always display inline. 如何在JLabel中添加多个String而不用mouseListener创建新对象,我希望它在块中显示Strinsg列表,但始终显示内联。
My question is everytime I have an String I need to create a new object of JLabel? 我的问题是,每当我有一个字符串时,我都需要创建一个新的JLabel对象? BTW I have a database with a lot of names and creating a lot of JLabel will be hard. 顺便说一句,我有一个名称很多的数据库,创建很多JLabel会很困难。
HERE is the image Click HERE 这里是图片点击这里
HERE is my current code with objects I have a database that gets the String of names and then add to JPanel. 这里是我当前的对象代码,我有一个数据库,该数据库获取名称的字符串,然后添加到JPanel。
x = new JLabel("name1");
x1 = new JLabel("name2");
x.setFont(new Font("calibri",Font.BOLD,20));
x.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
x.setForeground(Color.blue);
}
@Override
public void mouseExited(MouseEvent arg0) {
x.setForeground(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent arg0) {
x.setForeground(Color.RED);
}
@Override
public void mouseReleased(MouseEvent a) {
x.setForeground(Color.blue);
}});
add(x);
add(x1);
It sounds like you are looking for a better control. 听起来您正在寻找更好的控制。 What about JList? JList呢?
http://docs.oracle.com/javase/tutorial/uiswing/components/list.html http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
您可以在Swing按钮,菜单按钮,标签等内部使用HTML格式。设置文本时只需使用html标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.