JPanel 中的 GridBagLayout,组件不会更改 x || y 位置
GridBagLayout in JPanel, components don't change x || y positions
我正在尝试创建自己的 GUI,试图将 playerWins JLabel 移到最右边。我试过更改 x 和 y 坐标,但 JLabel 保持原样。我想知道这是否与将 JPanel 设置为 CENTRE 有关。
import javax.swing.*;
import java.awt.*;
public class DieIntGUI extends JFrame {
public DieIntGUI(String title) {
super(title);
setSize(700, 700);
getContentPane().setBackground(Color.white);
setLayout(new BorderLayout());
initComponents();
add(panel);
add(errorMessages, BorderLayout.SOUTH);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
DieIntGUI frame = new DieIntGUI("Dice Game");
frame.setVisible(true);
}
private void initComponents() {
panel = new JPanel();
errorMessages = new JLabel("T");
playerWins = new JLabel("F");
computerWins = new JLabel("S");
drawComponents();
}
private void drawComponents() {
GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(new GridBagLayout());
panel.setSize(700, 700);
panel.setBackground(Color.white);
gbc.gridx = 2;
gbc.gridy = 17;
panel.add(playerWins, gbc);
}
private JPanel panel;
private JLabel errorMessages;
public JLabel playerWins, computerWins;
}
这会将标签移到最右边。
public DieIntGUI(String title) {
super(title);
setSize(700, 700);
getContentPane().setBackground(Color.white);
setLayout(new BorderLayout());
initComponents();
add(panel, BorderLayout.EAST); // Move to right
add(errorMessages, BorderLayout.SOUTH);
setLocationRelativeTo(null);
}
输出:
是的,panel
被设置为 CENTER
因为在 BorderLayout 中,如果您不指定位置,它默认设置为 BorderLayout.CENTER
。
使用 GridBagLayout
的解决方案是。
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx=1; //Fill all space
gbc.anchor=GridBagConstraints.EAST; //align component to the EAST
我已将 x、y 设置为 1。重要的是要了解这些是相对于您添加的其他对象的索引。 (如果只有 1 个组件就没有意义,没有不可见的网格位置。)
我正在尝试创建自己的 GUI,试图将 playerWins JLabel 移到最右边。我试过更改 x 和 y 坐标,但 JLabel 保持原样。我想知道这是否与将 JPanel 设置为 CENTRE 有关。
import javax.swing.*;
import java.awt.*;
public class DieIntGUI extends JFrame {
public DieIntGUI(String title) {
super(title);
setSize(700, 700);
getContentPane().setBackground(Color.white);
setLayout(new BorderLayout());
initComponents();
add(panel);
add(errorMessages, BorderLayout.SOUTH);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
DieIntGUI frame = new DieIntGUI("Dice Game");
frame.setVisible(true);
}
private void initComponents() {
panel = new JPanel();
errorMessages = new JLabel("T");
playerWins = new JLabel("F");
computerWins = new JLabel("S");
drawComponents();
}
private void drawComponents() {
GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(new GridBagLayout());
panel.setSize(700, 700);
panel.setBackground(Color.white);
gbc.gridx = 2;
gbc.gridy = 17;
panel.add(playerWins, gbc);
}
private JPanel panel;
private JLabel errorMessages;
public JLabel playerWins, computerWins;
}
这会将标签移到最右边。
public DieIntGUI(String title) {
super(title);
setSize(700, 700);
getContentPane().setBackground(Color.white);
setLayout(new BorderLayout());
initComponents();
add(panel, BorderLayout.EAST); // Move to right
add(errorMessages, BorderLayout.SOUTH);
setLocationRelativeTo(null);
}
输出:
是的,panel
被设置为 CENTER
因为在 BorderLayout 中,如果您不指定位置,它默认设置为 BorderLayout.CENTER
。
使用 GridBagLayout
的解决方案是。
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx=1; //Fill all space
gbc.anchor=GridBagConstraints.EAST; //align component to the EAST
我已将 x、y 设置为 1。重要的是要了解这些是相对于您添加的其他对象的索引。 (如果只有 1 个组件就没有意义,没有不可见的网格位置。)