JTestArea带滚动条输出,为什么没有出现要显示的数据?
本程序是实现几种排序算法,通过提示输入数字来选择所要采用的算法。只贴了两个类上来,其他的只是继承InsertSort而已。Sort类是主类:
Java code
package cn.sort;
import javax.swing.JOptionPane;
/**
* @(#)Sort.java 主类
*
* @author
* @No.200631000524
* @Class:5
* @version 1.00 2008/4/5
*/
// import javax.swing.JOptionPane;
public class Sort {
/**
* @param argh
* 主函数用来调用各个子类的函数; 建立每一个类的对象,在主类直接调用排序方法实现排序;
*
*
*/
public static void main(String[] argh) {
InsertSort e = new InsertSort();
SeletionSort f = new SeletionSort();
BinaryInsertSort g = new BinaryInsertSort();
ShellSort h = new ShellSort();
BubbleSort i = new BubbleSort();
QuickSort j = new QuickSort();
HeapSort k = new HeapSort();
MergeSort l = new MergeSort();
RadixSort m = new RadixSort();
/**
* 建立对话框,用户选择需要的排序算法;
*/
String input = JOptionPane
.showInputDialog(
null,
"Input a number to select the algorithmic:
"
+ "Insert Algorithmic:1 Selection Algorithmic:2 BinaryInsert Algorithmic:3
"
+ "Shell Algorithmic:4 Bubble Algorithmic:5 Quick Algorithmic:6
"
+ "Heap Algorithmic:7 Merge Algorithmic:8 Radix Algorithmict:9",
"Welcome to Algorithmic-Sort!",
JOptionPane.QUESTION_MESSAGE);
int choice = Integer.parseInt(input);
/**
* switch()选择算法;
*/
switch (choice) {
case 1:
e.select();
break;
case 2:
f.select();
break;
case 3:
g.select();
break;
case 4:
h.select();
break;
case 5:
i.select();
break;
case 6:
j.select();
break;
case 7:
k.select();
break;
case 8:
l.select();
break;
case 9:
m.select();
break;
default:
System.out.println("WRONG INPUT!");
}
}
}
InsertSort实现插入排序并作为其他排序算法的基类:
Java code
package cn.sort;
import java.awt.BorderLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* 选择排序 实现选择排序算法; 作为其他排序类的基类;
*
* @author 谢家裕
*
*/
public class InsertSort extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/*public InsertSort() {
setTitle("title");
setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
for(int i=0;i<=100;i++){
textArea.append("a");
}
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
}*/
/**
* select()方法实现数组的输入,并调用sort()排序算法;
* 被其他类所继承;
*/
public void select() {
/**
* 通过对话框输入数组的长度,数组的各个元素;
*/
String input2 = JOptionPane.showInputDialog(null, "Number=",
"Input the length of the Array", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(input2);
int[] sortArray = new int[number];
Random generator = new Random();
/**
* 产生小于100000000的随机整数;
*/
for (int i = 0; i < sortArray.length; i++) {
sortArray = generator.nextInt(100000000);
}
sort(sortArray);
}
/**
* 选择排序算法的实现;
*
* @param a
*/
public void sort(int[] a) {
/*
* 获得执行for循环之前系统当前时间,以nanosecond毫微秒记;
*/
long begin = System.nanoTime();
for (int index = 1; index < a.length; index++) {
temp = a[index];
for (j = index; j > 0; j--) {
if (a[j - 1] > temp) {
a[j] = a[j - 1];
} else
break;
}
a[j] = temp;
}
/*
* 获得执行for循环之后系统当前时间,以nanosecond毫微秒记;
*/
long end = System.nanoTime();
long k = end - begin;
String efficiency = new String("O(1/4(n^2+5n-6))");
print(a, k, efficiency);
}
/**
* 输出排序的结果;
* 被其他类所继承;
* @param a
*/
public void print(int[] a, long k, String efficiency) {
String out = new String();
for (int i = 0; i < a.length; i++)
// System.out.print(a + " ");
if (i == 0) {
out = String.valueOf(a);
} else {
out = out + " " + a;
if (i % 30 == 0)
out += "
";
}
/*
* 输出排序后的元素,所采用算法的效率以及花费时间(nanosecond)毫微秒;
*/
JOptionPane.showMessageDialog(null, "The sort of this Array is:
"
+ out + "
The efficiency is:" + efficiency + ".
"
+ "The time of this algorithmic has spent is: " + k + " nanasecond.",
"Answer", JOptionPane.INFORMATION_MESSAGE);
InsertSort e=new InsertSort();
final int x=300;
final int y=300;
e.setTitle("title");
e.setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
textArea.append("a");//目的是想输出以下两句,但是显示了窗口里面什么也没有,也没有滚动条。@@@@@@@@@@@@@@@@@@@@@@@@@@
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true);
}
private int temp;
private int j;
}
|