📄 sortmain.java
字号:
package cn.hnu.haoc.strategy;
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.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.swing.*;
/**
* 显示界面
*
* @author haoc
* @since 2008.01.07
* @version 1.0.0.0
*
*/
public class SortMain extends JFrame {
String method=new String("BubbleSort");
String ss=null;
long t1,t2;
JLabel promptJlb = new JLabel("请选择排序方法:");
JLabel inputJlb = new JLabel("要排序的数据(从E:/demo.text中读取):");
JLabel outputJlb = new JLabel("排序结果:");
JLabel outputJlb1 = new JLabel();
JButton submitJbtn = new JButton("排序");
JComboBox sortMethodsJcbx = new JComboBox();
JTextField inputJtf = new JTextField();
JLabel runtimeJlb=new JLabel("运行时间:");
JLabel runtimeJlb1=new JLabel();
public SortMain() {
try {
super.setTitle("Strategy made by haoc!");
jInit();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 界面初始化
*
* @throws Exception
*/
private void jInit() throws Exception {
this.setSize(500, 266);
this.locate(200, 200);
this.getContentPane().setLayout(null);
sortMethodsJcbx.addItem("冒泡排序");
sortMethodsJcbx.addItem("选择排序");
sortMethodsJcbx.addItem("插入排序");
sortMethodsJcbx.addItem("归并排序");
sortMethodsJcbx.addItem("快速排序");
promptJlb.setBounds(new Rectangle(5, 5, 120, 20));
sortMethodsJcbx.setBounds(new Rectangle(125, 5, 150, 20));
inputJlb.setBounds(new Rectangle(5, 30, 200, 20));
inputJtf.setBounds(new Rectangle(5, 60, 450, 20));
outputJlb.setBounds(new Rectangle(5, 90, 200, 20));
outputJlb1.setBounds(new Rectangle(5, 120, 450, 20));
runtimeJlb.setBounds(new Rectangle(5,150,200,20));
runtimeJlb1.setBounds(new Rectangle(5,180,200,20));
submitJbtn.setBounds(new Rectangle(200, 210, 80, 20));
this.add(promptJlb);
this.add(sortMethodsJcbx);
this.add(inputJlb);
this.add(inputJtf);
this.add(outputJlb);
this.add(outputJlb1);
this.add(runtimeJlb);
this.add(runtimeJlb1);
this.add(submitJbtn);
sortMethodsJcbx.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
JComboBox jcb=(JComboBox)e.getSource();
int state=e.getStateChange();
if(state==ItemEvent.SELECTED){
ss=(String)jcb.getSelectedItem();
if(ss.equals("冒泡排序"))
method=new String("BubbleSort");
else if(ss.equals("选择排序"))
method=new String("ChooseSort");
else if(ss.equals("插入排序"))
method=new String("InsertSort");
else if(ss.equals("归并排序"))
method=new String("MergeSort");
else if(ss.equals("快速排序"))
method=new String("QuickSort");
else
;
}
}
});
submitJbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
t1=System.currentTimeMillis();
submitJbtn_actionPerformed(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
void submitJbtn_actionPerformed(ActionEvent e) throws IOException {
sort(method);
}
void sort(String method) throws IOException {
Color c = new Color((int) (255 * Math.random()), (int) (255 * Math
.random()), (int) (255 * Math.random()));
int i = 0;
int j = 0;
String num = null;
File file = new File("E:/demo.txt");
if (file.exists()) {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
while (str != null) {
i++;
str = br.readLine();
}
ArrayList[] list = new ArrayList[i];
FileReader fr1 = new FileReader(file);
BufferedReader br1 = new BufferedReader(fr1);
String str1 = br1.readLine();
while (str1 != null) {
list[j] = new ArrayList(Integer.parseInt(str1.toString()
.trim()));
j++;
str1 = br1.readLine();
}
Sort sort = new Sort(new String(method));
sort.sort(list);
t2=System.currentTimeMillis();
for (ArrayList e : list)
if(num==null)
num=new String(String.valueOf(e.getNum()))+" ";
else
num=num+new String(String.valueOf(e.getNum()))+" ";
outputJlb1.setForeground(c);
outputJlb1.setText(num);
runtimeJlb.setText(ss+"的运行时间:");
runtimeJlb1.setText(String.valueOf((int)((t2-t1)/1000))+"秒"+String.valueOf(((t2-t1)%1000))+"毫秒");
} else {
inputJtf.setText("文件不存在!");
}
}
public static void main(String[] args) throws IOException {
int i = 0;
int j = 0;
String num = null;
String bn=null;
SortMain sortMain = new SortMain();
sortMain.setVisible(true);
sortMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File file = new File("E:/demo.txt");
if (file.exists()) {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
while (str != null) {
i++;
if(bn==null)
bn=new String(str)+" ";
else
bn=bn+new String(str)+" ";
str = br.readLine();
}
sortMain.inputJtf.setText(bn);
//System.exit(0);
} else {
sortMain.inputJtf.setText("文件不存在!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -