问题描述
这是我试图复制的图片
这就是我所拥有的(还没有添加图标图像)
我似乎无法找到解决方案,我已经盯着它看了很长时间。
我正在尝试复制以下图片,使用 GridLayout
作为按钮,并使用 Java Swing 自行找出其余部分。此外,我已将按钮添加到 JPanel
中,现在我正在尝试在面板和窗格之间添加间距。
这就是我所拥有的,我该怎么办?
super(title);
this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.PAGE_AXIS));
Container pane = this.getContentPane();
JButton b1 = new JButton();
b1.setBackground(Color.white);
JButton b2 = new JButton();
b2.setBackground(Color.white);
JButton b3 = new JButton();
b3.setBackground(Color.white);
JButton b4 = new JButton();
b4.setBackground(Color.white);
JButton b5 = new JButton();
b5.setBackground(Color.white);
JButton b6 = new JButton();
b6.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,10,10));
panel.setBackground(Color.black);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
pane.add(panel);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloSEOperation(EXIT_ON_CLOSE);
解决方法
最简单的方法是在您的 JPanel (see this post on empty borders) 中添加一个空边框:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2,10,10));
// ...
panel.setBorder(new EmptyBorder(50,50,50));
另一种好方法(始终取决于您的应用程序需要),如果您设置了 JButton 首选大小,则将主 JPanel 的网格布局设置为两列一行,每列内有另一个 JPanel。将 Y_AXIS 模式下的 BoxLayout 添加到内部 JPanels 并将按钮与 setAlignmentX() 对齐也可以很好地工作(注意这种方法不会使 JButton 垂直居中)(请参阅 How to use BoxLayout):
public class MyFrame extends JFrame {
private String title = "Title";
public MyFrame(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,10));
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.Y_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.Y_AXIS));
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
JButton b1 = new JButton();
b1.setBackground(Color.white);
//b1.setIcon(new ImageIcon(img1));
b1.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b1);
JButton b2 = new JButton();
b2.setBackground(Color.white);
//b2.setIcon(new ImageIcon(img2));
b2.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b2);
JButton b3 = new JButton();
b3.setBackground(Color.white);
//b3.setIcon(new ImageIcon(img3));
b3.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b3);
JButton b4 = new JButton();
b4.setBackground(Color.white);
//b4.setIcon(new ImageIcon(img4));
b4.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b4);
JButton b5 = new JButton();
b5.setBackground(Color.white);
//b5.setIcon(new ImageIcon(img5));
b5.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b5);
JButton b6 = new JButton();
b6.setBackground(Color.white);
//b6.setIcon(new ImageIcon(img6));
b6.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b6);
add(mainPanel); //Adding our mainPanel to the contentPane of the JFrame
this.setSize(500,500); //or pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
}
}
,
这是我制作的一个小演示。
所有 Swing 应用程序都必须以调用 SwingUtilities
invokeLater
方法开始。此方法可确保在 Event Dispatch Thread 上创建和执行所有 Swing 组件。
您没有设置 JFrame
的大小,而是尝试使 Swing 组件适合。您让 JFrame
包含所有 Swing 组件。
您在 GridLayout
JPanel
内创建了一个 FlowLayout
JPanel
。 FlowLayout
JPanel
使用适当大小的空边框。
我使用 OP 提供的图像来获取图标。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class EmptySpaceDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new EmptySpaceDemo());
}
private Image[] images;
public EmptySpaceDemo() {
this.images = createImages();
}
private Image[] createImages() {
BufferedImage image = readImage();
Image[] images = new Image[6];
images[0] = image.getSubimage(155,113,110,90);
images[1] = image.getSubimage(276,90);
images[2] = image.getSubimage(155,217,90);
images[3] = image.getSubimage(276,90);
images[4] = image.getSubimage(155,321,90);
images[5] = image.getSubimage(276,90);
return images;
}
@Override
public void run() {
JFrame frame = new JFrame("Empty Space Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(),BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.BLACK);
panel.setBorder(BorderFactory.createEmptyBorder(40,100,40,100));
JPanel innerPanel = new JPanel(new GridLayout(0,10));
innerPanel.setBackground(Color.BLACK);
for (int i = 0; i < images.length; i++) {
JButton button = new JButton(new ImageIcon(images[i]));
innerPanel.add(button);
}
panel.add(innerPanel);
return panel;
}
private BufferedImage readImage() {
try {
return ImageIO.read(getClass().getResourceAsStream("/icons.png"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}