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

标签 java swing button row gridbaglayout

我是 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/

相关文章:

java - 如何防止用户输入数值

Android:如何使用可滚动 TextView 制作当前布局?

android - 每个按钮的有效性、Android 开关或点击监听器

java - 如何将字符串中的所有数字移动到字符串的开头?

java - 在二叉搜索树中查找落在范围内的数据值并按升序打印它们

java - 如何每秒将图像绘制到帧上?

java - 将项目添加到 netbeans 表格中的组合框

html - 如何使用 CSS 更改备用按钮的颜色?

java - 将 ppt 转换为 html

java - 直接从 XML 获取孙子元素