天气逐渐变冷,老夫学习java的热情不减。还记得第一篇文章中的主界面中显示了一个比较可爱的登陆用户的照片,今天咱们来看一下这个功能的实现以及添加系统参数界面的实现。java
Java主界面显示以下sql
看着有些差别,是由于没有精准的计算。不过这个没关系,要紧的是实现。看一下登陆成功显示主界面照片的代码数据库
private void Init() { this.SetLoginUserPhoto(); } private void SetLoginUserPhoto() { String sql = "SELECT TOP(1) photo FROM UerInfo WHERE useno='" + FrmLogin.user.getUserNo() + "'"; ResultSet res = JDBCSqlHelper.query(sql); try { res.next(); byte[] byts = res.getBytes(1); if (byts.length == 0) { return; } Image img = java.awt.Toolkit.getDefaultToolkit().createImage(byts); ImageIcon ico = new ImageIcon(img); ico = new ImageIcon(ico.getImage().getScaledInstance( internalFrame.getWidth(), internalFrame.getHeight(), Image.SCALE_DEFAULT)); this.labUserPhoto.setIcon(ico); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
从数据库读出图片的byte数组,而后转化为Image,而后赋给jLabel。在转化出来之后的图片是原始图片的大小,并不能缩放显示在label中,因此咱们用代码设置图片的须要缩放的缩放比例。因为图片所在的label的外围是一个JInternalFrame,因此这里的高度和宽度去的是JInternalFrame的高和宽。这样图片就好看多了,作这些仍是要多查JDK API和google。数组
好了,接下来进行咱们的系统参数增长实现app
JAVA版的以下ide
这个界面比较简单,清除按钮的实现以下学习
private void ClearComponentContent() { txtCName.setText(""); txtEname.setText(""); txtRemark.setText(""); txtData.setText(""); txtDisplay.setText(""); }
保存按钮的逻辑以下ui
private void AddCode() { String ename = txtEname.getText().trim(); String cname = txtCName.getText().trim(); String data = txtData.getText().trim(); String display = txtDisplay.getText().trim(); String remark = txtRemark.getText().trim(); if (!this.CheckInput(ename, cname, data, display, remark)) { return; } StringBuffer strBuffer = new StringBuffer(); strBuffer .append("INSERT INTO dbo.Codes(ename,cname,data,display_content,remark) Values"); strBuffer.append("('" + ename + "'"); strBuffer.append(",'" + cname + "'"); strBuffer.append(",'" + data + "'"); strBuffer.append(",'" + display + "'"); strBuffer.append(",'" + remark + "')"); JDBCSqlHelper.update(strBuffer.toString()); MessageHelper.ShowMessage("保存成功!"); if (chkAutoClose.isSelected() != isAutoClosed) { ModifyConfig(); } if(chkAutoClose.isSelected()){ this.WindowClose(); } }
先check输入,而后构造sql,完了以后判断复选框的选择和上次复选框的选择若是不一致,修改配置文件,记录该界面的复选框状态。关于这个记录复选框,咱们新增了一个配置文件this
OK,咱们看一下是如何修改这个配置来记录是否保存完成自动关闭这个值的。google
private Element GetElementByName(String name) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder db = null; Document doc = null; try { db = dbf.newDocumentBuilder(); doc = db.parse(new File(xmlPath)); Element root = doc.getDocumentElement(); NodeList nodList = root.getElementsByTagName("AutoCloseFrm"); for (int i = 0; i < nodList.getLength(); i++) { Node nd = nodList.item(i); Element elment = (Element) nd; String frmName = this.getClass().getSimpleName(); if (elment.getAttribute("name").equals(frmName)) { return elment; } } } catch (SAXException | IOException | ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private void ModifyConfig() { Element elment = this.GetElementByName(this.getClass().getSimpleName()); if (elment != null) { elment.setAttribute("IsAutoClose", chkAutoClose.isSelected() ? "true" : "false"); this.writeToXml(elment.getOwnerDocument(), xmlPath); } }
你们都知道java中解析xml有好几种方式,什么DOM,JDOM,SAX,咱们仍是采用DOM吧,毕竟咋们的xml不大。OK,咱们先看下xml文件
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> <AutoCloseConfigs> <AutoCloseFrm IsAutoClose="true" name="FrmCodeAdd"/> </AutoCloseConfigs>
在这个xml里面,咱们可能会把AutoCloseFrm节点配置不少个,因此上面先获取到AutoCloseFrm这个NodeList,而后循环遍历,根据他的name属性来找到对应的element。这里的name对应着每一个页面的JFrame的类名,本界面命名为FrmCodeAdd.java。而后在modifyConfig中对其IsAutoClose属性进行修改。修改完成以后,刷新数据到xml文件。
private void writeToXml(Document doc, String name) { try { OutputStream fileoutputStream = new FileOutputStream(name); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.VERSION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(fileoutputStream); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
OK,这里的一些SetOutputProperty是设置xml的格式以及编码等。好了,这就是保存的整个逻辑。
那么在初始化的时候,咱们会先拿出xml中的配置,进行初始化。
private void Init() { boolean isAutoClose = this.GetAutoCloseValue(); this.chkAutoClose.setSelected(isAutoClose); isAutoClosed = isAutoClose; } private boolean GetAutoCloseValue() { Element elment = this.GetElementByName(this.getClass().getSimpleName()); if (elment != null) { boolean isAutoClose = Boolean.parseBoolean(elment .getAttribute("IsAutoClose")); return isAutoClose; } return false; }
好了,今天的主要亮点仍是DOM解析xml,以及图片所放显示在Jlabel。虽然对于长期从事于java开发的人来讲这可能不算什么,可是我以为初学者仍是能够看的。好了,有什么事加群.net技术冲锋舟,205217091。