简体   繁体   English

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

[英]How can I make a Jbutton fill a JPanel so that there is no space between buttons?

I'm doing something like this: 我正在做这样的事情:

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

Which produces: 哪个产生:

在此输入图像描述

No matter how I resize the image or use a frame.size() I can't get the vertical gaps to go away. 无论我如何调整图像大小或使用frame.size(),我都无法让垂直间隙消失。 There is always space between the top of one block and the bottom of another. 在一个块的顶部和另一个块的底部之间总是存在空间。

在此输入图像描述

Does anyone know how I can remove the spaces between the bottom of one button and the top of the next button? 有谁知道如何删除一个按钮底部和下一个按钮顶部之间的空格?

Start by specifying the vertical (and horizontial) spacing for GridLayout via the constructor . 首先通过构造函数指定GridLayout的垂直(和水平)间距。

Next, you need to strip away all the unrequired content of the buttons... 接下来,你需要删除按钮的所有不需要的内容......

jb1.setMargin(new Insets(0, 0, 0, 0));
jb1.setContentAreaFilled(false);
jb1.setFocusPainted(false);
jb1.setBorder(new EmptyBorder(0, 0, 0, 0));

Then you should be set. 然后你应该被设置。

在此输入图像描述

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

Updated with individual panels 更新了各个面板

The default layout for a panel is FlowLayout , you need something that isn't going to add more padding, something like BorderLayout or even GridBagLayout 面板的默认布局是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);
}

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

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