0

挥杆的另一个问题。如果其中一个更改大小,如何阻止 GridBagLayout 重新调整组件的间距?例如,我有几列,其中有一个带有文本“文本”的 JLabel。当我将其更改为“texttext”时,布局管理器会调整整个列的大小。我不希望它那样做。有什么办法可以预防吗?

例子:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class ResizeIssue {

 static int value = 99;

 public static void main(String[] args) {

    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(--value));
        }
    });

    JButton incButton = new JButton("+");
    incButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(++value));
        }
    });

  JPanel panel = new JPanel();
  panel.setLayout(new GridBagLayout());
  GridBagConstraints c = new GridBagConstraints();
  c.weightx = 1;
  c.gridx = 0;
  c.gridy = 0;
  panel.add(decButton, c);
  c.gridx = 1;
  panel.add(valueLabel, c);
  c.gridx = 2;
  panel.add(incButton, c);

  frame.add(panel);
  frame.pack();
  frame.setVisible(true);
  }
 }

它在 9 -> 10 或任何时候文本更改宽度时可见。

4

2 回答 2

2

GridBagLayout 忽略最大宽度/高度。设置 JLabel 的最大大小并不容易。

但是,我认为您真正想要的是当 JLabel 中的文本更改时布局不会发生变化。

这可以通过使 JLabel 足够宽以容纳它需要显示的最大值来完成。例如:

    jLabel1.setFont(new Font("monospace", Font.PLAIN, 12));
    FontMetrics fm = jLabel1.getFontMetrics(jLabel1.getFont());
    int w = fm.stringWidth("0000");
    int h = fm.getHeight();
    Dimension size = new Dimension(w, h);
    jLabel1.setMinimumSize(size);
    jLabel1.setPreferredSize(size);

更新:

要使标签文本居中,只需添加:

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
于 2012-05-27T18:16:45.650 回答
1

这可能有效:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ResizeIssue2 {
  static int value = 99;
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(--value));
        }
    });

    JButton incButton = new JButton("+");
    incButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(++value));
        }
    });

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 0;
    panel.add(decButton, c);
    c.gridx = 1;
    panel.add(valueLabel, c);
    c.gridx = 2;
    panel.add(incButton, c);

    //*
    c.gridy = 1; 
    int w = 32; //incButton.getPreferredSize().width;
    for(c.gridx=0;c.gridx<3;c.gridx++) {
      panel.add(Box.createHorizontalStrut(w), c);
    }
    // */

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}
于 2012-05-27T19:39:31.153 回答