私は3つのボタンを含むパネルを持っています。このパネルをGridBagLayout
の左上のセルに配置します。GridBagLayoutの左上のセルに3つのボタンを含むパネルを配置する方法は?
私の問題は、プログラムを実行するとパネルが左上のセルに配置されていないが、レイアウトの途中にあるということです。 gridx
とgridy
の両方をゼロに設定しました。
private JFrame frame;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenuItem fileItem1;
private JMenuItem fileItem2;
private JPanel btnPanel;
private JButton btnRewind;
private JButton btnPlayPause;
private JButton btnFastForward;
private static boolean shouldFill = true;
private static boolean shouldWeightX = true;
private static boolean RIGHT_TO_LEFT = false;
public JPlayer() {
}
public void createAndShowGUI() {
frame = new JFrame();
frame.setTitle("JPlayer");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setVisible(true);
}
private void addComponentsToPane(Container pane) {
if(RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
if(shouldFill) {
gbc.fill = GridBagConstraints.HORIZONTAL;
}
btnRewind = new JButton();
try {
Image imgRewind = ImageIO.read(getClass().getResource("../utils/images/rewind.png"));
btnRewind.setIcon(new ImageIcon(imgRewind));
btnRewind.setOpaque(true);
btnRewind.setContentAreaFilled(false);
btnRewind.setBorderPainted(false);
} catch(Exception e) {
e.printStackTrace();
}
btnPlayPause = new JButton();
try {
Image imgPlay = ImageIO.read(getClass().getResource("../utils/images/play.png"));
Image imgPause = ImageIO.read(getClass().getResource("../utils/images/pause.png"));
btnPlayPause.setIcon(new ImageIcon(imgPlay));
btnPlayPause.setOpaque(true);
btnPlayPause.setContentAreaFilled(false);
btnPlayPause.setBorderPainted(false);
} catch(Exception e) {
e.printStackTrace();
}
btnFastForward = new JButton();
try {
Image imgFastForward = ImageIO.read(getClass().getResource("../utils/images/fast_forward.png"));
btnFastForward.setIcon(new ImageIcon(imgFastForward));
btnFastForward.setOpaque(true);
btnFastForward.setContentAreaFilled(false);
btnFastForward.setBorderPainted(false);
} catch(Exception e) {
e.printStackTrace();
}
btnPanel = new JPanel();
btnPanel.add(btnRewind);
btnPanel.add(btnPlayPause);
btnPanel.add(btnFastForward);
btnPanel.setSize(new Dimension(40, 40));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
JButton btn = new JButton("Some Button");
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(btnPanel, gbc);
pane.add(btn, c);
}