| | |
| | | public class ButtonUtils { |
| | | |
| | | /** |
| | | * 创建蓝色样式按钮的通用方法 |
| | | * @param text 按钮文本 |
| | | * @return 配置好样式的JButton |
| | | * 创建蓝色样式按钮的通用方法 |
| | | * @param text 按钮文本 |
| | | * @return 配置好样式的JButton |
| | | */ |
| | | public static JButton createBlueButton(String text) { |
| | | return createBlueButton(text, -1); // 调用重载方法,默认不设置固定高度 |
| | | return createBlueButton(text, -1); // 调用重载方法,默认不设置固定高度 |
| | | } |
| | | |
| | | /** |
| | | * 创建蓝色样式按钮的通用方法(带高度参数) |
| | | * @param text 按钮文本 |
| | | * @param height 按钮高度(像素),-1表示使用默认高度 |
| | | * @return 配置好样式的JButton |
| | | * 创建蓝色样式按钮的通用方法(带高度参数) |
| | | * @param text 按钮文本 |
| | | * @param height 按钮高度(像素),-1表示使用默认高度 |
| | | * @return 配置好样式的JButton |
| | | */ |
| | | public static JButton createBlueButton(String text, int height) { |
| | | JButton button = new JButton(text); |
| | | button.setBackground(new Color(0, 120, 215)); // 蓝色背景 |
| | | button.setForeground(Color.WHITE); // 白色文字 |
| | | button.setBackground(new Color(0, 120, 215)); // 蓝色背景 |
| | | button.setForeground(Color.WHITE); // 白色文字 |
| | | button.setFocusPainted(false); |
| | | button.setOpaque(true); |
| | | button.setBorderPainted(false); |
| | | button.setFont(button.getFont().deriveFont(Font.BOLD)); // 加粗字体 |
| | | button.setFont(button.getFont().deriveFont(Font.BOLD)); // 加粗字体 |
| | | |
| | | // 如果指定了高度,设置固定高度 |
| | | // 如果指定了高度,设置固定高度 |
| | | if (height > 0) { |
| | | Dimension preferredSize = button.getPreferredSize(); |
| | | Dimension newSize = new Dimension(preferredSize.width, height); |
| | |
| | | button.setMaximumSize(newSize); |
| | | } |
| | | |
| | | // 添加鼠标悬停效果 |
| | | // 添加鼠标悬停效果 |
| | | button.addMouseListener(new MouseAdapter() { |
| | | @Override |
| | | public void mouseEntered(MouseEvent e) { |
| | | button.setBackground(Color.GRAY); // 鼠标悬停时变为灰色 |
| | | button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 鼠标变为手型 |
| | | button.setBackground(Color.GRAY); // 鼠标悬停时变为灰色 |
| | | button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 鼠标变为手型 |
| | | } |
| | | |
| | | @Override |
| | | public void mouseExited(MouseEvent e) { |
| | | button.setBackground(new Color(0, 120, 215)); // 鼠标离开时恢复蓝色 |
| | | button.setCursor(Cursor.getDefaultCursor()); // 鼠标恢复默认形状 |
| | | button.setBackground(new Color(0, 120, 215)); // 鼠标离开时恢复蓝色 |
| | | button.setCursor(Cursor.getDefaultCursor()); // 鼠标恢复默认形状 |
| | | } |
| | | }); |
| | | |