import MySort.BubbleSort;
import MySort.InsertSort;
import MySort.SelectSort;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*2012-12
* @author
* 排序图形界面
*/
public class TFrame {
private JFrame frame;
private int n; //产生随机数的个数
private int sortchoise = 0; //排序选择
private Random rand = new Random(); //随机数实例
private String st;
private Integer R[]; //R0:保存产生的随机数组,R:用于排序的数组
private Float F[]; //同上
private int numtype = -1; //产生随机数据的类型
private JRadioButton radio1; //整数
private final JRadioButton radio2; //浮点数
private final JRadioButton radio3; //冒泡排序
private final JRadioButton radio4; //插入排序
private final JRadioButton radio5; //选择排序
private JTextArea ori_textArea; //产生随机数组文本框
private JTextArea las_textArea; //排序后的数组显示文本框
private JButton mButton; //手动输入数组按钮
private JButton jButton; //自动生成数组按钮
private JButton sortButton; //开始排序按钮
private ButtonGroup group1; //数据类型组
private ButtonGroup group2; //排序类型组
private JScrollPane scrollPane1; //初始数组滑动窗口
private JScrollPane scrollPane2; //排序后数组滑动窗口
private JPanel panel_1;
private JPanel panel_2;
private JPanel panel_3;
private JPanel panel_4;
public TFrame(){
frame = new JFrame("排序");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,550); //窗口大小
Container c = frame.getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
radio1 = new JRadioButton("整数");
radio2 = new JRadioButton("浮点数");
radio3 = new JRadioButton("冒泡排序");
radio4 = new JRadioButton("插入排序");
radio5 = new JRadioButton("选择排序");
ori_textArea = new JTextArea(10, 40);
las_textArea = new JTextArea(10, 40);
mButton = new JButton("手动输入数组");
jButton = new JButton("自动生成数组");
sortButton = new JButton("开始排序");
group1 = new ButtonGroup();
group2 = new ButtonGroup();
scrollPane1 = new JScrollPane(ori_textArea);
scrollPane2 = new JScrollPane(las_textArea);
panel_1 = new JPanel();
panel_2 = new JPanel();
panel_3 = new JPanel();
panel_4 = new JPanel();
group1.add(radio1);
group1.add(radio2);
group2.add(radio3);
group2.add(radio4);
group2.add(radio5);
panel_1.add(radio1);
panel_1.add(radio2);
panel_1.add(jButton);
panel_1.add(mButton);
panel_2.add(radio3);
panel_2.add(radio4);
panel_2.add(radio5);
panel_2.add(sortButton);
panel_3.add(panel_1, BorderLayout.NORTH);
panel_3.add(scrollPane1, BorderLayout.CENTER);
panel_4.add(panel_2, BorderLayout.NORTH);
panel_4.add(scrollPane2, BorderLayout.CENTER);
c.add(panel_3);
c.add(panel_4);
ori_textArea.setLineWrap(true);
las_textArea.setLineWrap(true);
doWork();
}//构造方法TFrame()结束
//操作
public void doWork(){
//“整数”“浮点数”监听器
ActionListener a1 = new ActionListener(){
public void actionPerformed(ActionEvent e) {
JRadioButton radio = (JRadioButton)e.getSource();
if(radio == radio1){
numtype = 1;
}else{
numtype = 2;
}
}
};
//按钮监听器
ActionListener a2 = new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("自动生成数组")){
try{
n = Integer.parseInt(JOptionPane.showInputDialog(null, "数组元素个数是"));
}catch(Exception ex){
n = 0;
}
st = Rand();
ori_textArea.append(st+"\n\n");
}else if(e.getActionCommand().equals("开始排序")){
if(sortchoise == 0)
JOptionPane.showMessageDialog(null, "请选择排序方法");
else{
try{
las_textArea.append(doSort(sortchoise)+"\n\n");
}catch(Exception exc){
JOptionPane.showMessageDialog(null, "排序数组有误");
}
}
}else if(e.getActionCommand().equals("手动输入数组")){
String s = JOptionPane.showInputDialog(null, "输入数组(以空格隔开):");
ori_textArea.append(s+"\n\n");
String []st = s.split(" ");
n = st.length;
if(numtype == 1){
R = new Integer[n];
try{
for(int i = 0; i < n; i++){
R[i] = Integer.parseInt(st[i]);
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "数组里的数据有误");
}
}else{
F = new Float[n];
try{
for(int i = 0; i < n; i++)
F[i] = Float.parseFloat(st[i]);
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "数组里的数据有误");
}
}
}//手动输入数组结束
}//actionPerformed结束
};
//排序按钮监听器
ActionListener radioL = new ActionListener(){
public void actionPerformed(ActionEvent e) {
JRadioButton radio = (JRadioButton)e.getSource();
if(radio == radio3){
sortchoise = 1;
}else if(radio == radio4){
sortchoise = 2;
}else{
sortchoise = 3;
}
}
};
radio1.addActionListener(a1);
radio2.addActionListener(a1);
jButton.addActionListener(a2);
mButton.addActionListener(a2);
sortButton.addActionListener(a2);
radio3.addActionListener(radioL);
radio4.addActionListener(radioL);
radio5.addActionListener(radioL);
}
//自动生成随机数组
public String Rand(){
if(n == 0)
return "";
String str = new String();
if(numtype == 1){
R = new Integer[n];
for (int i = 0; i < n; i++) {
R[i] = (int)(Math.random()*1000);//产生0~1000的随机整数
str += R[i]+" ";
}
return str;
}
else{
F = new Float[n];
for (int i = 0; i < n; i++) {
F[i] = rand.nextFloat()*1000;//随机小于1000的浮点数
str += F[i]+" ";
}
return str;
}
}//Rand()方法结束
//排序方法
public String doSort(int sorttype){
BubbleSort a1; //冒�