JAVA高等程序设计|实战教程第二章练习(学生基本信息的录入,图形界面)
1.CityMap.java(全国市县信息)
import java. util. LinkedHashMap;
import java. util. Map;
public class CityMap {
public static Map< String, String[ ] > model= new LinkedHashMap ( ) ;
static {
model. put ( "北京" , new String [ ] { "北京" } ) ;
model. put ( "上海" , new String [ ] { "上海" } ) ;
model. put ( "天津" , new String [ ] { "天津" } ) ;
model. put ( "重庆" , new String [ ] { "重庆" } ) ;
model. put ( "黑龙江" , new String [ ] { "哈尔滨" , "齐齐哈尔" , "牡丹江" , "大庆" , "伊春" , "双鸭山" , "鹤岗" , "鸡西" , "佳木斯" , "七台河" , "黑河" , "绥化" , "大兴安岭" } ) ;
model. put ( "吉林" , new String [ ] { "长春" , "延边" , "吉林" , "白山" , "白城" , "四平" , "松原" , "辽源" , "大安" , "通化" } ) ;
model. put ( "辽宁" , new String [ ] { "沈阳" , "大连" , "葫芦岛" , "旅顺" , "本溪" , "抚顺" , "铁岭" , "辽阳" , "营口" , "阜新" , "朝阳" , "锦州" , "丹东" , "鞍山" } ) ;
model. put ( "内蒙古" , new String [ ] { "呼和浩特" , "呼伦贝尔" , "锡林浩特" , "包头" , "赤峰" , "海拉尔" , "乌海" , "鄂尔多斯" , "通辽" } ) ;
model. put ( "河北" , new String [ ] { "石家庄" , "唐山" , "张家口" , "廊坊" , "邢台" , "邯郸" , "沧州" , "衡水" , "承德" , "保定" , "秦皇岛" } ) ;
model. put ( "河南" , new String [ ] { "郑州" , "开封" , "洛阳" , "平顶山" , "焦作" , "鹤壁" , "新乡" , "安阳" , "濮阳" , "许昌" , "漯河" , "三门峡" , "南阳" , "商丘" , "信阳" , "周口" , "驻马店" } ) ;
model. put ( "山东" , new String [ ] { "济南" , "青岛" , "淄博" , "威海" , "曲阜" , "临沂" , "烟台" , "枣庄" , "聊城" , "济宁" , "菏泽" , "泰安" , "日照" , "东营" , "德州" , "滨州" , "莱芜" , "潍坊" } ) ;
model. put ( "山西" , new String [ ] { "太原" , "阳泉" , "晋城" , "晋中" , "临汾" , "运城" , "长治" , "朔州" , "忻州" , "大同" , "吕梁" } ) ;
model. put ( "江苏" , new String [ ] { "南京" , "苏州" , "昆山" , "南通" , "太仓" , "吴县" , "徐州" , "宜兴" , "镇江" , "淮安" , "常熟" , "盐城" , "泰州" , "无锡" , "连云港" , "扬州" , "常州" , "宿迁" } ) ;
model. put ( "安徽" , new String [ ] { "合肥" , "巢湖" , "蚌埠" , "安庆" , "六安" , "滁州" , "马鞍山" , "阜阳" , "宣城" , "铜陵" , "淮北" , "芜湖" , "毫州" , "宿州" , "淮南" , "池州" } ) ;
model. put ( "陕西" , new String [ ] { "西安" , "韩城" , "安康" , "汉中" , "宝鸡" , "咸阳" , "榆林" , "渭南" , "商洛" , "铜川" , "延安" } ) ;
model. put ( "宁夏" , new String [ ] { "银川" , "固原" , "中卫" , "石嘴山" , "吴忠" } ) ;
model. put ( "甘肃" , new String [ ] { "兰州" , "白银" , "庆阳" , "酒泉" , "天水" , "武威" , "张掖" , "甘南" , "临夏" , "平凉" , "定西" , "金昌" } ) ;
model. put ( "青海" , new String [ ] { "西宁" , "海北" , "海西" , "黄南" , "果洛" , "玉树" , "海东" , "海南" } ) ;
model. put ( "湖北" , new String [ ] { "武汉" , "宜昌" , "黄冈" , "恩施" , "荆州" , "神农架" , "十堰" , "咸宁" , "襄樊" , "孝感" , "随州" , "黄石" , "荆门" , "鄂州" } ) ;
model. put ( "湖南" , new String [ ] { "长沙" , "邵阳" , "常德" , "郴州" , "吉首" , "株洲" , "娄底" , "湘潭" , "益阳" , "永州" , "岳阳" , "衡阳" , "怀化" , "韶山" , "张家界" } ) ;
model. put ( "浙江" , new String [ ] { "杭州" , "湖州" , "金华" , "宁波" , "丽水" , "绍兴" , "雁荡山" , "衢州" , "嘉兴" , "台州" , "舟山" , "温州" } ) ;
model. put ( "江西" , new String [ ] { "南昌" , "萍乡" , "九江" , "上饶" , "抚州" , "吉安" , "鹰潭" , "宜春" , "新余" , "景德镇" , "赣州" } ) ;
model. put ( "福建" , new String [ ] { "福州" , "厦门" , "龙岩" , "南平" , "宁德" , "莆田" , "泉州" , "三明" , "漳州" } ) ;
model. put ( "贵州" , new String [ ] { "贵阳" , "安顺" , "赤水" , "遵义" , "铜仁" , "六盘水" , "毕节" , "凯里" , "都匀" } ) ;
model. put ( "四川" , new String [ ] { "成都" , "泸州" , "内江" , "凉山" , "阿坝" , "巴中" , "广元" , "乐山" , "绵阳" , "德阳" , "攀枝花" , "雅安" , "宜宾" , "自贡" , "甘孜州" , "达州" , "资阳" , "广安" , "遂宁" , "眉山" , "南充" } ) ;
model. put ( "广东" , new String [ ] { "广州" , "深圳" , "潮州" , "韶关" , "湛江" , "惠州" , "清远" , "东莞" , "江门" , "茂名" , "肇庆" , "汕尾" , "河源" , "揭阳" , "梅州" , "中山" , "德庆" , "阳江" , "云浮" , "珠海" , "汕头" , "佛山" } ) ;
model. put ( "广西" , new String [ ] { "南宁" , "桂林" , "阳朔" , "柳州" , "梧州" , "玉林" , "桂平" , "贺州" , "钦州" , "贵港" , "防城港" , "百色" , "北海" , "河池" , "来宾" , "崇左" } ) ;
model. put ( "云南" , new String [ ] { "昆明" , "保山" , "楚雄" , "德宏" , "红河" , "临沧" , "怒江" , "曲靖" , "思茅" , "文山" , "玉溪" , "昭通" , "丽江" , "大理" } ) ;
model. put ( "海南" , new String [ ] { "海口" , "三亚" , "儋州" , "琼山" , "通什" , "文昌" } ) ;
model. put ( "新疆" , new String [ ] { "乌鲁木齐" , "阿勒泰" , "阿克苏" , "昌吉" , "哈密" , "和田" , "喀什" , "克拉玛依" , "石河子" , "塔城" , "库尔勒" , "吐鲁番" , "伊宁" } ) ;
}
}
2.InputFrame.java(界面类)
import javax. swing. *;
import javax. swing. border. TitledBorder;
import java. awt. *;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import java. awt. event. ItemEvent;
import java. awt. event. ItemListener;
import java. util. Map;
import java. util. Set;
public class InputFrame extends JFrame implements ActionListener {
final long serialVersionUID= - 4595347311922711984 L;
private JTextField nameJTextFirld;
private JTextField addressJextField;
private JComboBox sexJComBox;
private JTextField emailJTextField;
private JComboBox provinceJComboBox;
private JComboBox cityJComboBox;
private JLabel nameJLabel;
private JLabel sexJLabel;
private JLabel provinceJLable;
private JLabel cityJLable;
private JLabel emailJLable;
private JLabel addressJLable;
private JLabel nativePlaceJLable;
private JButton cancelButton;
private JButton saveButton;
private JTextArea infoArea;
public InputFrame ( String title) {
super ( title) ;
getContentPane ( ) . setLayout ( null) ;
setBounds ( 100 , 100 , 500 , 600 ) ;
setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
nameJTextFirld= new JTextField ( 20 ) ;
addressJextField= new JTextField ( ) ;
emailJTextField= new JTextField ( 30 ) ;
sexJComBox= new JComboBox < > ( ) ;
sexJComBox. setModel ( new javax. swing. DefaultComboBoxModel ( new String [ ] { "男" , "女" } ) ) ;
provinceJComboBox= new JComboBox < > ( ) ;
cityJComboBox= new JComboBox < > ( ) ;
nameJLabel= new JLabel ( "姓名" ) ;
sexJLabel= new JLabel ( "性别" ) ;
nativePlaceJLable= new JLabel ( "籍贯" ) ;
provinceJLable= new JLabel ( "省" ) ;
cityJLable= new JLabel ( "市" ) ;
addressJLable= new JLabel ( "通讯地址" ) ;
emailJLable= new JLabel ( "邮箱" ) ;
final JPanel pan1= new JPanel ( ) ;
pan1. setLayout ( new GridLayout ( ) ) ;
pan1. setBorder ( new TitledBorder ( null, "基本信息" , TitledBorder. DEFAULT_JUSTIFICATION, TitledBorder. DEFAULT_POSITION, null, null) ) ;
pan1. setBounds ( 12 , 12 , 418 , 70 ) ;
getContentPane ( ) . add ( pan1) ;
pan1. add ( nameJLabel) ;
pan1. add ( nameJTextFirld) ;
pan1. add ( sexJLabel) ;
pan1. add ( sexJComBox) ;
final JPanel pan2= new JPanel ( ) ;
pan2. setLayout ( new GridLayout ( ) ) ;
pan2. setBounds ( 12 , 98 , 418 , 70 ) ;
pan2. setBorder ( new TitledBorder ( null, "籍贯" , TitledBorder. DEFAULT_JUSTIFICATION, TitledBorder. DEFAULT_POSITION, null, null) ) ;
getContentPane ( ) . add ( pan2) ;
pan2. add ( provinceJLable) ;
pan2. add ( provinceJComboBox) ;
pan2. add ( cityJLable) ;
pan2. add ( cityJComboBox) ;
provinceJComboBox. addItemListener ( new ItemListener ( ) {
public void itemStateChanged ( final ItemEvent e) {
itemChange ( ) ;
}
} ) ;
provinceJComboBox. setModel ( new DefaultComboBoxModel ( getProvince ( ) ) ) ;
String province= ( String) getProvince ( ) [ 0 ] ;
cityJComboBox. setModel ( new DefaultComboBoxModel ( getCity ( province) ) ) ;
final JPanel pan3= new JPanel ( ) ;
pan3. setLayout ( new GridLayout ( ) ) ;
pan3. setBounds ( 12 , 178 , 418 , 70 ) ;
pan3. setBorder ( new TitledBorder ( null, "通讯地址" , TitledBorder. DEFAULT_JUSTIFICATION, TitledBorder. DEFAULT_POSITION, null, null) ) ;
getContentPane ( ) . add ( pan3) ;
pan3. add ( addressJLable) ;
pan3. add ( addressJextField) ;
pan3. add ( emailJLable) ;
pan3. add ( emailJTextField) ;
final JPanel pan4= new JPanel ( ) ;
pan4. setLayout ( null) ;
pan4. setBounds ( 12 , 278 , 418 , 70 ) ;
pan4. setBorder ( new TitledBorder ( null, "" , TitledBorder. DEFAULT_JUSTIFICATION, TitledBorder. DEFAULT_POSITION, null, null) ) ;
getContentPane ( ) . add ( pan4) ;
saveButton= new JButton ( "确定" ) ;
saveButton. setBounds ( 80 , 20 , 60 , 40 ) ;
cancelButton= new JButton ( "取消" ) ;
cancelButton. setBounds ( 280 , 20 , 60 , 40 ) ;
pan4. add ( saveButton) ;
pan4. add ( cancelButton) ;
saveButton. addActionListener ( this ) ;
cancelButton. addActionListener ( this ) ;
infoArea= new JTextArea ( "基本信息" , 100 , 200 ) ;
infoArea. setBounds ( 12 , 360 , 420 , 200 ) ;
add ( infoArea) ;
}
private String[ ] getCity ( String province) {
String[ ] city= CityMap. model. get ( province) ;
return city;
}
private Object[ ] getProvince ( ) {
Map< String, String[ ] > map= CityMap. model;
Set< String> set= map. keySet ( ) ;
Object[ ] province= set. toArray ( ) ;
return province;
}
private void itemChange ( ) {
String selectProvince= ( String) provinceJComboBox. getSelectedItem ( ) ;
cityJComboBox. removeAllItems ( ) ;
String[ ] arrCity= getCity ( selectProvince) ;
cityJComboBox. setModel ( new DefaultComboBoxModel ( arrCity) ) ;
}
@Override
public void actionPerformed ( ActionEvent e) {
if ( e. getSource ( ) == saveButton) {
infoArea. append ( "\n" ) ;
String string= "" ;
string+= "\n" + "姓名:" + nameJTextFirld. getText ( ) ;
string+= "\n" + "性别:" + sexJComBox. getSelectedItem ( ) ;
string+= "\n" + "籍贯:" + provinceJComboBox. getSelectedItem ( ) + "省" + cityJComboBox. getSelectedItem ( ) + "市/县" ;
string+= "\n" + "通讯地址" + addressJextField. getText ( ) ;
string+= "\n" + "邮箱:" + emailJTextField. getText ( ) ;
infoArea. append ( string) ;
}
if ( e. getSource ( ) == cancelButton) {
infoArea. setText ( "基本信息" ) ;
}
}
}
3.TestInputFrame.java(测试类)
import java. awt. *;
public class TestInputFrame {
public static void main ( String[ ] args) {
EventQueue. invokeLater ( new Runnable ( ) {
@Override
public void run ( ) {
try {
InputFrame frame = new InputFrame ( "信息录入界面" ) ;
frame. setVisible ( true ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
} ) ;
}
}