| | |
| | | import java.awt.*; |
| | | |
| | | public class LegendDialog extends JDialog { |
| | | private final Color THEME_COLOR; |
| | | |
| | | public LegendDialog(Component parent, Color themeColor) { |
| | | super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, |
| | | "图例", true); |
| | | this.THEME_COLOR = themeColor; |
| | | // 使用统一对话框尺寸(6.5 寸竖屏) |
| | | initializeDialog(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT); |
| | | |
| | | public LegendDialog(Component parent, Color ignoredThemeColor) { |
| | | super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, |
| | | "图例", true); |
| | | int adjustedWidth = (int) Math.round(UIConfig.DIALOG_WIDTH * 0.8); |
| | | int adjustedHeight = (int) Math.round(UIConfig.DIALOG_HEIGHT * 0.4); |
| | | initializeDialog(adjustedWidth, adjustedHeight); |
| | | initializeLegendContent(); |
| | | if (parent == null) { |
| | | setLocationRelativeTo(null); // 居中显示 |
| | |
| | | mainPanel.setBackground(Color.WHITE); |
| | | mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 10, 15)); |
| | | |
| | | // 标题 - 修改为"图例" |
| | | JLabel titleLabel = new JLabel("图例", JLabel.CENTER); |
| | | titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14)); |
| | | titleLabel.setForeground(new Color(60, 60, 60)); |
| | | titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); |
| | | mainPanel.add(titleLabel, BorderLayout.NORTH); |
| | | // 计算图例内容面板的宽度(用于设置图标尺寸) |
| | | // 图例对话框宽度 = DIALOG_WIDTH * 0.8 |
| | | // 主面板左右边框各15像素,图例内容面板左右内边距各10像素 |
| | | int adjustedWidth = (int) Math.round(UIConfig.DIALOG_WIDTH * 0.8); |
| | | int iconSize = adjustedWidth - 30 - 20; // 减去主面板左右边框(15*2)和图例内容面板左右内边距(10*2) |
| | | |
| | | // 创建割草机图标面板 |
| | | JPanel iconPanel = new JPanel(new BorderLayout()); |
| | | iconPanel.setBackground(Color.WHITE); |
| | | iconPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); // 底部间距10像素 |
| | | |
| | | JLabel gecaojiLabel = new JLabel(); |
| | | gecaojiLabel.setHorizontalAlignment(SwingConstants.CENTER); |
| | | ImageIcon gecaojiIcon = loadIcon("image/gecaoji.png", iconSize, iconSize); |
| | | if (gecaojiIcon != null) { |
| | | gecaojiLabel.setIcon(gecaojiIcon); |
| | | } else { |
| | | // 如果图标加载失败,显示占位文本 |
| | | gecaojiLabel.setText("割草机图标"); |
| | | gecaojiLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12)); |
| | | } |
| | | iconPanel.add(gecaojiLabel, BorderLayout.CENTER); |
| | | |
| | | // 图例内容面板 - 直接添加,不使用滚动条 |
| | | JPanel contentPanel = new JPanel(); |
| | |
| | | contentPanel.remove(contentPanel.getComponentCount() - 1); |
| | | } |
| | | |
| | | // 直接添加内容面板,不使用滚动条 |
| | | // 添加图标面板和图例内容面板 |
| | | mainPanel.add(iconPanel, BorderLayout.NORTH); |
| | | mainPanel.add(contentPanel, BorderLayout.CENTER); |
| | | |
| | | // 底部按钮面板 |
| | | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| | | buttonPanel.setBackground(Color.WHITE); |
| | | buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0)); |
| | | |
| | | // 美化关闭按钮 |
| | | JButton closeButton = new JButton("关闭") { |
| | | @Override |
| | | protected void paintComponent(Graphics g) { |
| | | Graphics2D g2 = (Graphics2D) g.create(); |
| | | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| | | |
| | | // 绘制圆角矩形背景 |
| | | g2.setColor(THEME_COLOR); |
| | | g2.fillRoundRect(0, 0, getWidth(), getHeight(), 15, 15); |
| | | |
| | | // 绘制边框 |
| | | g2.setColor(THEME_COLOR.darker()); |
| | | g2.setStroke(new BasicStroke(1.5f)); |
| | | g2.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15); |
| | | |
| | | // 绘制文字 |
| | | g2.setColor(Color.WHITE); |
| | | g2.setFont(getFont()); |
| | | FontMetrics fm = g2.getFontMetrics(); |
| | | int x = (getWidth() - fm.stringWidth(getText())) / 2; |
| | | int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent(); |
| | | g2.drawString(getText(), x, y); |
| | | |
| | | g2.dispose(); |
| | | } |
| | | }; |
| | | |
| | | closeButton.setFont(new Font("微软雅黑", Font.BOLD, 12)); |
| | | closeButton.setForeground(Color.WHITE); |
| | | closeButton.setBackground(THEME_COLOR); |
| | | closeButton.setFocusPainted(false); |
| | | closeButton.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15)); |
| | | closeButton.setContentAreaFilled(false); |
| | | closeButton.setPreferredSize(new Dimension(70, 28)); |
| | | closeButton.addActionListener(e -> dispose()); |
| | | |
| | | // 添加鼠标悬停效果 |
| | | closeButton.addMouseListener(new java.awt.event.MouseAdapter() { |
| | | public void mouseEntered(java.awt.event.MouseEvent evt) { |
| | | closeButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | } |
| | | public void mouseExited(java.awt.event.MouseEvent evt) { |
| | | closeButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
| | | } |
| | | }); |
| | | |
| | | buttonPanel.add(closeButton); |
| | | mainPanel.add(buttonPanel, BorderLayout.SOUTH); |
| | | |
| | | getContentPane().add(mainPanel); |
| | | } |
| | | |
| | |
| | | |
| | | return itemPanel; |
| | | } |
| | | |
| | | /** |
| | | * 加载并缩放图标 |
| | | * @param iconPath 图标路径 |
| | | * @param width 目标宽度 |
| | | * @param height 目标高度 |
| | | * @return 缩放后的图标 |
| | | */ |
| | | private ImageIcon loadIcon(String iconPath, int width, int height) { |
| | | try { |
| | | java.net.URL imgURL = getClass().getClassLoader().getResource(iconPath); |
| | | if (imgURL == null) { |
| | | // 尝试从文件系统加载 |
| | | java.io.File imgFile = new java.io.File(iconPath); |
| | | if (imgFile.exists()) { |
| | | ImageIcon originalIcon = new ImageIcon(imgFile.getAbsolutePath()); |
| | | Image scaledImage = originalIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH); |
| | | ImageIcon scaledIcon = new ImageIcon(scaledImage); |
| | | scaledIcon.setDescription(iconPath); |
| | | return scaledIcon; |
| | | } |
| | | } else { |
| | | ImageIcon originalIcon = new ImageIcon(imgURL); |
| | | Image scaledImage = originalIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH); |
| | | ImageIcon scaledIcon = new ImageIcon(scaledImage); |
| | | scaledIcon.setDescription(iconPath); |
| | | return scaledIcon; |
| | | } |
| | | } catch (Exception e) { |
| | | System.err.println("无法加载图标: " + iconPath + " - " + e.getMessage()); |
| | | } |
| | | return null; |
| | | } |
| | | } |