package zhuye; import ui.UIConfig; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class AreaSelectionDialog extends JDialog { private final Color THEME_COLOR; private final JLabel areaNameLabel; private final JLabel statusLabel; public AreaSelectionDialog(Component parent, Color themeColor, JLabel areaNameLabel, JLabel statusLabel) { super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, "选择地块", true); this.THEME_COLOR = themeColor; this.areaNameLabel = areaNameLabel; this.statusLabel = statusLabel; // 统一对话框尺寸 initializeDialog(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT); initializeAreaContent(); if (parent == null) { setLocationRelativeTo(null); // 居中显示 } } private void initializeDialog(int width, int height) { setSize(width, height); setLocationRelativeTo(getParent()); setResizable(false); getContentPane().setBackground(Color.WHITE); } private void initializeAreaContent() { JPanel contentPanel = new JPanel(); contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); String[][] areaOptions = { {"🏡", "前院草坪"}, {"🌳", "后院草坪"}, {"🌷", "花园区域"} }; for (String[] area : areaOptions) { JButton areaBtn = createAreaButton(area[0], area[1]); contentPanel.add(areaBtn); contentPanel.add(Box.createRigidArea(new Dimension(0, 10))); } JButton addAreaBtn = new JButton("➕ 新增地块"); addAreaBtn.setFont(new Font("微软雅黑", Font.BOLD, 14)); addAreaBtn.setBackground(THEME_COLOR); addAreaBtn.setForeground(Color.WHITE); addAreaBtn.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0)); addAreaBtn.setFocusPainted(false); addAreaBtn.setAlignmentX(Component.CENTER_ALIGNMENT); addAreaBtn.addActionListener(e -> { JOptionPane.showMessageDialog(this, "打开新增地块功能"); }); contentPanel.add(Box.createVerticalGlue()); contentPanel.add(addAreaBtn); getContentPane().add(contentPanel); } private JButton createAreaButton(String icon, String text) { JButton button = new JButton("
" + icon + "
" + text + "
"); button.setFont(new Font("微软雅黑", Font.PLAIN, 14)); button.setPreferredSize(new Dimension(250, 80)); button.setBackground(Color.WHITE); button.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(10, 10, 10, 10) )); button.setFocusPainted(false); button.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { button.setBackground(new Color(240, 250, 240)); button.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(THEME_COLOR), BorderFactory.createEmptyBorder(10, 10, 10, 10) )); } public void mouseExited(MouseEvent e) { button.setBackground(Color.WHITE); button.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(10, 10, 10, 10) )); } }); button.addActionListener(e -> { areaNameLabel.setText(text); statusLabel.setText("待机"); this.dispose(); }); return button; } }