java Map关联下拉列表item(地区选择)

此博客展示了一个用Java编写的信息录入与保存程序。程序创建了一个图形界面,包含姓名、详细地址、邮箱、性别、省份和城市等输入框和下拉框,用户可输入信息,点击保存按钮将信息保存到register.txt文件中,还可点击重置按钮重新输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class mapShowPlace extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mapShowPlace frame = new mapShowPlace();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public mapShowPlace() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        textField = new JTextField();
        textField.setBounds(92, 23, 195, 21);
        contentPane.add(textField);
        textField.setColumns(10);
        
        textField_1 = new JTextField();
        textField_1.setBounds(92, 54, 195, 21);
        contentPane.add(textField_1);
        textField_1.setColumns(10);
        
        textField_2 = new JTextField();
        textField_2.setBounds(92, 90, 195, 27);
        contentPane.add(textField_2);
        textField_2.setColumns(10);
        
        JLabel lblNewLabel = new JLabel("\u59D3\u540D");
        lblNewLabel.setBounds(10, 26, 54, 15);
        contentPane.add(lblNewLabel);
        
        JLabel lblNewLabel_1 = new JLabel("\u8BE6\u7EC6\u5730\u5740");
        lblNewLabel_1.setBounds(10, 57, 54, 15);
        contentPane.add(lblNewLabel_1);
        
        JLabel lblNewLabel_2 = new JLabel("email");
        lblNewLabel_2.setBounds(10, 96, 54, 15);
        contentPane.add(lblNewLabel_2);
        
        String[]sexitem={"男","女"};
        comboBox = new JComboBox(sexitem);
        comboBox.setBounds(92, 127, 195, 21);
        contentPane.add(comboBox);
        
        JLabel label = new JLabel("\u6027\u522B");
        label.setBounds(10, 130, 54, 15);
        contentPane.add(label);
        
        comboBox_1 = new JComboBox(getProvice());
        comboBox_1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    iteamChange();
                    
                    }
            }
        });
        
        /*
         * 1     JComboBox():建立一个新的JComboBox组件。
2     JComboBox(ComboBoxModel aModel):用ListModel建立一个新的JComboBox组件。
3     JComboBox(Object[] items):利用Array对象建立一个新的JComboBox组件。
4     JComboBox(Vector items):利用Vector对象建立一个新的JComboBox组件。
         */
        //comboBox_1.addItem(getProvice());
        
        comboBox_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                //
            }
        });
        comboBox_1.setBounds(92, 163, 195, 21);
        contentPane.add(comboBox_1);
        
        JLabel label_1 = new JLabel("\u7701\u4EFD");
        label_1.setBounds(10, 166, 54, 15);
        contentPane.add(label_1);
        
        JLabel label_2 = new JLabel("\u57CE\u5E02");
        label_2.setBounds(10, 198, 54, 15);
        contentPane.add(label_2);
        
        comboBox_2 = new JComboBox();
        comboBox_2.setBounds(92, 195, 195, 21);
        contentPane.add(comboBox_2);
        
        btnNewButton = new JButton("\u4FDD\u5B58");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                save();
            }
        });
        btnNewButton.setBounds(42, 228, 93, 23);
        contentPane.add(btnNewButton);
        
        btnNewButton_1 = new JButton("\u91CD\u7F6E");
        btnNewButton_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                try {
                    mapShowPlace frame = new mapShowPlace();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                };
            }
        });
        btnNewButton_1.setBounds(182, 228, 93, 23);
        contentPane.add(btnNewButton_1);
    }
    
    public static Map<String,String[]>model=new LinkedHashMap();
    private JTextField textField_2;
    private JTextField textField;
    private JTextField textField_1;
    private JComboBox comboBox;
    private JComboBox comboBox_1;
    private JComboBox comboBox_2;
    private JButton btnNewButton;
    private JButton btnNewButton_1;
    static
    {
        model.put("北京", new String[]{"北京"});
        model.put("上海", new String[]{"上海"});
        model.put("广州", new String[]{"广州"});
        
    }
    public Object[] getProvice()
    {
        Map<String, String[]>map=mapShowPlace.model;
        Set<String>set=map.keySet();
        Object[]provice=set.toArray();
        return provice;
        
    }
    private void iteamChange()
    {
        String selectProvince=(String)comboBox_1.getSelectedItem();
        comboBox_2.removeAllItems();
        String[] arrCity=getCity(selectProvince);
        comboBox_2.setModel(new DefaultComboBoxModel<>(arrCity));
        
    }
    public String[] getCity(String selectProvice)
    {
        Map<String, String[]>map=mapShowPlace.model;
        String[]arrCity=map.get(selectProvice);
        /*
        for (String string : arrCity) {
            System.out.println(string);
        }
        */
        return arrCity;
    }
    public void save()
    {
        try{
            PrintStream out=System.out;//获取当前输出位置
            PrintStream ps=new PrintStream("./register.txt");//获取目标输出位置
            System.setOut(ps);//设置输出位置
            String name=textField.getText();
            String place=textField_1.getText();
            String emial=textField_2.getText();
            String sex=(String)comboBox.getSelectedItem();
            String proverce=(String)comboBox_1.getSelectedItem();
            String city=(String)comboBox_2.getSelectedItem();
            System.out.println("姓名:"+name+"地址:"+place+"emial:"+emial+"性别:"+sex+"省份:"+proverce+"城市:"+city);
            System.setOut(out);//还原输出位置
            System.out.println("save succes in ./register.rtxt");
        }
        catch (FileNotFoundException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    
}

### 构建任务失败解决方案 当遇到 `Execution failed for task ':app:shrinkReleaseRes'` 错误时,这通常意味着资源压缩过程中出现了问题。此错误可能由多种原因引起,包括但不限于配置不正确、依赖冲突或特定于项目的其他因素。 #### 可能的原因分析 1. **ProGuard 或 R8 配置不当** ProGuard 和 R8 是用于优化和混淆代码以及减少 APK 大小的工具。如果这些工具的配置存在问题,可能会导致资源无法正常处理[^1]。 2. **重复资源** 如果项目中有多个模块定义了相同的资源名称,可能导致冲突并引发该错误。检查是否存在重名的 drawable、string 等资源文件[^2]。 3. **第三方库兼容性** 某些第三方库可能与当前使用的 Gradle 插件版本或其他库存在兼容性问题,从而影响到资源打包过程中的行为[^3]。 4. **Gradle 缓存问题** 有时旧缓存数据会干扰新编译的结果,尝试清理本地仓库和重新同步项目可以帮助排除此类潜在障碍[^4]。 #### 推荐的操作方法 为了有效解决问题,建议按照以下步骤逐一排查: ```bash # 清理项目构建目录 ./gradlew clean # 删除 .gradle 文件夹下的所有内容以清除缓存 rm -rf ~/.gradle/caches/ ``` 调整 `build.gradle` 中的相关设置也是一个重要环节: ```groovy android { ... buildTypes { release { minifyEnabled true // 是否启用代码缩减 shrinkResources true // 是否开启资源压缩 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' // 尝试禁用 shrinkResources 来测试是否为资源压缩引起的错误 // shrinkResources false } } } ``` 此外,在 `proguard-rules.pro` 文件内添加必要的保留规则,防止关键类被意外移除: ```text -keep class com.example.yourpackage.** { *; } # 替换为你自己的包路径 -dontwarn androidx.**,com.google.** # 忽略警告信息 ``` 最后,确保所使用的 Android Studio 版本是最新的稳定版,并且已经应用了所有的补丁更新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值