JLabel在组布局-冻结大小



我在组布局中有一个JLabel。JLabel是用HTML文本填充的,所以当文本太长而不能单行时,它会自动换行。

如果JLabel已经显示了两行,并且我在JLabel上设置了新的文本,这也将导致两行,那么JLabel的大小将临时更改,然后恢复到与之前相同的大小。这种临时的大小变化会导致环绕元素出现不必要的可见变化。

是否有办法防止这种临时的大小变化,使显示不移动?

我查看了最小/最大/首选大小,但在设置大小之前,所有3的高度都是相同的,所以设置最大值似乎没有帮助。

如下所示。要运行这个示例1)运行。2)滚动列表到最底部,这样20就会显示在列表中。3)点击按钮。列表的底部现在将显示19,因为列表的大小改变了,因为标签的大小改变了,即使标签不需要改变大小。

    import java.util.Date;
    public class LabelSizeTest extends javax.swing.JFrame {
      public LabelSizeTest() {
        initComponents();
      }
      private void initComponents() {
        lblTest = new javax.swing.JLabel();
        btnTest = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        lblTest.setText("<html>test asd asdf asdf asdf asdf asdf asdf asdf asdf end");
        lblTest.setName("lblTest"); // NOI18N
        btnTest.setText("Click Me");
        btnTest.setName("btnTest"); // NOI18N
        btnTest.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnTestActionPerformed(evt);
          }
        });
        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jList1.setModel(new javax.swing.AbstractListModel() {
          String[] strings = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
          public int getSize() { return strings.length; }
          public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
              .addGroup(layout.createSequentialGroup()
                .addComponent(btnTest)
                .addGap(0, 165, Short.MAX_VALUE))
              .addComponent(jScrollPane1))
            .addContainerGap())
        );
        layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnTest)
            .addContainerGap())
        );
        pack();
      }
      private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {                                        
        lblTest.setText(lblTest.getText() + "x");
      }                                       
      public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            new LabelSizeTest().setVisible(true);
          }
        });
      }
      private javax.swing.JButton btnTest;
      private javax.swing.JList jList1;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JLabel lblTest;
    }

我在你的代码中做了两个改变:(你可以使用它们中的任何一个)首先:HTML文本

<html><body style='width:200 px'>test asd asdf asdf asdf asdf asdf asdf asdf asdf end

第二:设置Jlabel

的setPreferredSize
    Dimension D = lblTest.getSize();
    lblTest.setPreferredSize(D);

检查下面的完整工作代码:(第一个更改在这里实现,第二个更改被注释掉)

    import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import java.util.Date;
public class LabelSizeTest extends javax.swing.JFrame {
    JScrollBar vertical;
  public LabelSizeTest() {
    initComponents();
  }
  private void initComponents() {
    lblTest = new javax.swing.JLabel();
    btnTest = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


    lblTest.setText("<html><body style='width:200 px'>test asd asdf asdf asdf asdf asdf asdf asdf asdf end");
    lblTest.setName("lblTest"); // NOI18N

    btnTest.setText("Click Me");
    btnTest.setName("btnTest"); // NOI18N
    btnTest.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnTestActionPerformed(evt);
      }
    });
    jScrollPane1.setName("jScrollPane1"); // NOI18N
    vertical = jScrollPane1.getVerticalScrollBar();
    jList1.setModel(new javax.swing.AbstractListModel() {
      String[] strings = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
      public int getSize() { return strings.length; }
      public Object getElementAt(int i) { return strings[i]; }
    });
    jList1.setName("jList1"); // NOI18N
    jScrollPane1.setViewportView(jList1);
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
          .addGroup(layout.createSequentialGroup()
            .addComponent(btnTest)
            .addGap(0, 165, Short.MAX_VALUE))
          .addComponent(jScrollPane1))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btnTest)
        .addContainerGap())
    );
    pack();

        //Dimension D = lblTest.getSize();
        //System.out.println(D);
        //lblTest.setPreferredSize(D);
  }
  private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {  
    lblTest.setText(lblTest.getText() + "x  dsfsd  sdfsdf  dfgdfg  dfgdfg  dfgdfg  dgfdgf  dfgfg  fgdfgf  fgdg");
    vertical.setValue( vertical.getMaximum());
    pack();
  }                                       
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new LabelSizeTest().setVisible(true);
      }
    });
  }
  private javax.swing.JButton btnTest;
  private javax.swing.JList jList1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JLabel lblTest;
}

相关内容

  • 没有找到相关文章

最新更新