GridBagLayout 在按钮/交错/可变列之间添加空间?
我正在尝试使用gridbaglayout
创建一个布局,但是在jbutton
控件之间的空间很难相等。在第一行中,有5个按钮之间没有空间。第二行和第三行应具有4个按钮,这些按钮之间的空间介于两者之间。
我尝试过的解决方法是使其成为35x35网格,顶部按钮具有宽度7和其他按钮宽度5。我不知道如何使4个按钮均匀对齐(最后一列中的空间较小)。
代码下面:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
public class MainTwo {
public static void main(String[] a) {
final JColorChooser colorChooser = new JColorChooser();
MyChooserPanel newChooser = new MyChooserPanel();
colorChooser.addChooserPanel(newChooser);
ActionListener okActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(colorChooser.getColor());
}
};
ActionListener cancelActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Cancel");
}
};
final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
okActionListener, cancelActionListener);
dialog.setVisible(true);
}
}
class MyChooserPanel extends AbstractColorChooserPanel {
public void buildChooser() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.ipadx = 21;
c.ipady = 15;
c.gridwidth = 7;
makeAddButton("Black", Color.BLACK, c);
makeAddButton("Dark Grey", Color.PINK, c);
makeAddButton("Custom PathVisio Grey", Color.YELLOW, c);
makeAddButton("Grey", Color.CYAN, c);
makeAddButton("White", Color.WHITE, c);
c.ipadx = 15;
c.gridwidth = 5;
c.insets = new Insets(10,0,0,0);
c.gridy = 1; // new row
makeAddButton("Blue", Color.BLUE, c);
c.gridx = 10;
makeAddButton("Green", Color.GREEN, c);
c.gridx = 20;
makeAddButton("Purple", Color.MAGENTA, c);
c.gridx = 30;
makeAddButton("Orange", Color.ORANGE, c);
c.gridy = 2; // new row
c.gridx = 0;
makeAddButton("Dark Blue", Color.BLUE, c);
c.gridx = 10;
makeAddButton("Dark Green", Color.GREEN, c);
c.gridx = 20;
makeAddButton("Dark Purple", Color.MAGENTA, c);
c.gridx = 30;
makeAddButton("Dark Orange", Color.ORANGE, c);
}
public void updateChooser() {
}
public String getDisplayName() {
return "Panel";
}
public Icon getSmallDisplayIcon() {
return null;
}
public Icon getLargeDisplayIcon() {
return null;
}
private void makeAddButton(String name, Color color, GridBagConstraints c) {
JButton button = new JButton(name);
button.setBackground(color);
button.setBorderPainted(false);
button.setOpaque(true);
button.setAction(setColorAction);
button.setToolTipText(name);
add(button, c);
}
Action setColorAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
JButton button = (JButton) evt.getSource();
getColorSelectionModel().setSelectedColor(button.getBackground());
}
};
}
I am trying to create a layout with GridBagLayout
but am having trouble getting the spaces in between the JButton
controls to be equal. In the first row, there are 5 buttons that have no space in between. The second and third row should have 4 buttons which are smaller which spaces in between.
The workaround I tried was to make it a 35x35 grid where the top buttons have width 7 and the other buttons width 5. I cannot figure out how to get the 4 buttons to align evenly (The space in the last column is smaller).
Code below:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
public class MainTwo {
public static void main(String[] a) {
final JColorChooser colorChooser = new JColorChooser();
MyChooserPanel newChooser = new MyChooserPanel();
colorChooser.addChooserPanel(newChooser);
ActionListener okActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(colorChooser.getColor());
}
};
ActionListener cancelActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Cancel");
}
};
final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
okActionListener, cancelActionListener);
dialog.setVisible(true);
}
}
class MyChooserPanel extends AbstractColorChooserPanel {
public void buildChooser() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.ipadx = 21;
c.ipady = 15;
c.gridwidth = 7;
makeAddButton("Black", Color.BLACK, c);
makeAddButton("Dark Grey", Color.PINK, c);
makeAddButton("Custom PathVisio Grey", Color.YELLOW, c);
makeAddButton("Grey", Color.CYAN, c);
makeAddButton("White", Color.WHITE, c);
c.ipadx = 15;
c.gridwidth = 5;
c.insets = new Insets(10,0,0,0);
c.gridy = 1; // new row
makeAddButton("Blue", Color.BLUE, c);
c.gridx = 10;
makeAddButton("Green", Color.GREEN, c);
c.gridx = 20;
makeAddButton("Purple", Color.MAGENTA, c);
c.gridx = 30;
makeAddButton("Orange", Color.ORANGE, c);
c.gridy = 2; // new row
c.gridx = 0;
makeAddButton("Dark Blue", Color.BLUE, c);
c.gridx = 10;
makeAddButton("Dark Green", Color.GREEN, c);
c.gridx = 20;
makeAddButton("Dark Purple", Color.MAGENTA, c);
c.gridx = 30;
makeAddButton("Dark Orange", Color.ORANGE, c);
}
public void updateChooser() {
}
public String getDisplayName() {
return "Panel";
}
public Icon getSmallDisplayIcon() {
return null;
}
public Icon getLargeDisplayIcon() {
return null;
}
private void makeAddButton(String name, Color color, GridBagConstraints c) {
JButton button = new JButton(name);
button.setBackground(color);
button.setBorderPainted(false);
button.setOpaque(true);
button.setAction(setColorAction);
button.setToolTipText(name);
add(button, c);
}
Action setColorAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
JButton button = (JButton) evt.getSource();
getColorSelectionModel().setSelectedColor(button.getBackground());
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将这个GUI作为一个包含两个网格布局的边框布局。
I'd approach this GUI as one border layout containing two grid layouts.
通常,我反对使用多个布局来实现目标,但在这种情况下,我建议将其分成 2 个面板。这是一个示例:
Normally, I'm against using multiple layouts to achieve a goal, but in the case, I'll suggest splitting it up into 2 panels. Here's an example: