You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中JTattoo实现无内部线条圆形按钮问题求助

解决JTattoo AluminiumLookAndFeel圆形按钮内部线条问题

方案1:自定义ButtonUI重写绘制逻辑

JTattoo的AluminiumButtonUI默认会绘制内部装饰线条,我们可以继承该UI类,完全自定义按钮的绘制流程,跳过内部线条的绘制:

import com.jtattoo.plaf.aluminium.AluminiumButtonUI;
import javax.swing.*;
import java.awt.*;

public class CustomRoundButtonUI extends AluminiumButtonUI {
    @Override
    public void paint(Graphics g, JComponent c) {
        JButton btn = (JButton) c;
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制圆形背景,根据按钮状态调整颜色
        Color bgColor = btn.getBackground();
        if (btn.getModel().isPressed()) {
            bgColor = bgColor.darker();
        } else if (btn.getModel().isRollover()) {
            bgColor = bgColor.brighter();
        }
        g2.setColor(bgColor);
        g2.fillOval(0, 0, btn.getWidth() - 1, btn.getHeight() - 1);

        // 绘制按钮文本
        FontMetrics fm = g2.getFontMetrics();
        String text = btn.getText();
        int textX = (btn.getWidth() - fm.stringWidth(text)) / 2;
        int textY = (btn.getHeight() + fm.getAscent()) / 2 - fm.getDescent();
        g2.setColor(btn.getForeground());
        g2.drawString(text, textX, textY);

        g2.dispose();
    }
}

给按钮绑定自定义UI:

JButton roundBtn = new JButton("1");
roundBtn.setUI(new CustomRoundButtonUI());
roundBtn.setBorder(BorderFactory.createEmptyBorder());
roundBtn.setPreferredSize(new Dimension(60, 60));

方案2:修改UIManager属性禁用内部装饰

在设置LookAndFeel后,通过UIManager关闭按钮的内部边框和焦点边框,避免默认线条绘制:

UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
// 清空按钮的内部边框和焦点边框
UIManager.put("Button.innerBorder", BorderFactory.createEmptyBorder());
UIManager.put("Button.focusBorder", BorderFactory.createEmptyBorder());

然后设置按钮为圆形:

JButton roundBtn = new JButton("1");
roundBtn.setBorder(BorderFactory.createEmptyBorder());
roundBtn.setShape(new java.awt.geom.Ellipse2D.Double(0, 0, 59, 59)); // 尺寸比按钮小1,防止边缘截断
roundBtn.setPreferredSize(new Dimension(60, 60));

方案3:禁用默认绘制,自定义paintComponent

直接禁用按钮的默认内容填充和边框绘制,重写paintComponent方法完全控制外观:

JButton roundBtn = new JButton("1") {
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制圆形背景,根据按钮状态调整颜色
        Color bgColor = getBackground();
        if (getModel().isPressed()) {
            bgColor = bgColor.darker();
        } else if (getModel().isRollover()) {
            bgColor = bgColor.brighter();
        }
        g2.setColor(bgColor);
        g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

        // 调用父类方法绘制文本和图标
        super.paintComponent(g);
        g2.dispose();
    }
};
roundBtn.setBorderPainted(false);
roundBtn.setContentAreaFilled(false);
roundBtn.setFocusPainted(false); // 移除焦点框
roundBtn.setPreferredSize(new Dimension(60, 60));

内容的提问来源于stack exchange,提问作者Ali si

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 11:54:22