Java Swing简单样例

来源:互联网 发布:扫描编辑软件 编辑:程序博客网 时间:2024/05/24 00:11

     大学时候Java 学的很简单,当时玩的也多,学了就忘。额外买的java书看了几章就不看了,想想真是惭愧。最近由于实验室需要对于别人的东西做一点简单测试,我也就花了1天重新写了一个简单的Jframe界面。

   非常非常简单,本来都不好意思放上来,不过想着一点是一点,大学没不好好学习靠着以前的底子写一点简单东西

    1.JFileChooser选择路径在JTextField上展示

    2.数据浏览功能,txt数据用一个新的frame展示出来

    3.bean结构数据降序排列,计算时间

     4.将文件输出保存到当前工程文件夹下,按照文件名_type_日期(win下是\r\n换行、日期中符号  :  不能在文件名中出现)


需要排序的bean:

public class Store {private String str1;private String str2;private double rs;public String getStr1() {return str1;}public void setStr1(String str1) {this.str1 = str1;}public String getStr2() {return str2;}public void setStr2(String str2) {this.str2 = str2;}public double getRs() {return rs;}public void setRs(double rs) {this.rs = rs;}}
主函数所在类:
import javax.swing.*;import sentsim.CompSim;import java.util.*;import java.util.List;import java.awt.*;import java.lang.System;import java.text.SimpleDateFormat;import java.awt.event.*;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;class Intcom implements Comparator<Store> {public int compare(Store A, Store B) {if (A.getRs() > B.getRs())return -1;else if(A.getRs() == B.getRs()) return 0;else return 1;}}@SuppressWarnings("serial")public class SimiliarFrame extends JFrame implements ActionListener {JButton btn;JButton Watch1, Choose1;JButton Watch2, Choose2;JLabel b1, b2, b3;JTextField t1, t2, t3;JFileChooser fc = new JFileChooser();TextArea area;JPanel p1, p2;public SimiliarFrame() {//super();btn = new JButton("开始运算");Choose1 = new JButton("文件选择");Choose2 = new JButton("文件选择");Watch1 = new JButton("数据浏览");Watch2 = new JButton("数据浏览");b1 = new JLabel("文件路径1:");b2 = new JLabel("文件路径2:");b3 = new JLabel("得分最高的多少个:");t1 = new JTextField(10); t2 = new JTextField(10);t3 = new JTextField(10);btn.addActionListener(this);Choose1.addActionListener(this);Choose2.addActionListener(this);Watch1.addActionListener(this);Watch2.addActionListener(this);p1 = new JPanel();// row cloumn 水平、垂直间隔p1.setLayout(new GridLayout(3, 4, 20, 50));p1.add(b1);p1.add(t1);p1.add(Choose1);p1.add(Watch1);p1.add(b2);p1.add(t2);p1.add(Choose2);p1.add(Watch2);p1.add(b3);p1.add(t3);this.setLayout(null);   p1.setBounds(40, 30, 500, 200);this.add(p1,"NORTH");p2 = new JPanel();p2.setBounds(230, 400, 100, 100);//p2.setLayout(new BorderLayout());   btn.setActionCommand("开始运算");p2.add(btn);this.add(p2, "South");}void encode() throws IOException {String s1, s2, s3;String statement1, statement2;List<Store> store = new ArrayList<Store>();s1 = t1.getText();s2 = t2.getText();s3 = t3.getText();int length = Integer.parseInt(s3);//compSim接口调用//String location=System.getProperty("user.dir");//System.out.println(location);//CompSim cs = new CompSim();//cs.init("config.properties");//  ./本级路径    ../上级路径     只写名字当前工程中?  String filename1=getfileName(s1);String filename2=getfileName(s2);//System.out.println(output);BufferedReader reader1 = new BufferedReader(new FileReader(s1));BufferedReader reader2 = new BufferedReader(new FileReader(s2));//System.out.println(s1 + " " + s2 + " " + s3 + " " + length);statement1 = reader1.readLine();while (statement1 != null && statement1.length()>0) {// System.out.println(statement1);statement2 = reader2.readLine();while (statement2 != null && statement2.length()>0) {// System.out.println(statement2);//double rs = cs.compSim(statement1, statement2);double rs =0;Store ff = new Store();ff.setStr1(statement1);ff.setStr2(statement2);ff.setRs(rs);store.add(ff);statement2 = reader2.readLine();}statement1 = reader1.readLine();reader2 = new BufferedReader(new FileReader(s2));}reader1.close();reader2.close();Collections.sort(store, new Intcom());//System.out.println(store.size());//BufferedReader reader = new BufferedReader(new FileReader("config.properties"));//String output=reader.readLine();//output=reader.readLine();//output=reader.readLine();//output=output.substring(output.indexOf("=")+1, output.length());//reader.close();SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");  File file = new File("Result");  //判断文件夹是否存在,如果不存在则创建文件夹  if (!file.exists()) {   file.mkdir();  }BufferedWriter out1=new BufferedWriter(new FileWriter("Result/"+filename1+"_ResultType1_"+df.format(new Date())+".txt"));BufferedWriter out2=new BufferedWriter(new FileWriter("Result/"+filename2+"_ResultType1_"+df.format(new Date())+".txt"));BufferedWriter out3=new BufferedWriter(new FileWriter("Result/"+filename1+"_ResultType2_"+df.format(new Date())+".txt"));BufferedWriter out4=new BufferedWriter(new FileWriter("Result/"+filename2+"_ResultType2_"+df.format(new Date())+".txt"));HashSet<String> Type2Set1=new HashSet<String>();HashSet<String> Type2Set2=new HashSet<String>();out1.write("");out1.flush();out2.write("");out2.flush();out3.write("");out3.flush();out4.write("");out4.flush();for (int i = 0; i < length; i++) {//System.out.println(store.get(i).getStr1() + " " + store.get(i).getStr2() + " " + store.get(i).getRs());    out1.write(store.get(i).getStr1()+"\r\n");    out2.write(store.get(i).getStr2()+"\r\n");    Type2Set1.add(store.get(i).getStr1()) ;    Type2Set2.add(store.get(i).getStr2());} Iterator<String> it1=Type2Set1.iterator();Iterator<String> it2=Type2Set2.iterator();while(it1.hasNext()){String ss=it1.next();out3.write(ss+"\r\n");}while(it2.hasNext()){String ss=it2.next();out4.write(ss+"\r\n");}out1.flush();    out1.close();    out2.flush();       out2.close();    out3.flush();       out3.close();    out4.flush();    out4.close();}String formatTime(long ms) {int ss = 1000;int mi = ss * 60;int hh = mi * 60;int dd = hh * 24;long day = ms / dd;long hour = (ms - day * dd) / hh;long minute = (ms - day * dd - hour * hh) / mi;long second = (ms - day * dd - hour * hh - minute * mi) / ss;long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;//String strDay = day < 10 ? "0" + day : "" + day; // 天//String strHour = hour < 10 ? "0" + hour : "" + hour;// 小时String strMinute = minute < 10 ? "0" + minute : "" + minute;// 分钟String strSecond = second < 10 ? "0" + second : "" + second;// 秒String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;// 毫秒strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;return strMinute + " 分钟 " + strSecond + " 秒";}public void actionPerformed(ActionEvent e) {String s = e.getActionCommand();if (e.getSource() == Choose1) {int intRetVal = fc.showOpenDialog(this);if (intRetVal == JFileChooser.APPROVE_OPTION) {t1.setText(fc.getSelectedFile().getPath());}}if (e.getSource() == Choose2) {int intRetVal = fc.showOpenDialog(this);if (intRetVal == JFileChooser.APPROVE_OPTION) {t2.setText(fc.getSelectedFile().getPath());}}if (e.getSource() == Watch1) {JFrame f = new JFrame();setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 设置仅仅关闭当前窗口//点击关闭后不会使得所有窗口关闭f.setSize(400, 400);f.setLocationRelativeTo(null);// 获取文件后缀名String extensionName = getExtensionName(t1.getText());if ("txt".equals(extensionName)) {f.setTitle("显示文本");area = new TextArea();// 获取文本值String text = readTxt(t1.getText());area.setText(text);f.add(area);f.setVisible(true);// f.dispose();}}if (e.getSource() == Watch2) {JFrame f = new JFrame();setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 设置仅仅关闭当前窗口f.setSize(400, 400);f.setLocationRelativeTo(null);// 获取文件后缀名String extensionName = getExtensionName(t2.getText());if ("txt".equals(extensionName)|| "log".equals(extensionName)) {f.setTitle("显示文本");area = new TextArea();// 获取文本值String text = readTxt(t2.getText());area.setText(text);f.add(area);f.setVisible(true);// f.dispose();}}if (s.equals("开始运算")) {Graphics g = this.getGraphics();long starttime=System.currentTimeMillis();g.setFont(new Font("宋体", Font.BOLD,15));g.drawString("相似度运行计算中 .....", 220, 350);try {encode();// Thread.sleep(1000);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}long endtime=System.currentTimeMillis();g.clearRect(200, 300, 200, 100);g.setFont(new Font("宋体", Font.BOLD,15));g.drawString("程序运行时间: " + formatTime(endtime-starttime), 210, 350); try {Thread.sleep(3000);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} g.clearRect(200, 300, 250, 100);}}/** * @Description:读取文件 * @param path *            - 文件地址 * @return * @throws */private String readTxt(String path) {if (path == null || "".equals(path)) {return "";}StringBuffer sb = new StringBuffer();InputStreamReader read = null;BufferedReader reader = null;try {// FileInputStream类是以字节读取文件的,所以读到的中文在控制台输出的是乱码// 或者FileReader是可以正常输出中文的//read = new InputStreamReader(new FileInputStream(file));reader = new BufferedReader(new FileReader(path));String line;while ((line = reader.readLine()) != null) {sb.append(line);sb.append("\n");}} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (read != null) {try {read.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (reader != null) {try {reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return sb.toString();}/** * @Description:获取文件后缀名 * @param filename * @return * @throws */private String getExtensionName(String filename) {if ((filename != null) && (filename.length() > 0)) {int dot = filename.lastIndexOf('.');if ((dot > -1) && (dot < (filename.length() - 1))) {return filename.substring(dot + 1);}}return filename;}private String getfileName(String filename) {if ((filename != null) && (filename.length() > 0)) {int dot = filename.indexOf(".");int start=0;    start=filename.lastIndexOf("/");if(start!=-1){//System.out.println("zuo "+start);return filename.substring(start+1,dot);}else{start=filename.lastIndexOf("\\");//System.out.println("you "+start);return filename.substring(start+1,dot);}  }return filename;}public static void main(String[] args) {SimiliarFrame frame = new SimiliarFrame();frame.setSize(600, 500);//frame.setFont(new Font("宋体", Font.BOLD,5));frame.setTitle("ISCAS");frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}


0 0
原创粉丝点击