简体   繁体   English

如何在不创建新对象的情况下向JLabel添加多个字符串

[英]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.

相关问题 如何在不创建新对象的情况下修剪java中的字符串? - How to trim strings in java without creating a new object? 如何在不使用'+'或任何内置函数的情况下在'String'类中创建新对象的情况下在1个对象中并置两个字符串 - How to concat two strings in 1 object without creating new object in 'String' class without using '+' or any inbuilt function 如何更改“ panel.add(new JLabel(“”));”中的JLabel的字体大小 - How to change the font size of a JLabel that is in a “panel.add(new JLabel(”“));” 如何使用变量在JLabel中添加新行 - How to add new line in a JLabel with variables 有没有一种方法可以在不创建新对象的情况下将秒添加到SimpleDateFormat对象? - Is there a way to add seconds to a SimpleDateFormat object without creating a new object? 如何在不使用 HTML 的情况下向 JLabel 添加换行符 - How to add a newline to JLabel without using HTML 如何将鼠标侦听器添加到多个JLabel [] - How to add a mouse listener to multiple JLabel[] 如何在多个复选框的顶部添加JLabel文本? - How to add JLabel text on top of multiple Checkboxes? 在 mouseListener 中删除并添加新的 JLabel - Remove and add new JLabel in mouseListener 如何在Swing JLabel中按设置的每个字符数添加新行 - How to add a new line every set number of chars in a swing JLabel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM