gpt4 book ai didi

Java 图形用户界面 : How do I have more than one button on a row using GridBagLayout

转载 作者:行者123 更新时间:2023-11-29 03:24:03 25 4
gpt4 key购买 nike

我是 java 的新手,我在尝试连续放置多个按钮时遇到了问题,目前我正在将两个按钮添加到面板,但我不知道如何分隔它们的 x 位置,即它们都直接添加到彼此之上。

我是否需要创建一个新布局,或者我是否可以使用我目前拥有的布局找到解决方案?

public class Question1 {
public static void main (String[] args){
MyFrame f = new MyFrame("Simple Submit Cancel Form");
f.init();
}
}

class MyFrame extends JFrame{
MyFrame(String title){
super(title);
}

private JPanel mainPanel;
private GridBagConstraints cText = new GridBagConstraints();
private GridBagConstraints cButton = new GridBagConstraints();
private GridBagLayout gbLayout = new GridBagLayout();

void init(){
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
this.setContentPane(mainPanel);

cText.anchor = GridBagConstraints.WEST;
cText.weightx = 0.0;
cText.gridx = 0;
cText.gridy = 0;

JTextField tf = new JTextField(20);
gbLayout.setConstraints(tf,cText);
mainPanel.add(tf);

cButton.gridwidth = 4;
cButton.weightx = 0.0;
cButton.gridx = 0;
cButton.gridy = 1;

JButton b = new JButton("Submit");
gbLayout.setConstraints(b,cButton);
mainPanel.add(b);
b = new JButton("Cancel");
gbLayout.setConstraints(b,cButton);
mainPanel.add(b);



this.pack();
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) ;
this.setVisible(true);
}

最佳答案

只需增加 gridx 值:

    JButton b = new JButton("Submit");
// gbLayout.setConstraints(b,cButton);
mainPanel.add(b, cButton);
b = new JButton("Cancel");
cButton.gridx++;
// gbLayout.setConstraints(b,cButton);
mainPanel.add(b, cButton);

您还需要使用在使用容器将组件添加到网格包布局时创建的约束。

例如,

import java.awt.*;
import javax.swing.*;

public class Question1 {
public static void main(String[] args) {
MyFrame f = new MyFrame("Simple Submit Cancel Form");
f.init();
}
}

class MyFrame extends JFrame {
private static final int GAP = 2;

MyFrame(String title) {
super(title);
}

private JPanel mainPanel;
private GridBagLayout gbLayout = new GridBagLayout();

void init() {
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
this.setContentPane(mainPanel);

GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(GAP, GAP, GAP, GAP);
gbc.gridwidth = 2;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;

JTextField tf = new JTextField(20);

mainPanel.add(tf, gbc);

gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;

JButton b = new JButton("Submit");
mainPanel.add(b, gbc);
b = new JButton("Cancel");
gbc.gridx++;
mainPanel.add(b, gbc);

this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

关于Java 图形用户界面 : How do I have more than one button on a row using GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22021571/

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