gpt4 book ai didi

javaFx 单选按钮和文本字段用户选择和用户输入

转载 作者:行者123 更新时间:2023-11-30 06:47:37 27 4
gpt4 key购买 nike

所以我的问题是尝试实现一个具有 4 个单选按钮的 GUI,让用户选择他们想要执行的排序类型(快速、插入、冒泡、选择),然后他们可以从另外 3 个单选按钮中进行选择,选择已经排序、随机、反向排序。然后它有一个文本字段,允许他们选择数组的输入大小,然后选择 block 大小。用户选择单选按钮并将信息输入输入大小文本字段和 block ​​大小文本字段后,单击“执行”,程序将对数组进行排序并将排序后的数组输出到控制台。

因此,我需要帮助是实现“执行”按钮的操作事件或监听器,以从单选按钮和文本字段获取信息。我理解其背后的逻辑,但我不确定如何将其全部链接到事件处理程序/操作事件中。到目前为止,我的代码已编译完毕,当您点击“执行”按钮时,它会打印一个包含 100 个数字的数组,该数组已按 1-100 的顺序排序。它没有考虑用户在单选按钮或文本字段上选择的 block 大小和输入大小,我需要这方面的帮助。这是我的代码:

package project4practice;

import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Project4practice extends Application {

@Override
public void start(Stage primaryStage) {
BorderPane rootPane = new BorderPane();
GridPane gp = new GridPane();
rootPane.setCenter(gp);
gp.setVgap(5);
gp.setHgap(5);
rootPane.prefWidth(700);
rootPane.prefHeight(400);
gp.prefWidth(400);
gp.prefHeight(400);
Label sort = new Label(" Sorting Algorithm ");
RadioButton selection = new RadioButton("selection ");
RadioButton bubble = new RadioButton("bubble ");
RadioButton insertion = new RadioButton("insertion");
RadioButton quick = new RadioButton("Quick ");
Label inputType = new Label(" Input Type ");
RadioButton sorted = new RadioButton("Already Sorted ");
RadioButton reverse = new RadioButton("Reverse ");
RadioButton random = new RadioButton("Random ");
Label inputSize = new Label(" Input Size: ");
TextField inputText = new TextField();

inputText.setOnAction((ActionEvent inputText1) -> {
String inputText2 = inputText.getText();
double inputText3 = Double.parseDouble(inputText2);
System.out.println(inputText3);
});
Label blockSize = new Label(" Block Size: ");
TextField block = new TextField();

block.setOnAction((ActionEvent block1) -> {
String block2 = block.getText();
double block3 = Double.parseDouble(block2);
System.out.println(block3);
});

Button go = new Button("Go ");
ToggleGroup tg = new ToggleGroup();

selection.setToggleGroup(tg);
selection.setSelected(true);
bubble.setToggleGroup(tg);
insertion.setToggleGroup(tg);
quick.setToggleGroup(tg);
ToggleGroup tg1 = new ToggleGroup();
sorted.setToggleGroup(tg1);
sorted.setSelected(true);
reverse.setToggleGroup(tg1);
random.setToggleGroup(tg1);

gp.add(sort, 0, 0);
gp.add(selection, 0, 1);
gp.add(bubble, 0, 2);
gp.add(insertion, 0, 3);
gp.add(quick, 0, 4);
gp.add(inputType, 0, 7);
gp.add(sorted, 0, 8);
gp.add(reverse, 0, 9);
gp.add(random, 0, 10);
gp.add(inputSize, 0, 12);
gp.add(inputText, 1, 12);
gp.add(blockSize, 0, 13);
gp.add(block, 1, 13);
gp.add(go, 0, 16);

go.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent go1) {
//selection sorted
if (selection.isSelected() && sorted.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int chunk = Integer.parseInt(block.getText());//block size user input

// for(int i=0;i<block.length;i+=chunk){
// System.out.println(Arrays.toString(Arrays.copyOfRange(block, i, Math.min(block.length,i+chunk))));
// }
int[] array = getSorted(arraySize, true);
print(array);
selectionSort(array);

}//selction sorted reverse
else if (selection.isSelected() && reverse.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = null;
array = getReverse(array);
print(array);
selectionSort(array);
} //selection sorted random
else if (selection.isSelected() && random.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = getRandom(arraySize);
print(array);
selectionSort(array);
}//quick sort random
else if (quick.isSelected() && random.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = getRandom(arraySize);
print(array);
quickSort(array, 0, array.length - 1);

}//quick sort sorted
else if (quick.isSelected() && sorted.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = getSorted(arraySize, true);
print(array);
quickSort(array, 0, array.length - 1);
}//quick reverse sort
else if (quick.isSelected() && reverse.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = null;
array = getReverse(array);
print(array);
quickSort(array, 0, array.length - 1);
}//insertion sorted sort
else if (insertion.isSelected() && sorted.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = getSorted(arraySize, true);
print(array);
insertionSort(array);
}//insertion random sort
else if (insertion.isSelected() && random.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = getRandom(arraySize);
print(array);
insertionSort(array);
}//insertion reverse
else if (insertion.isSelected() && reverse.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = null;
array = getReverse(array);
print(array);
insertionSort(array);
}//bubble sort
else if (bubble.isSelected() && sorted.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = getSorted(arraySize, true);
print(array);
bubbleSort(array);
}//bubble random sort
else if (bubble.isSelected() && random.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = getRandom(arraySize);
print(array);
bubbleSort(array);
}//bubble reverse sort
else if (bubble.isSelected() && reverse.isSelected()) {
int arraySize = Integer.parseInt(inputText.getText());
int[] array = null;
array = getReverse(array);
print(array);
bubbleSort(array);
}
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Thread Sorted!");
alert.setHeaderText("Finished");
alert.setContentText("Sort completed in milliseconds ");
alert.showAndWait();
}
});
Scene scene = new Scene(rootPane, 500, 350);
primaryStage.setTitle("Project 4");
primaryStage.setScene(scene);
primaryStage.show();
}
//insertion sort

public static void insertionSort(int array[]) {
// int loopCount = 0;
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)) {
array[i + 1] = array[i];
i--;

}
array[i + 1] = key;
}
//return loopCount;
}
//quick sort

int partition(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];

while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}

return i;
}
//quick sort

public void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1) {
quickSort(arr, left, index - 1);
}
if (index < right) {
quickSort(arr, index, right);
}
//return index;
}
//bubble sort

public static void bubbleSort(int[] arr) {
int n = arr.length;
// int loopCount = 0;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}

}
}
//return loopCount;
}
//selection sort

public static void selectionSort(int[] arr) {

for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) {
index = j;
}

}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}

}

public static int[] getRandom(int size) {
Random rand = new Random();
int[] array = new int[size];
for (int i = 1; i <= size; i++) {
array[i - 1] = Math.abs(rand.nextInt()) % 100;
}
return array;
}

public static int[] getSorted(int size, boolean accending) {
int[] array = new int[size];
if (accending) {
for (int i = 1; i <= size; i++) {
array[i - 1] = i;
}
} else {
for (int i = size; i > 0; i--) {
array[size - i] = i;
}
}
return array;
}
public static int[] getReverse(int[] arrayw) {
int[] array = new int[arrayw.length];
for (int i = 0,j = array.length-1; i<array.length;i++,j--) {
array[j] = arrayw[i];
}
return array;
}


public static void print(int[] array) {

for (int i = 0; i < array.length; i++) {
System.out.print(" " + array[i]);
}
System.out.println();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

最佳答案

您潜在的 radio 选项是:

"selection, Already Sorted":"selection,Reverse":...:"Quick, Random"

您需要处理两个 ToggleGroup 之间的所有不同情况。

      if(selection.isSelected() && sorted.isSelected()){
int arraySize = Integer.parseInt(inputText.getText());

int[] array = getSorted(arraySize,true);
print(array);
int loopCount = selectionSort(array);

}
else if(selection.isSelected() && reverse.isSelected()
{
//do Something
}
else if(...)
{

}
.
.
.
else if(quick.isSelected() && random.isSelected())
{
//do Something
}

Added due to updated comment

现在你的打印方法应该如下所示:

public static void print(int[] array, int blockSize) {

for (int i = 0; i < array.length; i++) {
System.out.print(" " + array[i]);
if((i + 1) % blockSize == 0)
{
System.out.println();
}
}
System.out.println();
}

And the old call to the print methods change from:

print(array); 

To:

print(array, Integer.parseInt(block.getText())); 

关于javaFx 单选按钮和文本字段用户选择和用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43414513/

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