【问题标题】:How can I make a Jbutton fill a JPanel so that there is no space between buttons?如何让 Jbutton 填充 JPanel 以便按钮之间没有空格?
【发布时间】:2012-11-13 03:49:51
【问题描述】:

我正在做这样的事情:

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

产生:

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

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

【问题讨论】:

    标签: java swing jpanel jbutton layout-manager


    【解决方案1】:

    首先通过constructor 指定GridLayout 的垂直(和水平)间距。

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

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

    那么你应该已经准备好了。

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

    更新了各个面板

    面板的默认布局是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);
    }
    

    【讨论】:

    • 感谢您的快速响应,我尝试实现此代码,但因为我的 JButton 都在各自的 JPanel 中,我仍然无法删除不需要的间距。
    • 为什么他们需要在自己的窗格中?
    • 我正在使用 CardLayout 在我的原始按钮和另一个面板之间切换。
    • 是的,我只需要为每个面板设置布局。非常感谢您的帮助
    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2011-07-04
    相关资源
    最近更新 更多