jbutton设置圆角矩形
时间: 2024-05-30 21:13:10 浏览: 12
好的,您可以通过设置按钮的边框形状来实现圆角矩形。具体操作如下:
1.创建按钮对象
JButton button = new JButton("按钮");
2.设置按钮的边框
button.setBorder(BorderFactory.createRoundRectBorder(10, 10));
其中,边框的第一个参数和第二个参数分别表示 X 轴和 Y 轴的圆角半径,您可以根据需要进行调整。
3.将按钮添加到容器中并显示出来
例如:
JFrame frame = new JFrame("圆角矩形按钮");
frame.getContentPane().add(button);
frame.setVisible(true);
希望我的回答能够帮助您,如果还有其他问题,可以随时向我提出。
相关问题
jbutton设置边框为圆角矩形
您可以使用下面这段代码来设置JButton的边框为圆角矩形:
```
jbutton.setBorder(new RoundBorder(20));
```
其中,RoundBorder是自定义的一个类,代码如下:
```
import java.awt.*;
import java.awt.geom.*;
import javax.swing.border.*;
public class RoundBorder implements Border {
private int radius;
public RoundBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(this.radius + 1, this.radius + 1, this.radius + 2, this.radius);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(c.getBackground());
g2.fill(new RoundRectangle2D.Double(x, y, width - 1, height - 1, radius, radius));
g2.setColor(c.getForeground());
g2.draw(new RoundRectangle2D.Double(x, y, width - 1, height - 1, radius, radius));
}
}
```
这样,您就可以把JButton的边框设置为圆角矩形了。
JButton设置圆角
JButton可以通过设置其Border来实现圆角效果。
以下是一个示例代码:
```
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class RoundedButton extends JButton {
public RoundedButton(String text) {
super(text);
setBackground(Color.WHITE);
setForeground(Color.BLACK);
setBorder(new RoundedBorder(10));
}
private class RoundedBorder implements Border {
private int radius;
public RoundedBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.setColor(getForeground());
g.drawRoundRect(x, y, width-1, height-1, radius, radius);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rounded Button Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
JPanel panel = new JPanel(new FlowLayout());
panel.add(new RoundedButton("Click me!"));
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
```
该代码中,我们定义了一个RoundedButton类,继承自JButton。在构造函数中,我们设置了按钮的背景和前景色,并且将按钮的边框设置为一个自定义的RoundedBorder类。
RoundedBorder类实现了Border接口,用于绘制圆角边框。在getBorderInsets方法中,我们返回一个Insets对象,用于指定边框的大小和位置。在paintBorder方法中,我们使用Graphics对象绘制圆角边框。
最后,在main方法中,我们创建了一个JFrame和一个JPanel,并将一个RoundedButton添加到该JPanel中,最后将JPanel添加到JFrame中。运行该程序,可以看到一个带有圆角边框的按钮。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)