簡體   English   中英

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

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

這是我要復制的圖片1

這就是我所擁有的(尚未添加圖標圖像)2

我似乎無法找到解決方案,一直盯着它很長一段時間。

我正在嘗試復制下圖,使用GridLayout作為按鈕,並使用 Java Swing 自行計算 rest。 此外,我已將按鈕添加到JPanel中,現在我正在嘗試在面板和窗格之間添加間距。

這就是我所擁有的,我怎么能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);

最簡單的方法是為您的 JPanel 添加一個空邊框( 請參閱這篇關於空邊框的帖子):

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

另一種好方法(始終取決於您的應用程序需要),如果您設置了 JButton 首選大小,則將主 JPanel 的網格布局設置為具有兩列和一行,每列內有另一個 JPanel。 在 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);
    }

}

這是我制作的一個小演示。

空白空間演示

所有 Swing 應用程序必須從調用SwingUtilitiesinvokeLater方法開始。 此方法確保所有 Swing 組件都在Event Dispatch Thread上創建和執行。

您不設置JFrame的大小並嘗試使 Swing 組件適合。 您讓JFrame與所有 Swing 組件一起打包。

您在FlowLayout JPanel內創建一個GridLayout 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, 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