将宽度固定为GridBagLayout

Rom*_*dgz 4 java swing layout-manager gridbaglayout

在此输入图像描述

我正在使用GridBagLayout创建一个看起来像图片中的StatusBar.我有4个区域,所以我在第一个区域有一个按钮,然后在第二个区域有信息消息,然后我又想要两个(我还有第五个用于制作角落).

按钮区域非常适合,因为内容始终是具有相同宽度的按钮.与角落区域相同.信息区域必须获得所有可用空间.第3和第4个区域必须具有固定值,与屏幕大小无关.

我怎样才能做到这一点?

我目前的代码是:

public MyStatusBar() {
        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(getWidth(), 23));
        GridBagConstraints c = new GridBagConstraints();

        botonDispositivo = new JButton("");
        this.setText(0, "Disconnected");
        URL imageUrl = getClass().getResource("resources/22x22/dispositivo01.png");
        this.setButtonImg(imageUrl);
        this.setButtonEnabled(false);


        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(botonDispositivo, c);

        c.insets= new Insets(0,10,0,0);
        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.7;
        c.fill = GridBagConstraints.HORIZONTAL;
        msgLabel = new JLabel("");
        msgLabel.setPreferredSize(new Dimension(500, msgLabel.getHeight()));
        this.add(msgLabel, c);
        this.setText(1, "Waiting for potentiostat conection");

        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.NONE;
        this.add(new SeparatorPanel(Color.GRAY, Color.WHITE), c);

        c.gridx ++;
        c.gridy = 0;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;
        overErrorLabel = new JLabel("");
        overErrorLabel.setSize(new Dimension(150, overErrorLabel.getHeight()));
        this.add(overErrorLabel, c);
        //this.setText(2, "");

        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.NONE;
        this.add(new SeparatorPanel(Color.GRAY, Color.WHITE), c);

        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.EAST;
        timeLabel = new JLabel("");
        timeLabel.setMinimumSize(new Dimension(150, timeLabel.getHeight()));
        //timeLabel.setMaximumSize(new Dimension(150, timeLabel.getHeight()));
        this.add(timeLabel, c);
        //this.setText(3, "");


        JPanel rightPanel = new JPanel(new GridBagLayout());
        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.BOTH;
        rightPanel.add(new JLabel(new AngledLinesWindowsCornerIcon()), c);
        rightPanel.setOpaque(false);

        c.gridx ++;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        this.add(rightPanel, c);

        setBackground(SystemColor.control);
      }
Run Code Online (Sandbox Code Playgroud)

Sta*_*avL 12

您可以为不同的标签设置首选,最小,最大等大小.我认为你需要的只是设置权重X> 0并填充param = HORIZONTAL约束你要调整大小的标签和weightX = 0和fill-NONE为你不想调整大小的标签.

还可以使用ipadx指定标签的最小尺寸.


mKo*_*bel 4

为了更好、最简单地输出到 GUI,最好寻找Borderlayout

JPanel#setPrefferedSize(200, 30);对于JButtonwithIcon和 JLabel 放入WEST area

JPanel#setPrefferedSize(200, 30); (右面板)到EAST area

把另一个JPanel放到CENTER area