62,614
社区成员
发帖
与我相关
我的任务
分享
public class Main extends JFrame {
/**
* @param args the command line arguments
*/
TextArea ta; //文本区域组件,用于输入数据
static File f = new File("data.txt"); //数据文件
static FileWriter fw;
static boolean startSort = true;
public static void main(String[] args) {
// TODO code application logic here
int param1=0, param2=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
fw = new FileWriter(f);
} catch (IOException e) {
e.printStackTrace();
}
if(args.length==0) { //如果命令行没有输入数据
System.out.println("命令行不含有参数,请选择其他方式导入数据");
System.out.println("************************************************");
System.out.println("1.手动输入 2.从文件夹导入 其他:退出");
System.out.println("************************************************");
try {
param1 = Integer.parseInt(br.readLine());
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
if(1==param1) {
new Main().launchFrame(); //选择手动输入后,弹出数据输入图形界面
}
else if(2==param1) {
}
else {
startSort = false;
}
}
else { //否则,将命令行输入的数据写入文件中
for(int i =0; i<args.length; i++) {
try {
fw.write(args[i]);
fw.write('\n');
fw.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
while(startSort) {
BubbleSort bs =new BubbleSort();
InsertSort is = new InsertSort();
SelectSort ss = new SelectSort();
System.out.println("************************************************************");
System.out.println("请选择您想要排序的方法:1.冒泡排序 2.插入排序 3.选择排序 0.退出");
System.out.println("************************************************************");
try {
param2 = Integer.parseInt(br.readLine()); //读取参数
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
switch(param2) {
case 0:
startSort = false;
break;
case 1:
bs.getData(f);
bs.Sort();
System.out.println(bs);
break;
case 2:
is.getData(f);
is.Sort();
System.out.println(is);
break;
case 3:
ss.getData(f);
ss.Sort();
System.out.println(ss);
break;
}
}
}
public void launchFrame() { //弹出输入界面方法
this.setLocation(200, 200);
this.setSize(600, 400);
this.setTitle("排序性能测试小程序");
this.getContentPane().setBackground(Color.WHITE);
this.setLayout(null);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
ta = new TextArea();
ta.setBounds(100, 120, 400, 160); //设置输入框的位置和大小
JButton b;
b = new JButton("提交数据");
b.setBounds(250, 300, 100, 60); //设置按钮的位置和大小
b.addActionListener(new MyActionListener()); //为按钮添加事件监听器
this.add(ta); //添加组件
this.add(b); //添加组件
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String[] str = ta.getText().split(" "); //将字符串拆分以便写入数据文件
for(int i =0; i<str.length; i++) {
try {
fw.write(str[i]);
fw.write('\n');
fw.flush();
/*
* 将数据写入文件f中
*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
System.out.println("数据写入完毕..."); //开始进入排序模块
JButton b = (JButton)e.getSource();
JFrame f = (JFrame)b.getTopLevelAncestor(); //得到当前JFrame容器
f.setVisible(false); //输入数据完毕,关闭输入界面
}
}
}