如何使 Jbutton 填充 JPanel 以使按钮之间没有空格?

Gra*_*min 3 java swing jpanel jbutton layout-manager

我正在做这样的事情:

JFrame frame = new JFrame();
frame.setSize(216, 272);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel jp = new JPanel();
GridLayout gl = new GridLayout(10, 2);
jp.setLayout(gl);

//this an an 18x18 png
Image img = ImageIO.read(new File("C:\\Users\\Josh\\workspace\\src\\red.png")); 

for(int ii=0; ii < 20; ii++)
{
    JPanel buttonPanel = new JPanel();
    JButton jb1 = new JButton(new ImageIcon(img));

    jb1.setBorder(BorderFactory.createEmptyBorder());

    buttonPanel.add(jb1);
    jp.add(buttonPanel);
}

frame.add(jp);
Run Code Online (Sandbox Code Playgroud)

其产生:

在此输入图像描述

无论我如何调整图像大小或使用frame.size(),我都无法消除垂直间隙。一个块的顶部和另一个块的底部之间始终存在空间。

在此输入图像描述

有谁知道如何删除一个按钮底部和下一个按钮顶部之间的空格?

Mad*_*mer 5

GridLayout首先通过构造函数指定垂直(和水平)间距。

接下来,您需要剥离按钮中所有不需要的内容......

jb1.setMargin(new Insets(0, 0, 0, 0));
jb1.setContentAreaFilled(false);
jb1.setFocusPainted(false);
jb1.setBorder(new EmptyBorder(0, 0, 0, 0));
Run Code Online (Sandbox Code Playgroud)

那么你应该已经设置好了。

在此输入图像描述

public class TestButtons {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ButtonPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ButtonPane extends JPanel {

        public ButtonPane() {

            setLayout(new GridLayout(10, 2, 0, 0));

            BufferedImage img = new BufferedImage(18, 18, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.RED);
            g2d.fillRect(0, 0, 18, 18);
            g2d.dispose();

            for (int ii = 0; ii < 20; ii++) {
                JButton jb1 = new JButton(new ImageIcon(img));
                jb1.setMargin(new Insets(0, 0, 0, 0));
//                jb1.setBorderPainted(false);
                jb1.setContentAreaFilled(false);
                jb1.setFocusPainted(false);
                jb1.setBorder(new EmptyBorder(0, 0, 0, 0));

                add(jb1);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新了单独的面板

面板的默认布局是FlowLayout,您需要一些不会添加更多填充的东西,BorderLayout例如GridBagLayout

for (int ii = 0; ii < 20; ii++) {
    JButton jb1 = new JButton(new ImageIcon(img));
    JPanel panel = new JPanel(new BorderLayout());
    jb1.setMargin(new Insets(0, 0, 0, 0));
    jb1.setContentAreaFilled(false);
    jb1.setFocusPainted(false);
    jb1.setBorder(new EmptyBorder(0, 0, 0, 0));

    panel.add(jb1);
    add(panel);
}
Run Code Online (Sandbox Code Playgroud)