首页javalayoutJava Swing - 如何使用GridBagLayout在中心的任一侧对齐2套/项目...

Java Swing - 如何使用GridBagLayout在中心的任一侧对齐2套/项目...

我们想知道如何使用GridBagLayout在中心的任一侧对齐2套/项目。...
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Main {
  JFrame frame = new JFrame("Test");
  JPanel panel = new JPanel();
  JButton subscribe = new JButton("Subscribe");
  JButton unsubscribe = new JButton("Unsubscribe");
  JButton refresh = new JButton("Refresh");
  JButton save = new JButton("Save");

  public Main() {
    panel.setLayout(new GridBagLayout());
    panel.add(subscribe);
    panel.add(unsubscribe);
    panel.add(refresh);
    panel.add(save);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
  public static void main(String[] args) {
    new Main();
  }
}