Swing JButton 背景色设置示例

package buttonDemo;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
 
public class CrystalButton extends JButton {  
 private String text;
    private float alpha = 1f; 
    private boolean isMouseEntered = false;
    private boolean isPressed = false;
 
   
    private Color COLOR_DARK_PURPLE = new Color(161, 123, 192);
    private Color COLOR_LIGHT_PURPLE = new Color(255, 228, 255);
    private Color COLOR_TEXT_DARKBLUE = new Color(90, 121,255);
  
    public CrystalButton(String buttonText) {  
       // super(buttonText);
        this.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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中。运行该程序,可以看到一个带有圆角边框的按钮。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值