3

私はこのようなことをしています:

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() を使用したりしても、垂直方向の隙間をなくすことはできません。あるブロックの上部と別のブロックの下部の間には常にスペースがあります。

ここに画像の説明を入力

あるボタンの下部と次のボタンの上部の間のスペースを削除する方法を知っている人はいますか?

4

1 に答える 1

4

コンストラクター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ものが必要です。BorderLayoutGridBagLayout

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);
}
于 2012-11-13T04:06:38.920 に答える