荐 Intellij Idea中手把手教你,快速开发Java GUI桌面应用,功能很强度很方便
程序员文章站
2022-04-29 10:42:04
使用开发Java GUI桌面应用,虽然说现在很少用java开发桌面应用,今天没事,在idea重温一下Java GUI,发现功能很强度很方便。下面把细节分享给大家。一、创建Java GUI项目二、快速做GUI界面......
使用开发Java GUI桌面应用,虽然说现在很少用java开发桌面应用,今天没事,在idea重温一下Java GUI,发现功能很强度很方便。
下面把细节分享给大家。
一、创建Java GUI项目
创建完成后的界面如下:
二、快速做GUI界面
GrabBaiduData.java代码分享如下:
public class GrabBaiduData extends Thread implements ActionListener, WindowListener { String[] provinces = {"北京", "上海", "天津", "重庆", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "四川", "贵州", "云南", "*", "陕西", "甘肃", "宁夏", "青海", "*", "香港", "澳门", "*"}; String[][] citys = { {"东城区", "西城区", "崇文区", "宣武区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "怀柔区", "平谷区", "密云县", "延庆县"}, {"黄浦区", "卢湾区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "闵行区", "宝山区", "嘉定区", "浦东新区", "金山区", "松江区", "青浦区", "南汇区", "奉贤区", "崇明县"}, {"和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区", "东丽区", "西青区", "津南区", "北辰区", "武清区", "宝坻区", "宁河县", "静海县", "蓟县"}, {"万州区", "涪陵区", "渝中区", "大渡口区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "北碚区", "万盛区", "双桥区", "渝北区", "巴南区", "黔江区", "长寿区", "綦江县", "潼南县", "铜梁县", "大足县", "荣昌县", "璧山县", "梁平县", "城口县", "丰都县", "垫江县", "武隆县", "忠县", "开县", "云阳县", "奉节县", "巫山县", "巫溪县", "石柱土家族自治县", "秀山土家族苗族自治县", "酉阳土家族苗族自治县", "彭水苗族土家族自治县", "江津市", "合川市", "永川市", "南川市"}, {"石家庄市", "张家口市", "承德市", "秦皇岛市", "唐山市", "廊坊市", "保定市", "衡水市", "沧州市", "邢台市", "邯郸市"}, {"太原市", "朔州市", "大同市", "阳泉市", "长治市", "晋城市", "忻州市", "晋中市", "临汾市", "吕梁市", "运城市"}, {"呼和浩特市", "包头市", "乌海市", "赤峰市", "通辽市", "呼伦贝尔市", "鄂尔多斯市", "乌兰察布市", "巴彦淖尔市", "兴安盟", "锡林郭勒盟", "阿拉善盟"}, {"沈阳市", "朝阳市", "阜新市", "铁岭市", "抚顺市", "本溪市", "辽阳市", "鞍山市", "丹东市", "大连市", "营口市", "盘锦市", "锦州市", "葫芦岛市"}, {"长春市", "白城市", "*市", "吉林市", "四平市", "辽源市", "通化市", "白山市", "延边州"}, {"哈尔滨市", "齐齐哈尔市", "七台河市", "黑河市", "大庆市", "鹤岗市", "伊春市", "佳木斯市", "双鸭山市", "鸡西市", "牡丹江市", "绥化市", "大兴安岭地区"}, {"南京市", "徐州市", "连云港市", "宿迁市", "淮安市", "盐城市", "扬州市", "泰州市", "南通市", "镇江市", "常州市", "无锡市", "苏州市"}, {"杭州市", "湖州市", "嘉兴市", "舟山市", "宁波市", "绍兴市", "衢州市", "金华市", "台州市", "温州市", "丽水市"}, {"合肥市", "宿州市", "淮北市", "亳州市", "阜阳市", "蚌埠市", "淮南市", "滁州市", "马鞍山市", "芜湖市", "铜陵市", "安庆市", "黄山市", "六安市", "巢湖市", "池州市", "宣城市"}, {"福州市", "南平市", "莆田市", "三明市", "泉州市", "厦门市", "漳州市", "龙岩市", "宁德市"}, {"南昌市", "九江市", "景德镇市", "鹰潭市", "新余市", "萍乡市", "赣州市", "上饶市", "抚州市", "宜春市", "吉安市"}, {"济南市", "青岛市", "聊城市", "德州市", "东营市", "淄博市", "潍坊市", "烟台市", "威海市", "日照市", "临沂市", "枣庄市", "济宁市", "泰安市", "莱芜市", "滨州市", "菏泽市"}, {"郑州市", "开封市", "三门峡市", "洛阳市", "焦作市", "新乡市", "鹤壁市", "安阳市", "濮阳市", "商丘市", "许昌市", "漯河市", "平顶山市", "南阳市", "信阳市", "周口市", "驻马店市", "济源市"}, {"武汉市", "十堰市", "襄樊市", "荆门市", "孝感市", "黄冈市", "鄂州市", "黄石市", "咸宁市", "荆州市", "宜昌市", "随州市", "省直辖县级行政单位", "恩施州"}, {"长沙市", "张家界市", "常德市", "益阳市", "岳阳市", "株洲市", "湘潭市", "衡阳市", "郴州市", "永州市", "邵阳市", "怀化市", "娄底市", "湘西州"}, {"广州市", "深圳市", "清远市", "韶关市", "河源市", "梅州市", "潮州市", "汕头市", "揭阳市", "汕尾市", "惠州市", "东莞市", "珠海市", "中山市", "江门市", "佛山市", "肇庆市", "云浮市", "阳江市", "茂名市", "湛江市"}, {"南宁市", "桂林市", "柳州市", "梧州市", "贵港市", "玉林市", "钦州市", "北海市", "防城港市", "崇左市", "百色市", "河池市", "来宾市", "贺州市"}, {"海口市", "三亚市", "省直辖县级行政单位"}, {"成都市", "广元市", "绵阳市", "德阳市", "南充市", "广安市", "遂宁市", "内江市", "乐山市", "自贡市", "泸州市", "宜宾市", "攀枝花市", "巴中市", "达州市", "资阳市", "眉山市", "雅安市", "阿坝州", "甘孜州", "凉山州"}, {"贵阳市", "六盘水市", "遵义市", "安顺市", "毕节地区", "铜仁地区", "黔东南州", "黔南州", "黔西南州"}, {"昆明市", "曲靖市", "玉溪市", "保山市", "昭通市", "丽江市", "思茅市", "临沧市", "德宏州", "怒江州", "迪庆州", "大理州", "楚雄州", "红河州", "文山州", "西双版纳州"}, {"拉萨市", "那曲地区", "昌都地区", "林芝地区", "山南地区", "日喀则地区", "阿里地区"}, {"西安市", "延安市", "铜川市", "渭南市", "咸阳市", "宝鸡市", "汉中市", "榆林市", "安康市", "商洛市"}, {"兰州市", "嘉峪关市", "白银市", "天水市", "武威市", "酒泉市", "张掖市", "庆阳市", "平凉市", "定西市", "陇南市", "临夏州", "甘南州"}, {"西宁市", "海东地区", "海北州", "海南州", "黄南州", "果洛州", "玉树州", "海西州"}, {"银川市", "石嘴山市", "吴忠市", "固原市", "中卫市"}, {"乌鲁木齐市", "克拉玛依市", "自治区直辖县级行政单位", "喀什地区", "阿克苏地区", "和田地区", "吐鲁番地区", "哈密地区", "克孜勒苏柯州", "博尔塔拉州", "昌吉州", "巴音郭楞州", "伊犁州", "塔城地区", "阿勒泰地区"}, {"香港"}, {"澳门"}, {"台北市", "*市", "台中市", "花莲市", "基隆市", "嘉义市", "金门市", "连江市", "苗栗市", "南投市", "澎湖市", "屏东市", "台东市", "台南市", "桃园市", "新竹市", "宜兰市", "云林市", "彰化市"}}; private JTextField textField1; private JComboBox comboBox1; private JCheckBox provinceCheckBox; private JCheckBox infoCheckBox; private JCheckBox mobileCheckBox; private JButton exportButton; private JPanel jpaneltitle; private JCheckBox searchkeyCheckBox; private JLabel selectprovince; private JLabel exportfield; private JLabel searchcontent; public void run() { Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); double Width = screensize.getWidth(); double Height = screensize.getHeight(); JFrame frame = new JFrame("百度地图数据抓取工具"); // GrabBaiduData data=new GrabBaiduData(); //设置大小 frame.setSize(500, 300); //窗体位置 frame.setLocation((int) ((Width - 500) / 2), (int) ((Height - 300) / 2)); frame.setContentPane(jpaneltitle); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); for (String province : provinces) { comboBox1.addItem(province); } exportButton.addActionListener(this); } @Override public void actionPerformed(ActionEvent actionEvent) { //System.out.println("//"+actionEvent.getSource()); if (actionEvent.getSource() == exportButton) { exportButton.setText("导出中……"); exportButton.setEnabled(false); try { String keyValue = textField1.getText(); if (keyValue == null || "".equals(keyValue.trim())) { JOptionPane.showMessageDialog(null, "查询内容不能为空!"); exportButton.setText("导出"); exportButton.setEnabled(true); return; } String privince = (String) comboBox1.getSelectedItem(); Integer privinceIndex = (Integer) comboBox1.getSelectedIndex(); boolean priviceData = provinceCheckBox.isSelected(); boolean infoData = infoCheckBox.isSelected(); boolean mobileData = mobileCheckBox.isSelected(); boolean nameData = searchkeyCheckBox.isSelected(); // System.out.println(new Gson().toJson(citys[privinceIndex])); // System.out.println(keyValue+"//"+privince+"//"+priviceData+"//"+infoData+"//"+mobileData); BaiduMapAPI baiduMapAPI = new BaiduMapAPI(priviceData, infoData, mobileData, nameData); baiduMapAPI.getArea(keyValue, citys[privinceIndex]); } catch (IOException e) { e.printStackTrace(); } JOptionPane.showMessageDialog(null, "导出完成!文件目录:" + BaiduMapAPI.saveFile); exportButton.setText("导出"); exportButton.setEnabled(true); } } @Override public void windowOpened(WindowEvent windowEvent) { } @Override public void windowClosing(WindowEvent windowEvent) { System.exit(0); } @Override public void windowClosed(WindowEvent windowEvent) { System.exit(0); } @Override public void windowIconified(WindowEvent windowEvent) { } @Override public void windowDeiconified(WindowEvent windowEvent) { } @Override public void windowActivated(WindowEvent windowEvent) { } @Override public void windowDeactivated(WindowEvent windowEvent) { } }
baiduMapAPI.java代码分享如下:
public class BaiduMapAPI { private boolean priviceData=true; private boolean infoData=true; private boolean mobileData=true; private boolean nameData=true; private int ExcelNum=1000; public static String saveFile=""; public BaiduMapAPI(){} public BaiduMapAPI( boolean priviceData,boolean infoData,boolean mobileData, boolean nameData){ this.priviceData=priviceData; this.infoData=infoData; this.mobileData=mobileData; this.nameData=nameData; } public void getArea(String searchKey,String[] areas) throws IOException { List<PageData> resultlist=new ArrayList<PageData>(); for(int k = 0; k < areas.length; k++){ String area=areas[k]; for (int i = 1; i < 100; i++) { String data=getBaiduData(searchKey,area,i+""); //System.out.println(data); BaiduMapAPIData baiduMapAPIData = new Gson().fromJson(data, BaiduMapAPIData.class); if(baiduMapAPIData.getTotal()==0){ break; } List<BaiduMapAPIResult> list=baiduMapAPIData.getResults(); for(int j = 0; j < list.size(); j++){ BaiduMapAPIResult baiduMapAPIResult=list.get(j); String mobile=baiduMapAPIResult.getTelephone(); if(mobile==null){ mobile=""; } String priviceDataStr=baiduMapAPIResult.getProvince() +baiduMapAPIResult.getCity(); String infoDataStr=baiduMapAPIResult.getProvince() +baiduMapAPIResult.getCity() +baiduMapAPIResult.getArea() +baiduMapAPIResult.getAddress().replace(baiduMapAPIResult.getProvince(), "").replace(baiduMapAPIResult.getCity(), "").replace(baiduMapAPIResult.getArea(), ""); String mobileDataStr=mobile; String nameDataStr=baiduMapAPIResult.getName(); if(resultlist.size()>=ExcelNum){ break; } //System.out.println(priviceDataStr+"//"+infoDataStr+"//"+nameDataStr+"//"+mobileDataStr); PageData pd=new PageData(); pd.put("priviceDataStr",priviceDataStr); pd.put("infoDataStr",infoDataStr); pd.put("nameDataStr",nameDataStr); pd.put("mobileDataStr",mobileDataStr); resultlist.add(pd); } if(resultlist.size()>=ExcelNum){ break; } } if(resultlist.size()>=ExcelNum){ break; } } try { this.exportExcel(resultlist); }catch (Exception e){} } private void exportExcel(List<PageData> resultlist)throws Exception{ ExportData2Excel excel=new ExportData2Excel(); // 创建工作薄 HSSFWorkbook workbook = excel.generateWorkbook(resultlist); File xlsFile = new File("导出数据.xls"); saveFile=xlsFile.getAbsolutePath(); FileOutputStream xlsStream = new FileOutputStream(xlsFile); workbook.write(xlsStream); } private String getBaiduData(String searchKey,String area,String pageNum) throws IOException{ //1:创建服务地址 URL url = new URL("http://api.map.baidu.com/place/v2/search?query="+searchKey+"®ion="+area+"&output=json&ak=cnOqGWBsMTo7em7***&page_num="+pageNum); //2:打开到服务地址的一个连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //3:设置连接参数 //3.1设置发送方式:POST必须大写 connection.setRequestMethod("GET"); //3.2设置数据格式:Content-type connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); connection.setRequestProperty("Connection", "keep-alive"); connection.setRequestProperty("Cookie", ""); //3.3设置输入输出,新创建的connection默认是没有读写权限的, connection.setDoInput(true); connection.setDoOutput(true); //4:组织SOAP协议数据,发送给服务端 OutputStream os = null; String returnData=""; try{ os=connection.getOutputStream(); //5:接收服务端的响应 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服务端响应成功 InputStream is =null; InputStreamReader isr=null; BufferedReader br =null; try{ is = connection.getInputStream(); isr = new InputStreamReader(is,"UTF-8"); br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while(null != (temp = br.readLine())){ sb.append(temp); } returnData=sb.toString(); }catch(Exception ee){ ee.printStackTrace(); }finally{ if(br!=null){ br.close(); } if(isr!=null){ isr.close(); } if(is!=null){ is.close(); } } } }catch(Exception e){ e.printStackTrace(); }finally{ if(os!=null){ os.close(); } } return returnData; } }
本文地址:https://blog.csdn.net/jlq_diligence/article/details/106879309