简体   繁体   English

防止GridBagLayout调整列大小

[英]prevent GridBagLayout from resizing columns

Another problem with swing. 秋千的另一个问题。 How can I stop GridBagLayout from respacing components if one of them changes size? 如果其中一个组件更改了大小,如何停止GridBagLayout重新设置组件的大小? For example, I have few columns, in one of which there is a JLabel with text "text". 例如,我的列很少,其中一列是带有文本“ text”的JLabel。 When I change it to "texttext" layout manager resizes the whole column. 当我将其更改为“ texttext”时,布局管理器将调整整个列的大小。 I don't want it to do that. 我不希望它那样做。 Is there any way to prevent it? 有什么办法可以防止呢?

Example: 例:

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);
  }
 }

It is visible while 9 -> 10 or anytime text changes width. 在9-> 10或随时更改宽度时可见。

GridBagLayout ignores maximumWidth/Height. GridBagLayout忽略maximumWidth / Height。 There's not an easy way to set the maximum size of the JLabel. 没有简单的方法来设置JLabel的最大大小。

But, what I think you really want is the layout to not shift when the text in the JLabel changes. 但是,我认为您真正想要的是当JLabel中的文本更改时,布局不移位。

That can be done by making the JLabel wide enough to hold the largest value it needs to display. 这可以通过使JLabel足够宽以容纳其需要显示的最大值来实现。 For example: 例如:

    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);

Update: 更新:

To center the label text, just add: 要使标签文本居中,只需添加:

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

This might work: 这可能起作用:

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);
  }
}

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

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