Java 如何在GridBagLayout的左上角单元格中放置包含三个按钮的面板?
我有一个包含三个按钮的面板。我希望将此面板放置在Java 如何在GridBagLayout的左上角单元格中放置包含三个按钮的面板?,java,swing,Java,Swing,我有一个包含三个按钮的面板。我希望将此面板放置在GridBagLayout的左上角单元格中 我的问题是,当我运行程序时,面板不是位于左上角的单元格中,而是位于布局的中间。我已将gridx和gridy设置为零 private JFrame frame; private JMenuBar menuBar; private JMenu menuFile; private JMenuItem fileItem1; private JMenuItem fileItem2; private JPanel
GridBagLayout
的左上角单元格中
我的问题是,当我运行程序时,面板不是位于左上角的单元格中,而是位于布局的中间。我已将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);
}
尝试将默认值作为“中心定位”的约束
直接从
如果组件的显示区域大于组件本身,则可以使用GridBagConstraints.anchor
约束指定组件在显示区域中的位置
锚定约束的值可以是绝对的(北、南、东、西等),也可以是相对的方向(在页面开始、行结束、第一行开始等),或者相对于组件的基线
注意:
weightx
和weighty
以正确使用anchor
约束
GridBagConstraints.anchor
约束指定组件在显示区域中的位置
锚定约束的值可以是绝对的(北、南、东、西等),也可以是相对的方向(在页面开始、行结束、第一行开始等),或者相对于组件的基线
注意:
weightx
和weighty
以正确使用anchor
约束