java 如何在java中创建圆形JButton ..?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5120116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create rounded JButton in java..?
提问by mani
I want to create rounded JButton in Java...
For that I use rounded image and placed that image on button but I didn't get rounded button..
我想在 Java 中创建圆形 JButton ......
为此,我使用圆形图像并将该图像放在按钮上,但我没有得到圆形按钮......
please any one can tell how to create rounded button in Java like show in below figure..
请任何人都可以告诉如何在 Java 中创建圆形按钮,如下图所示..
thanks in advance.....
提前致谢.....
回答by mre
If you're just going to use an image of a round button, then why not just use a JLabel
? That is, simply invoke setIcon(...)
, passing your BufferedImage
instance as an argument.
如果您只想使用圆形按钮的图像,那么为什么不使用JLabel
?也就是说,只需调用setIcon(...)
,将您的BufferedImage
实例作为参数传递。
CODE
代码
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
输出
Keep in mind that you'll need to either programmatically make the background of your image transparent, or you'll need to use an image editing tool like Paint.NET.
请记住,您需要以编程方式使图像的背景透明,或者需要使用像Paint.NET这样的图像编辑工具。
回答by Aaron Digulla
回答by Kozio?ek
You can use JavaFX to define "Rich Graphic Components" example (rounded button with gradient): http://poligloci.blogspot.com/2009/07/beauty-and-beast-javafx-12-in-netbeans.html
您可以使用 JavaFX 来定义“丰富的图形组件”示例(带渐变的圆形按钮):http: //poligloci.blogspot.com/2009/07/beauty-and-beast-javafx-12-in-netbeans.html