我想在Java中创建圆形的JButton...为此,我使用了圆形图像并将其放置在按钮上,但我没有获得圆形的按钮。
请问有人可以告诉我如何在Java中创建像下面的圆形按钮吗?
提前致谢......
请问有人可以告诉我如何在Java中创建像下面的圆形按钮吗?
提前致谢......
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);
}
}
请注意,您需要通过编程使图像的背景透明,或者使用像Paint.NET这样的图像编辑工具。
您可以使用JavaFX定义“丰富的图形组件”示例(带有渐变的圆形按钮):http://poligloci.blogspot.com/2009/07/beauty-and-beast-javafx-12-in-netbeans.html