Java 卸下JButton边框并在内部安装ImageButton
我有一个网格布局,其中添加了100个JButton。现在我要求按钮之间没有任何边距或间隙。我尝试的是:Java 卸下JButton边框并在内部安装ImageButton,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,我有一个网格布局,其中添加了100个JButton。现在我要求按钮之间没有任何边距或间隙。我尝试的是: private void initializeBoard() { container = getContentPane(); container.setLayout(new GridLayout(1,2)); boardPanel = new JPanel(new GridLayout(10,10)); dataPanel = new JPanel();
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);
}
private void initializeBoard(){
container=getContentPane();
container.setLayout(新的GridLayout(1,2));
boardPanel=新JPanel(新网格布局(10,10));
dataPanel=newjpanel();
int指数=0;
对于(int i=0;i您还可以使用setVgap(0)、setHgap(0)将垂直和水平间隙设置为零
干杯!您还可以使用setVgap(0)、setHgap(0)将垂直和水平间隙设置为零
干杯!明白了。我使用的图像不够宽,无法填充这些区域。谢谢。明白了。我使用的图像不够宽,无法填充这些区域。谢谢。您很可能在某个地方设置了大小,而不允许容纳JButton网格的容器达到其首选大小。我已在bot中添加了完整的类汤姆:我的问题。你能检查并确认我哪里做错了吗?去掉所有的设置并调用pack()
在添加所有组件后,在您的主JFrame上查看发生了什么。明白了。我使用的图像不够宽。谢谢。您很可能在某个地方设置了大小,不允许容纳JButton网格的容器达到其首选大小。我已在问题的底部添加了完整的类。请检查并确认rm我哪里做错了?去掉所有setSize并调用pack()
在添加所有组件后,在主JFrame上,查看发生了什么。明白了。我使用的图像不够宽。谢谢。JButton没有setVgap或setVerticalGap方法。您能详细说明一下吗?@sdewan64:GridLayout有这些属性,但默认情况下它们应该已经为零。JButton没有setVgap或setVerticalGap方法。您能详细说明一下吗?@sdewan64:GridLayout有这些属性,但默认情况下它们应该已经为零。