BufferedImage image = new BufferedImage(x,y,i,j);
JButton button = new JButton();想把button的背景图设为 image,用什么方法可以实现哦?
button..setIcon()可以吗?GUI高手们帮帮小弟吧
JButton button = new JButton();想把button的背景图设为 image,用什么方法可以实现哦?
button..setIcon()可以吗?GUI高手们帮帮小弟吧
调试欢乐多
private static final long serialVersionUID = 1965063150601339314L;
Image image = new ImageIcon("v4.png").getImage(); public ImageButton(String text) {
super(text);
setOpaque(false);
setFocusable(false);
setBorderPainted(false); this.setPreferredSize(new Dimension(image.getWidth(this), image
.getHeight(this)));
} @Override
protected void paintComponent(Graphics g) {
g.drawImage(image,
0,
0,
null);
}
}
BufferedImage resizedImg = new BufferedImage(width, heigth,
BufferedImage.TYPE_INT_RGB); Image image = resizedImg.getScaledInstance(resizedImg.getWidth(), resizedImg
.getHeight(), Image.SCALE_DEFAULT); //先把它转换成Image Icon icon = new ImageIcon(image); JButton button = new JButton(icon);
import java.awt.image.*;
import javax.swing.*;
public class MainPanel {
private Matrix matrix = new Matrix(4,4);
private BufferedImage bufferedImage;
private SubImg subImg = new SubImg();
private ImageIcon image = new ImageIcon();
private JButton blocks[][] ;
private JPanel panelOfPuzzle;
private JPanel panelOfImg;
public MainPanel () {
//这一步一定要记得*********
blocks = new JButton[matrix.getRow()][matrix.getCal()];
panelOfPuzzle = new JPanel();
panelOfPuzzle.setLayout(new FlowLayout( FlowLayout.CENTER) );
panelOfPuzzle.setLayout(new GridLayout(4,4) );
int Count = matrix.getRow()*matrix.getCal();
for(int i=0;i<=3;i++){
for(int j=0;j<=3;j++){
blocks[i][j] = new JButton( Integer.toString( Count-- ) );
panelOfPuzzle.add(blocks[i][j]);
}
}//end outside for
BufferedImage resizedImg = subImg.getSubImage(2, 2);
//运行到这里就出问题了
Image image = resizedImg.getScaledInstance(
resizedImg.getWidth(),
resizedImg.getHeight(),
Image.SCALE_DEFAULT); //先把它转换成Image Icon icon = new ImageIcon(image);
blocks[2][2].setIcon(icon);
}//end constructor
public JPanel gethePanelOfPuzzle(){
return panelOfPuzzle;
}
public JPanel gethePanelOfImg(){
return panelOfImg;
}
}//end class MainPanel