简体   繁体   English

如何在 JPanel 和 JFrame 的 contentPane 之间添加间距?

[英]How to add spacing between JPanel and JFrame's contentPane?

This is the picture I am trying to replicate这是我要复制的图片1

This is what I have (didn't add icon images yet)这就是我所拥有的(尚未添加图标图像)2

I can't seem to find a solution, been staring at it for quite some time.我似乎无法找到解决方案,一直盯着它很长一段时间。

I am trying to replicate the following picture, using GridLayout for the buttons and the figure out the rest on my own using Java Swing.我正在尝试复制下图,使用GridLayout作为按钮,并使用 Java Swing 自行计算 rest。 Furthermore, I've added my buttons into a JPanel and now I'm trying to add spacing between the panel and the pane.此外,我已将按钮添加到JPanel中,现在我正在尝试在面板和窗格之间添加间距。

This is what I have, how can I go about it?这就是我所拥有的,我怎么能go一下呢?

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);

The easiest way to do it would be to add an empty border to your JPanel ( see this post on empty borders ):最简单的方法是为您的 JPanel 添加一个空边框( 请参阅这篇关于空边框的帖子):

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// ...
panel.setBorder(new EmptyBorder(50, 50, 50, 50));

Another good approach (depending always on your application needs), if you have the JButton preferred size set, would be to have the main JPanel's grid layout set to have two columns and one row, with another JPanel inside each column.另一种好方法(始终取决于您的应用程序需要),如果您设置了 JButton 首选大小,则将主 JPanel 的网格布局设置为具有两列和一行,每列内有另一个 JPanel。 Adding to the interior JPanels a BoxLayout in Y_AXIS mode and aligning the buttons with setAlignmentX() would work great too (note this approach wouldn't center the JButtons vertically) (seeHow to use BoxLayout ):在 Y_AXIS 模式下向内部 JPanel 添加一个 BoxLayout 并使用 setAlignmentX() 对齐按钮也可以很好地工作(注意这种方法不会垂直居中 JButtons)(请参阅如何使用 BoxLayout ):

public class MyFrame extends JFrame {

    private String title = "Title";

    public MyFrame(){

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(1,2,10,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);
    }

}

Here's a little demonstration I whipped up.这是我制作的一个小演示。

空白空间演示

All Swing applications must start with a call to the SwingUtilities invokeLater method.所有 Swing 应用程序必须从调用SwingUtilitiesinvokeLater方法开始。 This method ensures that all Swing components are created and executed on the Event Dispatch Thread .此方法确保所有 Swing 组件都在Event Dispatch Thread上创建和执行。

You don't set the size of the JFrame and try and make the Swing components fit.您不设置JFrame的大小并尝试使 Swing 组件适合。 You let the JFrame pack with all the Swing components.您让JFrame与所有 Swing 组件一起打包。

You create a GridLayout JPanel inside of a FlowLayout JPanel .您在FlowLayout JPanel内创建一个GridLayout JPanel The FlowLayout JPanel uses an empty border of the appropriate size. FlowLayout JPanel使用适当大小的空边框。

I used the image the OP provided to get the icons.我使用 OP 提供的图像来获取图标。

图标

Here's the complete runnable code.这是完整的可运行代码。

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, 113, 110, 90); 
        images[2] = image.getSubimage(155, 217, 110, 90); 
        images[3] = image.getSubimage(276, 217, 110, 90); 
        images[4] = image.getSubimage(155, 321, 110, 90); 
        images[5] = image.getSubimage(276, 321, 110, 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, 2, 10, 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;
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM