如果您只是想使用圆形按钮的图像,那么为什么不使用JLabel呢?也就是说,只需调用
setIcon(...),将BufferedImage实例作为参数传递.
码
public final class RoundedButtonDemo {
private static BufferedImage bi;
public static void main(String[] args){
try {
loadImage();
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
createAndShowGUI();
}
});
} catch (IOException e) {
// handle exception
}
}
private static void loadImage() throws IOException{
bi = ImageIO.read(RoundedButtonDemo.class.getResource("../resources/login.png"));
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setIcon(new ImageIcon(bi));
frame.getContentPane().add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
OUTPUT
请注意,您需要以编程方式使图像的背景透明,或者您需要使用像Paint.NET这样的图像编辑工具.