package zhuye; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.BorderFactory; import javax.swing.JButton; /** * 提供统一按钮样式的工厂方法。 */ public final class buttonset { private static final Dimension DEFAULT_SIZE = new Dimension(90, 36); private buttonset() { // 工具类不需要实例化 } /** * 创建带有统一样式的按钮,供多个界面复用。 * * @param text 按钮显示文本 * @param backgroundColor 按钮背景色 * @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); button.setFont(new Font("微软雅黑", Font.BOLD, 14)); button.setBackground(baseColor); button.setForeground(Color.WHITE); button.setFocusPainted(false); button.setBorder(BorderFactory.createEmptyBorder(8, 18, 8, 18)); Dimension preferred = button.getPreferredSize(); int width = Math.max(preferred.width, DEFAULT_SIZE.width); int height = Math.max(preferred.height, DEFAULT_SIZE.height); Dimension adjustedSize = new Dimension(width, height); button.setPreferredSize(adjustedSize); button.setMinimumSize(new Dimension(DEFAULT_SIZE.width, DEFAULT_SIZE.height)); button.setMaximumSize(adjustedSize); button.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseEntered(java.awt.event.MouseEvent event) { button.setBackground(brightenColor(baseColor)); } @Override public void mouseExited(java.awt.event.MouseEvent event) { button.setBackground(baseColor); } }); return button; } private static Color brightenColor(Color color) { if (color == null) { return new Color(200, 200, 200); } int r = Math.min(255, color.getRed() + 30); int g = Math.min(255, color.getGreen() + 30); int b = Math.min(255, color.getBlue() + 30); return new Color(r, g, b); } }