gpt4 book ai didi

java - 如何将 2 个 JButtons 放置在 JPanel 的顶部(一个居中,另一个位于右上角)?

转载 作者:行者123 更新时间:2023-12-01 20:51:08 24 4
gpt4 key购买 nike

我想在我的 JPanel 上放置 2 个 JButton,其中一个位于居中,另一个位于 JPanel 的右上角。 JPanel 的大小与包含 JPanel 的 JFrame 的大小相同。如何使用 GridBagLayout 和 GridBagConstraints 执行此操作?

public class MyPanel extends JPanel {
public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");

setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.weighty = 1;
c.gridx = 0;
add(btnGame1, c);
c.gridx = 1;
c.anchor = GridBagConstraints.FIRST_LINE_END;
add(btnExitFrame, c);
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

camickr 的 MCVE

public class MyPanel extends JPanel {
static JFrame frame = new JFrame();

public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");;

setLayout(new BorderLayout());

JPanel top = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
top.add( btnExitFrame );

JPanel center = new JPanel(new GridBagLayout());
center.add(btnGame1, new GridBagConstraints());

add(top, BorderLayout.PAGE_START);
add(center, BorderLayout.CENTER);
}

public static void main(String[] args) {
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

固定解决方案:

public class MyPanel extends JPanel {
static JFrame frame = new JFrame();

public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");;

int nOfCells = 3;
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
for(int i = 0; i < nOfCells; i++){
for(int j = 0; j < nOfCells; j++){
c.gridx = i;
c.gridy = j;
if(i == 1 && j == 0) {
c.anchor = c.PAGE_START;
add(btnGame1, c);
c.anchor = c.CENTER;
}
else if(i == 2 && j == 0) {
c.anchor = c.FIRST_LINE_END;
add(btnExitFrame, c);
c.anchor = c.CENTER;
}
else {
c.fill = c.BOTH;
add(Box.createRigidArea(new Dimension(frame.getWidth()/nOfCells, frame.getHeight()/nOfCells)), c);
c.fill = c.NONE;
}
}
}

}

public static void main(String[] args) {
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

image of the fixed text box

最佳答案

仅使用 GridBagLayout 的另一个选项是添加不可见的框作为填充物,将 JPanel 划分为多个部分,然后用 JButton 替换您首选位置中的框。

    GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
for(int i=0; i<nOfCells; i++){
for(int k=0; k<nOfCells; k++){

c.gridx = i;
c.gridy= k;
this.add(Box.createRigidArea(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)), c);
}
}

GridBagLayout 不允许在空间中固定位置,它们取决于相对于 JFrame 的其他组件的位置。

关于java - 如何将 2 个 JButtons 放置在 JPanel 的顶部(一个居中,另一个位于右上角)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43482899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com