gpt4 book ai didi

java - 如何围绕我的模型程序包装 javafx 8 GUI

转载 作者:行者123 更新时间:2023-11-28 16:46:58 25 4
gpt4 key购买 nike

我是 JavaFx 8 的新手,我非常喜欢它。我已经构建了我的 GUI,并且能够让我的所有基本功能按预期运行。现在作为实验,我有一大块代码想在此 GUI 中运行,但我不确定是否应该将它扔到我的(Controller)类中并替换 System.out.println()textArea.setText() 每当我需要打印到文本区域 -> 屏幕时。或将其保留在(模型)中。

这是我想放在程序中的代码...

    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");

}

什么是实现这一目标的最佳方式。我只需要看到它完成一次,然后再打开...

谢谢!!!

最佳答案

第三种方法呢?

你可以定义一个 Service它执行 QuickSort 计算(在后台线程中)并在工作完成时告诉您的 Controller 。你的 Controller 只需要用结果更新 View 。因此,您的逻辑与应用程序代码的其余部分分离(如果排序需要很长时间,您可以避免卡住 UI)。

你在教程中有一个例子Concurrency in JavaFx来自甲骨文。负责实例化、给出“成功回调”和开始计算的代码部分应该交给 Controller :

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。

关于java - 如何围绕我的模型程序包装 javafx 8 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com