利用可用的布局管理器。详情请见Laying Out Components Within a Container
为简单起见,我只使用了GridBagLayout,因为默认情况下这会使容器内的组件居中
而且你可以免费获得可抗性
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(16, 16, 16, 16));
setLayout(new GridBagLayout());
add(new JButton("Login"));
add(new JButton("Sign Up"));
}
}
}
现在,如果您希望按钮相同,则可以使用 GridLayout 之类的方式使其工作,
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(32, 32, 32, 32));
setLayout(new GridBagLayout());
JPanel buttonPane = new JPanel(new GridLayout(1, -1));
buttonPane.add(new JButton("This is a long button"));
buttonPane.add(new JButton("Sign Up"));
add(buttonPane);
}
}
}
或者您可以使用自定义布局管理器,例如...
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.LayoutManager2;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(32, 32, 32, 32));
setLayout(new ButtonLayoutManager());
add(new JButton("This is a long button"));
add(new JButton("Sign Up"));
}
}
public class ButtonLayoutManager implements LayoutManager2 {
private int horizontalPadding = 0;
@Override
public void addLayoutComponent(Component comp, Object constraints) {
}
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void removeLayoutComponent(Component comp) {
}
@Override
public void invalidateLayout(Container target) {
}
@Override
public Dimension maximumLayoutSize(Container target) {
return preferredLayoutSize(target);
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
@Override
public Dimension preferredLayoutSize(Container parent) {
int height = 0;
int width = 0;
for (Component comp : parent.getComponents()) {
height = Math.max(comp.getPreferredSize().height, height);
width = Math.max(comp.getPreferredSize().width, width);
}
width = (width * parent.getComponentCount()) + (horizontalPadding * parent.getComponentCount() - 1);
Insets insets = parent.getInsets();
width += insets.left + insets.right;
height += insets.top + insets.bottom;
return new Dimension(width, height);
}
@Override
public float getLayoutAlignmentX(Container target) {
return 0.5f;
}
@Override
public float getLayoutAlignmentY(Container target) {
return 0.5f;
}
@Override
public void layoutContainer(Container parent) {
int width = parent.getWidth();
int height = parent.getHeight();
Insets insets = parent.getInsets();
int maxWidth = 0;
int maxHeight = 0;
for (Component comp : parent.getComponents()) {
maxWidth = Math.max(comp.getPreferredSize().width, maxWidth);
maxHeight = Math.max(comp.getPreferredSize().height, maxHeight);
}
int padding = (horizontalPadding * parent.getComponentCount() - 1);
int totalWidth = padding + (maxWidth * parent.getComponentCount());
int yOffset = (height - maxHeight) / 2;
int xOffset = (width - totalWidth) / 2;
for (Component comp : parent.getComponents()) {
comp.setBounds(xOffset, yOffset, maxWidth, maxHeight);
xOffset += horizontalPadding + maxWidth;
}
}
}
}
nb:我没有对此进行广泛的测试,仅用于演示目的