JLabel在组布局中 - 冻结大小

时间:2014-05-22 15:37:11

标签: java swing jlabel grouplayout

我在组布局中有一个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;
    }

1 个答案:

答案 0 :(得分:0)

我在您的代码中进行了两处更改:(您可以使用其中任何一项) 第一个: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;
}