package zhuye; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.RoundRectangle2D; import javax.swing.JButton; import javax.swing.border.EmptyBorder; /** * 提供统一按钮样式的工厂方法(美化版)。 * 风格与 ObstacleManagementPage 保持一致:圆角、扁平化、抗锯齿。 */ public final class buttonset { // 默认按钮尺寸 private static final Dimension DEFAULT_SIZE = new Dimension(90, 36); // 圆角半径 private static final int CORNER_RADIUS = 8; private buttonset() { // 工具类不需要实例化 } /** * 创建带有现代化统一样式的按钮(圆角、扁平风格)。 * * @param text 按钮显示文本 * @param backgroundColor 按钮主色调(如果为null,默认使用灰蓝色) * @return 已应用样式的 JButton */ public static JButton createStyledButton(String text, Color backgroundColor) { // 确定基础颜色,如果未指定则使用默认的钢蓝色 final Color baseColor = backgroundColor != null ? backgroundColor : new Color(70, 130, 180); // 创建自定义绘制的按钮 JButton button = new JButton(text) { private static final long serialVersionUID = 1L; @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g.create(); // 开启抗锯齿,使圆角和文字更平滑 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 获取按钮状态 boolean isPressed = getModel().isPressed(); boolean isRollover = getModel().isRollover(); int w = getWidth(); int h = getHeight(); // 计算当前背景色 if (isPressed) { g2.setColor(baseColor.darker()); // 按下变深 } else if (isRollover) { // 悬停时稍微透明或变亮,增加交互感 g2.setColor(new Color(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), 220)); } else { g2.setColor(baseColor); // 正常颜色 } // 绘制圆角矩形背景 g2.fill(new RoundRectangle2D.Double(0, 0, w, h, CORNER_RADIUS, CORNER_RADIUS)); // 如果需要在按下时有轻微的边框效果(可选,增加立体感) if (isPressed) { g2.setColor(new Color(0, 0, 0, 30)); g2.draw(new RoundRectangle2D.Double(0.5, 0.5, w - 1, h - 1, CORNER_RADIUS, CORNER_RADIUS)); } // 绘制文字 g2.setColor(Color.WHITE); g2.setFont(getFont()); FontMetrics fm = g2.getFontMetrics(); // 精确计算文字居中位置 int textX = (w - fm.stringWidth(getText())) / 2; // 注意:y坐标是基线位置,需要加上 Ascent 并减去高度的一半 int textY = (h - fm.getHeight()) / 2 + fm.getAscent(); g2.drawString(getText(), textX, textY); g2.dispose(); } }; // 基础属性设置 button.setFont(new Font("微软雅黑", Font.BOLD, 14)); button.setForeground(Color.WHITE); button.setFocusPainted(false); // 去除焦点虚线框 button.setContentAreaFilled(false); // 去除默认背景绘制(由paintComponent接管) button.setBorderPainted(false); // 去除默认边框 button.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 鼠标变成手型 // 设置内边距(虽然主要由paintComponent控制,但对布局计算有帮助) button.setBorder(new EmptyBorder(8, 18, 8, 18)); // 尺寸计算逻辑 Dimension preferred = button.getPreferredSize(); // 确保宽度不小于默认值,且根据文字长度自适应 int width = Math.max(preferred.width + 20, DEFAULT_SIZE.width); int height = Math.max(preferred.height, DEFAULT_SIZE.height); Dimension finalSize = new Dimension(width, height); button.setPreferredSize(finalSize); button.setMinimumSize(DEFAULT_SIZE); button.setMaximumSize(finalSize); // 防止在某些布局中被过度拉伸 return button; } }