📄 sortpanel.java
字号:
package datas2;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Rectangle;
public class sortPanel extends JPanel {
public int time;
//Runn run = new Runn(time);;//= new Runn();//
Runn run = new Runn();
XYLayout xYLayout1 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField jTextField2 = new JTextField();
JButton jButton1 = new JButton();
String[] box = {"1","10","100","1000"};
public sortPanel() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(xYLayout1);
this.setBackground(Color.lightGray);
xYLayout1.setWidth(520);
xYLayout1.setHeight(445);
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setBorder(BorderFactory.createLineBorder(Color.black));
run.setPreferredSize(new Dimension(100, 100));
jPanel1.setLayout(xYLayout2);
jPanel1.setBackground(Color.lightGray);
jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jLabel1.setForeground(Color.blue);
jLabel1.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("请输入要排序的数组的大小:");
jTextField1.setBackground(Color.lightGray);
jTextField1.setFont(new java.awt.Font("宋体", Font.BOLD, 14));
jTextField1.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel2.setForeground(Color.red);
jLabel2.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setHorizontalTextPosition(SwingConstants.LEFT);
jLabel2.setText("从左边选择合适的排序算法");
jLabel3.setFont(new java.awt.Font("宋体", Font.BOLD, 14));
jLabel3.setForeground(Color.blue);
jLabel3.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel3.setText("您选择的排序算法是:");
jTextField2.setBorder(BorderFactory.createLineBorder(Color.black));
jTextField2.setEditable(false);
jTextArea1.setBackground(Color.lightGray);
jTextArea1.setEditable(false);
jButton1.setText("开始");
jButton1.addActionListener(new sortPanel_jButton1_actionAdapter(this));
jScrollPane1.getViewport().add(jTextArea1);
jPanel1.add(jLabel2, new XYConstraints(0, 58, 191, 31));
jPanel1.add(jLabel1, new XYConstraints(0, 0, 191, 31));
jPanel1.add(jTextField1, new XYConstraints(196, 0, 77, 31));
jPanel1.add(jLabel3, new XYConstraints(0, 114, 186, 28));
jPanel1.add(jTextField2, new XYConstraints(196, 114, 77, 28));
jPanel1.add(jButton1, new XYConstraints(303, 114, -1, 28));
jPanel1.add(run, new XYConstraints(376, 1, 104, 140));
this.add(jPanel1, new XYConstraints(4, 7, 562, -1));
this.add(jScrollPane1, new XYConstraints(10, 171, 474, 261));
}
public void jButton1_actionPerformed(ActionEvent e) {
int max = Integer.parseInt(jTextField1.getText());
Basicsort basicsort = new Basicsort(max);
Heap heap = new Heap(max);
if (jTextField2.getText().equals("冒泡排序")) {
Thread counterThread = new Thread(run, "counter");
counterThread.start();
int[] array = basicsort.bubblesort();
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");
}
}
// run.stopClock();
}
else if(jTextField2.getText().equals("插入排序"))
{
Thread counterThread = new Thread(run, "counter");
counterThread.start();
int[] array = basicsort.insertsort();
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");}
}
//run.stopClock();
}
else if(jTextField2.getText().equals("选择排序"))
{
Thread counterThread = new Thread(run, "counter");
counterThread.start();
int[] array =basicsort.selectionsort();
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");}
}
// run.stopClock();
}
else if (jTextField2.getText().equals("希尔排序") )
{
Thread counterThread = new Thread(run, "counter");
counterThread.start();
int[] array =basicsort.shellSort();
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");}
}
//run.stopClock();
}
else if(jTextField2.getText().equals("归并排序"))
{
Thread counterThread = new Thread(run, "counter");
counterThread.start();
int[] array =basicsort.mergeSort();
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");}
}
// run.stopClock();/*
}
else if (jTextField2.getText().equals("基数排序"))
{
Thread counterThread = new Thread(run, "counter");
int[] b = new int[max];
int[] cnt=new int[10];
String s = String.valueOf(max);
int m1 = s.length();
counterThread.start();
int[] array =basicsort.binsort(b,max,m1,10,cnt);
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");
}
}
}
else if(jTextField2.getText().equals("快速排序"))
{
Thread counterThread = new Thread(run, "counter");
counterThread.start();
int[] array =basicsort.quickSort();
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");}
}
// run.stopClock();
}
else if(jTextField2.getText().equals("堆排序"))
{
for(int j=0; j<max; j++) // fill array with
{ // random nodes
int random = (int)(java.lang.Math.random()*max);
Node newNode = new Node(random);
heap.insertAt(j, newNode);
heap.incrementSize();
}
for(int j=max/2-1; j>=0; j--) // make random array into heap
heap.trickleDown(j);
for(int j=max-1; j>=0; j--) // remove from heap and
{ // store at array end
Node biggestNode = heap.remove();
heap.insertAt(j, biggestNode);
}
Thread counterThread = new Thread(run, "counter");
counterThread.start();
int[] a = new int[max];
int[] array = heap.displayArray(a);
run.stopClock();
for (int i = 0; i < array.length; i++) {
if(i<=100){
jTextArea1.append(String.valueOf(array[i]) + " ");
if (i % 20 == 0 && i != 0)
jTextArea1.append("\n");}
}
// run.stopClock();
}
}
public void jComboBox1_actionPerformed(ActionEvent e) {
// String n = (String);
// time = Integer.parseInt(n);
}
}
class sortPanel_jButton1_actionAdapter implements ActionListener {
private sortPanel adaptee;
sortPanel_jButton1_actionAdapter(sortPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -