删除 JButton 边框并在内部安装 ImageButton
Removing JButton Border and Fitting ImageButton inside
我有一个网格布局,我在上面添加了 100 个 Jbuttons.Now 我要求按钮之间没有任何边距或间隙 them.What 我在这里试过:
private void initializeBoard() {
container = getContentPane();
container.setLayout(new GridLayout(1,2));
boardPanel = new JPanel(new GridLayout(10,10));
dataPanel = new JPanel();
int index = 0;
for(int i=0;i<BOARD_SIZE;i++){
for(int j=0;j<BOARD_SIZE;j++){
squares[i][j] = new JButton(String.valueOf(numbers[index]));
squares[i][j].addActionListener(this);
squares[i][j].setMargin(new Insets(0,0,0,0));
squares[i][j].setBorder(BorderFactory.createEmptyBorder());
squares[i][j].setBorderPainted(false);
squares[i][j].setContentAreaFilled(false);
squares[i][j].setFocusPainted(true);
squares[i][j].setIcon(new ImageIcon("images/board/"+numbers[index++]+".jpg"));
boardPanel.add(squares[i][j]);
}
}
boardPanel.setBorder(BorderFactory.createEmptyBorder());
container.add(boardPanel);
container.add(dataPanel);
}
但结果就像 this.I 按钮之间仍然存在垂直间隙。
我需要消除这些间隙,这样按钮之间就没有间隙,并让按钮代表整个面板。
提前致谢。
编辑
我使用的 it.The 个图像不够宽,无法填满那些 areas.Thanks。
您还可以使用 setVgap(0)、setHgap(0) 将垂直和水平间隙设置为零。
干杯!
得到 it.The 我使用的图片不够宽,无法填满 areas.Thanks。
我有一个网格布局,我在上面添加了 100 个 Jbuttons.Now 我要求按钮之间没有任何边距或间隙 them.What 我在这里试过:
private void initializeBoard() {
container = getContentPane();
container.setLayout(new GridLayout(1,2));
boardPanel = new JPanel(new GridLayout(10,10));
dataPanel = new JPanel();
int index = 0;
for(int i=0;i<BOARD_SIZE;i++){
for(int j=0;j<BOARD_SIZE;j++){
squares[i][j] = new JButton(String.valueOf(numbers[index]));
squares[i][j].addActionListener(this);
squares[i][j].setMargin(new Insets(0,0,0,0));
squares[i][j].setBorder(BorderFactory.createEmptyBorder());
squares[i][j].setBorderPainted(false);
squares[i][j].setContentAreaFilled(false);
squares[i][j].setFocusPainted(true);
squares[i][j].setIcon(new ImageIcon("images/board/"+numbers[index++]+".jpg"));
boardPanel.add(squares[i][j]);
}
}
boardPanel.setBorder(BorderFactory.createEmptyBorder());
container.add(boardPanel);
container.add(dataPanel);
}
但结果就像 this.I 按钮之间仍然存在垂直间隙。
我需要消除这些间隙,这样按钮之间就没有间隙,并让按钮代表整个面板。
提前致谢。
编辑 我使用的 it.The 个图像不够宽,无法填满那些 areas.Thanks。
您还可以使用 setVgap(0)、setHgap(0) 将垂直和水平间隙设置为零。
干杯!
得到 it.The 我使用的图片不够宽,无法填满 areas.Thanks。