张世豪
2025-12-01 d23f280e37080cb9b5934350cc0fafb2c68421d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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("<html><center>" + icon + "<br>" + text + "</center></html>");
        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;
    }
}