簡體   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