我正在使用 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);
}