| | |
| | | public class LegendDialog extends JDialog { |
| | | |
| | | public LegendDialog(Component parent, Color ignoredThemeColor) { |
| | | super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, |
| | | "图例", true); |
| | | // 使用统一宽度,高度缩减为一半 |
| | | initializeDialog(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT / 2); |
| | | 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); |
| | | |
| | | 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; |
| | | } |
| | | } |