简体   繁体   English

如果不使用 label 文本,如何从 Jlabel 中删除空行

[英]How to remove empty lines from Jlabel if the label text is not used

"If user input YES for flag1, then the message History is recommended... will display in the first line of JLabel.If user input NO for flag1, then it will not display anything in the first line. My question is how to enable the flag2 message,English core is recommended... to be displayed in the first line of JLabel if the user input NO for flag1 and YES for flag2 " “如果用户为 flag1 输入 YES,则建议消息历史记录...将显示在 JLabel 的第一行。如果用户为 flag1 输入 NO,则它不会在第一行显示任何内容。我的问题是如何启用flag2 消息,推荐英文核心...如果用户为 flag1 输入 NO 为 flag2 输入 YES,则显示在 JLabel 的第一行"

        if (flag1 == 1) {
            JLabel label1 = new JLabel("History is recommended because you have not taken the core subject");
            label1.setBounds(10, 10, 800, 25);
            add(label1);
        }
        if (flag2 == 2) {
            JLabel label2 = new JLabel("English core is recommended because you have not taken the language core");
            label2.setBounds(10, 30, 800, 25);
            add(label2);
  1. Build an ArrayList of messages.构建消息的 ArrayList。
  2. Then iterate through the ArrayList and create a JLabel for each message and add the label to the panel.然后遍历 ArrayList 并为每条消息创建一个 JLabel 并将 label 添加到面板中。

Also, don't use a null layout and setBounds().此外,不要使用 null 布局和 setBounds()。 Swing was designed to be used with layout managers. Swing 旨在与布局管理器一起使用。 You could for example use a Box Layout .例如,您可以使用Box Layout

This approach is more dynamic as it will support any number of messages.这种方法更加动态,因为它将支持任意数量的消息。

Or instead of using individual labels, you could use other Swing components for multi-line display, like:或者不使用单独的标签,您可以使用其他 Swing 组件进行多行显示,例如:

  1. a non editable JTextArea and just append(...) a message as required.一个不可编辑的JTextArea并根据需要append(...)一条消息。
  2. a JList一个JList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM