繁体   English   中英

Java GUI:如何使用GridBagLayout连续有多个按钮

[英]Java GUI: How do I have more than one button on a row using 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);
   }
}

尝试以下代码:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

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;

        JPanel demoPanel = new JPanel();

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

        mainPanel.add(demoPanel);

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

我只是将两个按钮放在JPanel中,然后将JPanel放在GridBagLayout面板中!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM