我想将其他类的输出放在 JFrame 中,代码如下:
在主类里面
import java.util.Arrays;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class Main {
public static void main(String[] args){
int choice;
int g;
String randomNo;
Scanner input=new Scanner(System.in);
randomNo=JOptionPane.showInputDialog(null,"Enter how many random no:");
g=Integer.parseInt(randomNo);
RandomAlgo rand=new RandomAlgo();
int[] data=rand.randomNum(g);
JOptionPane.showMessageDialog(null,"Your Random numbers Are : " + Arrays.toString(data));
String randomChoice;
randomChoice=JOptionPane.showInputDialog(null, "Choose Sorting algorithm: \n (1)Selection Sort \n (2)Insertion Sort \n (3)Bubble Sort \n (4)Quick Sort" );
choice=Integer.parseInt(randomChoice);
switch(choice){
case 1:{
SortingAlgo algo=new SortingAlgo();
data=algo.selectionSort(data);
break;
}
case 2:{
SortingAlgo algo=new SortingAlgo();
data=algo.InsertionSort(data);
break;
}
case 3:{
SortingAlgo algo=new SortingAlgo();
data=algo.bubbleSort(data);
break;
}
case 4:{
SortingAlgo algo=new SortingAlgo();
data=algo.quickSort(data);
}
}
}
public Main(){
JFrame frame=new JFrame("Sorted List");
JLabel jdate=new JLabel("");
frame.setSize(300,500);
frame.setVisible(true);
}
}
然后这是我的 SortingAlgo 类.......................... ......
import java.util.Arrays;
public class SortingAlgo{
public int[] selectionSort(int[] data){
int lenD = data.length;
int j = 0;
int tmp = 0;
for(int i=0;i<lenD;i++){
j = i;
for(int k = i;k<lenD;k++){
if(data[j]>data[k]){
j = k;
}
}
tmp = data[i];
System.out.println("\n"+ Arrays.toString(data));
data[i] = data[j];
data[j] = tmp;
}
return data;
}
public int[] InsertionSort(int[] data){
int len = data.length;
int key = 0;
int i = 0;
for(int j = 1;j<len;j++){
key = data[j];
i = j-1;
while(i>=0 && data[i]>key){
data[i+1] = data[i];
i = i-1;
data[i+1]=key;
System.out.println("\n"+ Arrays.toString(data));
}
}
return data;
}
public int[] bubbleSort(int[] data){
int lenD = data.length;
int tmp = 0;
for(int i = 0;i<lenD;i++){
for(int j = (lenD-1);j>=(i+1);j--){
System.out.println("\n"+ Arrays.toString(data));
if(data[j]<data[j-1]){
tmp = data[j];
data[j]=data[j-1];
data[j-1]=tmp;
}
} System.out.println("\n"+ Arrays.toString(data));
}
return data;
}
public int[] quickSort(int[] data){
int lenD = data.length;
int pivot = 0;
int ind = lenD/2;
int i,j = 0,k = 0;
if(lenD<2){
return data;
}
else{
int[] L = new int[lenD];
int[] R = new int[lenD];
int[] sorted = new int[lenD];
pivot = data[ind];
for(i=0;i<lenD;i++){
if(i!=ind){
if(data[i]<pivot){
L[j] = data[i];
j++;
}
else{
R[k] = data[i];
k++;
}
}
}
int[] sortedL = new int[j];
int[] sortedR = new int[k];
System.arraycopy(L, 0, sortedL, 0, j);
System.arraycopy(R, 0, sortedR, 0, k);
sortedL = quickSort(sortedL);
sortedR = quickSort(sortedR);
System.arraycopy(sortedL, 0, sorted, 0, j);
sorted[j] = pivot;
System.arraycopy(sortedR, 0, sorted, j+1, k);
System.out.println("\n"+ Arrays.toString(sorted));
return sorted;
}
}
}
至于我的随机课..................................
import java.util.HashSet;
import java.util.*;
public class RandomAlgo {
public int[] randomNum(int g){
Random rand = new Random();
int[] randomNumbers = new int[g];
for (int i = 0; i < g; i++) {
int e = rand.nextInt(1000);
randomNumbers[i] = e;
}
return randomNumbers;
}
}
我的代码在控制台中运行。但我现在想将排序列表的输出放在一个框架内。而不是在控制台上。