我是JavaFx 8的新手,我绝对喜欢它。我已经构建了我的GUI,并且能够像我们想象的那样运行所有基本功能。现在作为一个实验,我有一大堆代码,我想在这个GUI中运行,但我不确定是否应该把它放在我的(Controller)类中,每当用System.out.println()
替换textArea.setText()
时我需要打印到文字区域 - > screen.or将其保存在(Model)中。
这是我想要放在程序中的代码......
Quick_SortFXController theSort = new Quick_SortFXController(10);
theSort.generateRandomArray();
System.out.println(Arrays.toString(Quick_SortFX.theArray));
theSort.quickSort(0, 9);
System.out.println(Arrays.toString(Quick_SortFX.theArray));
}
Quick_SortFXController(int newArraySize) {
arraySize = newArraySize;
theArray = new int[arraySize];
generateRandomArray();
}
public void quickSort(int left, int right) {
if (right - left <= 0)
return; // Everything is sorted
else {
// It doesn't matter what the pivot is, but it must
// be a value in the array
int pivot = theArray[right];
System.out.println("Value in right " + theArray[right]
+ " is made the pivot");
System.out.println("left = " + left + " right= " + right
+ " pivot= " + pivot + " sent to be partitioned");
int pivotLocation = partitionArray(left, right, pivot);
System.out.println("Value in left " + theArray[left]
+ " is made the pivot");
quickSort(left, pivotLocation - 1); // Sorts the left side
quickSort(pivotLocation + 1, right);
}
}
public int partitionArray(int left, int right, int pivot) {
int leftPointer = left - 1;
int rightPointer = right;
while (true) {
while (theArray[++leftPointer] < pivot)
;
printHorzArray(leftPointer, rightPointer);
System.out.println(theArray[leftPointer] + " in index "
+ leftPointer + " is bigger than the pivot value " + pivot);
while (rightPointer > 0 && theArray[--rightPointer] > pivot)
;
printHorzArray(leftPointer, rightPointer);
System.out.println(theArray[rightPointer] + " in index "
+ rightPointer + " is smaller than the pivot value "
+ pivot);
printHorzArray(leftPointer, rightPointer);
if (leftPointer >= rightPointer) {
System.out.println("left is >= right so start again");
break;
}
else {
swapValues(leftPointer, rightPointer);
System.out.println(theArray[leftPointer] + " was swapped for "
+ theArray[rightPointer]);
}
}
swapValues(leftPointer, right);
return leftPointer;
}
public void swapValues(int indexOne, int indexTwo) {
int temp = theArray[indexOne];
theArray[indexOne] = theArray[indexTwo];
theArray[indexTwo] = temp;
}
public void generateRandomArray() {
for (int i = 0; i < arraySize; i++) {
// Generate a random array with values between
// 10 and 59
theArray[i] = (int) (Math.random() * 50) + 10;
}
}
static void printHorzArray(int i, int j) {
for (int n = 0; n < 61; n++)
System.out.print("-");
System.out.println();
for (int n = 0; n < arraySize; n++) {
System.out.format("| %2s " + " ", n);
}
System.out.println("|");
for (int n = 0; n < 61; n++)
System.out.print("-");
System.out.println();
for (int n = 0; n < arraySize; n++) {
System.out.print(String.format("| %2s " + " ", theArray[n]));
}
System.out.println("|");
for (int n = 0; n < 61; n++)
System.out.print("-");
System.out.println();
if (i != -1) {
// Number of spaces to put before the F
int spacesBeforeFront = 6 * (i + 1) - 5;
for (int k = 0; k < spacesBeforeFront; k++)
System.out.print(" ");
System.out.print("L" + i);
// Number of spaces to put before the R
int spacesBeforeRear = 5 * (j + 1) - spacesBeforeFront;
for (int l = 0; l < spacesBeforeRear; l++)
System.out.print(" ");
System.out.print("R" + j);
System.out.print("\n");
}
是实现这一目标的最佳方式。我只需要看到它完成一次然后它就在那之后......
感谢!!!
答案 0 :(得分:1)
第三种方法呢?
您可以定义一个Service进行QuickSort计算(在后台线程中),并在作业完成时通知您的控制器。并且您的控制器只需要使用结果更新视图。因此,您的逻辑与其他应用程序代码分离(如果排序需要很长时间,您可以避免冻结UI。)
您在Oracle的教程Concurrency in JavaFx中有一个示例。负责实例化,给出“成功回调”并开始计算的代码部分应该转到控制器:
FirstLineService service = new FirstLineService();
service.setUrl("http://google.com");
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
System.out.println("done:" + t.getSource().getValue());
}
});
service.start();
此处服务获取HTTP响应的第一行,但您可以以相同的方式定义自己的服务。享受JavaFx。