如何摆脱瓷砖之间的空隙?

时间:2017-01-20 12:11:40

标签: java netbeans

/ *  *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。  *要更改此模板文件,请选择“工具”|模板  *并在编辑器中打开模板。  * / 包org.me.images;

import java.awt。; import javax.swing。;

公共类JFrameTester {

public JPanel createContentPane() {

    JPanel panel = new JPanel();

    JPanel mainPanel = new JPanel(new GridLayout(8, 8, 0, 0));
    mainPanel.setBorder(null);
    JButton[] label = new JButton[64];
    for (int i = 0; i < 64; i++) {
        label[i] = new JButton();
        label[i].setSize(50, 50);
        label[i].setBorderPainted(false);
        label[i].setBorderPainted(false);
        label[i].setMargin(new Insets(0, 0, 0, 0));
        panel.add(label[i]);
        panel.setBorder(null);
        label[i].setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/images/O.png")));
        label[i].setPreferredSize(new Dimension(50, 50));
        label[i].setToolTipText("label" + i);
        label[i].setBorder(null);
        label[i].setBorderPainted(false);
        label[i].setMargin(new Insets(0, 0, 0, 0));

    }

    panel.add(mainPanel);
    panel.setOpaque(true);

    return panel;
}

public static void main(String[] args) {

    JFrame frame = new JFrame("Board");
    JFrameTester Display = new JFrameTester();

    //try here for layout space
    frame.setContentPane(Display.createContentPane());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.pack();
    frame.setVisible(true);
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    JFrame.setDefaultLookAndFeelDecorated(true);

}

} / **  *  * @author Alexa  * /

1 个答案:

答案 0 :(得分:1)

您需要将JButton添加到mainPanel而不是panel

删除了一些重复的行并没有删除必要的设置

for (int i = 0; i < 64; i++) {
    label[i] = new JButton();
    label[i].setBorderPainted(false);
    mainPanel.add(label[i]);
    label[i].setIcon(new ImageIcon(getClass()
        .getResource("/org/me/images/O.png")));
    label[i].setPreferredSize(new Dimension(50, 50));
    label[i].setToolTipText("label" + i);
}