さて、GridBagLayoutをいじって、それを理解します。これを使用してJFrameでJPanelsをレイアウトしたいのですが、これらの個々のパネルは異なるレイアウトマネージャーを使用するため、GridBagLayoutはおそらく適切ではありません。
しかし、私には問題があります。パネルに何も追加しない場合、この例のようにすべての比率が完全に機能します。
public class RealMess extends JFrame{
private JPanel panel;
public RealMess(){
initUI();
}
public final void initUI(){
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screensize.getWidth();
int height = (int) screensize.getHeight();
panel = new JPanel();
panel.setLayout(new GridBagLayout());
JPanel left = new JPanel();
left.setBackground(Color.GREEN);
JPanel centre = new JPanel();
centre.setBackground(Color.PINK);
JPanel right = new JPanel();
right.setBackground(Color.CYAN);
JPanel leftBottom = new JPanel();
leftBottom.setBackground(Color.LIGHT_GRAY);
leftBottom.setLayout(new FlowLayout(0));
JPanel bottom = new JPanel();
bottom.setBackground(Color.MAGENTA);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0.11;
gbc.weighty = 0.9;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(left, gbc);
gbc.weightx = 0.11;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LAST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(leftBottom, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.9;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(centre, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(bottom, gbc);
gbc.weightx = 0.11;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_END;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
panel.add(right, gbc);
add(panel);
setTitle("Jack");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
RealMess rm = new RealMess();
rm.setVisible(true);
}
});
}
}
一部のGridBagConstraintsを不必要に再定義したことは知っていますが、何も見逃したくありませんでした。
とにかく、パネルのいずれかにボタンを追加すると、問題が発生します。このような:
public class RealMess extends JFrame{
private JPanel panel;
public RealMess(){
initUI();
}
public final void initUI(){
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screensize.getWidth();
int height = (int) screensize.getHeight();
panel = new JPanel();
panel.setLayout(new GridBagLayout());
JPanel left = new JPanel();
left.setBackground(Color.GREEN);
JPanel centre = new JPanel();
centre.setBackground(Color.PINK);
JPanel right = new JPanel();
right.setBackground(Color.CYAN);
JPanel leftBottom = new JPanel();
leftBottom.setBackground(Color.LIGHT_GRAY);
leftBottom.setLayout(new FlowLayout(0));
JButton rewind = new JButton("1");
rewind.setPreferredSize(new Dimension(50, 25));
leftBottom.add(rewind);
JPanel bottom = new JPanel();
bottom.setBackground(Color.MAGENTA);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0.11;
gbc.weighty = 0.9;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(left, gbc);
gbc.weightx = 0.11;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LAST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(leftBottom, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.9;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(centre, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(bottom, gbc);
gbc.weightx = 0.11;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_END;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
panel.add(right, gbc);
add(panel);
setTitle("Jack");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
RealMess rm = new RealMess();
rm.setVisible(true);
}
});
}
}
左下のパネルが大きくなり、それに関連するグリッドの正方形のサイズが大きくなりました。
最初の例では、すべてがウィンドウの幅または高さのパーセンテージを占めています。
gbc.weightx
と
gbc.weighty
このようにすると、すべてを正しいサイズにするのが非常に簡単になります。
パネルサイズが重量値のみに影響されるようにする回避策はありますか?