gpt4 book ai didi

java - 如何设置与按钮之间的间距不同的边距间隙?

转载 作者:行者123 更新时间:2023-12-01 07:18:31 27 4
gpt4 key购买 nike

我有一个按钮面板,我希望按钮之间的间距为 25 像素,右边距为 5 像素,左边距为 5 像素(右侧的按钮位于窗口边框的 5 像素处)。

流式布局在各处设置了相同大小的间隙。 Gridlayout 允许这样做,但是所有按钮都具有相同的大小,这不是我想要的。我找到的唯一解决方案是将 Flow 布局设置为 hgap=0。然后我有一个emptyMargin,并且每个按钮之前都有一个刚性区域,但我认为这个解决方案是一个不好的做法。

最好的解决方案是什么?

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;

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

public class FlowLayoutDemo extends JFrame{
FlowLayout experimentLayout = new FlowLayout(FlowLayout.LEFT, 25, 0);

public FlowLayoutDemo(String name) {
super(name);
}

public void addComponentsToPane(final Container pane) {
final JPanel compsToExperiment = new JPanel();
compsToExperiment.setLayout(experimentLayout);
experimentLayout.setAlignment(FlowLayout.TRAILING);
compsToExperiment.add(new JButton("Button 1"));
compsToExperiment.add(new JButton("Button 2"));
compsToExperiment.add(new JButton("Button 3"));
compsToExperiment.add(new JButton("Long-Named Button 4"));
compsToExperiment.add(new JButton("5"));
pane.add(compsToExperiment, BorderLayout.CENTER);
}

private static void createAndShowGUI() {
FlowLayoutDemo frame = new FlowLayoutDemo("FlowLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

最佳答案

考虑使用 GridBagLayout 来代替,它提供了更多的控制和自定义。

Buttons

参见How to use GridBagLayout了解更多详情。

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.insets = new Insets(5, 5, 5, 25);
gbc.fill = gbc.HORIZONTAL;
gbc.weightx = 1;

add(new JButton("Button 1"), gbc);
gbc.insets = new Insets(5, 0, 5, 25);
add(new JButton("Button 2"), gbc);
add(new JButton("Button 3"), gbc);
add(new JButton("Long-Named Button 4"), gbc);
gbc.insets = new Insets(5, 0, 5, 5);
add(new JButton("5"), gbc);
}

}

}

请注意,该示例强制按钮也占据所有可用空间。如果这不能满足您的具体需求,请尝试使用 fillweightx

关于java - 如何设置与按钮之间的间距不同的边距间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48825612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com