package application; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class ACSDHomework3III extends Application { TextField tfA = new TextField(); TextField tfB = new TextField(); TextField tfC = new TextField(); TextField tfD = new TextField(); Button btisDight = new Button(" is Dight "); Button btisNum = new Button(" is Num "); Button bttoUp = new Button(" to Up "); Button bttoLow = new Button(" to Low "); Button btisUp = new Button(" is Up "); @Override public void start(Stage primaryStage) { GridPane gridPane = new GridPane(); gridPane.setHgap(5); gridPane.setVgap(5); gridPane.add(new Label("Class ID:"), 0, 0); gridPane.add(tfA, 1, 0); gridPane.add(new Label("Name:"), 0, 1); gridPane.add(tfB, 1, 1); gridPane.add(new Label("A Character"), 1, 2); gridPane.add(new Label("Result"), 5, 3); gridPane.add(tfC, 1, 4); gridPane.add(tfD, 5, 4); gridPane.setAlignment(Pos.CENTER); tfD.setAlignment(Pos.BOTTOM_RIGHT); HBox hBox = new HBox(btisDight,btisNum,bttoUp,bttoLow,btisUp); hBox.getChildren().addAll(); hBox.setAlignment(Pos.CENTER); BorderPane borderPane = new BorderPane(); borderPane.setCenter(gridPane); borderPane.setBottom(hBox); btisDight.setOnAction(e -> isDight()); btisNum.setOnAction(e -> isNum()); bttoUp.setOnAction(e -> toUp()); bttoLow.setOnAction(e -> toLow()); btisUp.setOnAction(e -> isUp()); Scene scene = new Scene(borderPane, 500, 200); primaryStage.setTitle("William 201045"); primaryStage.setScene(scene); primaryStage.show(); } String C; private void isDight() { tfD = Charater.isDight(tfC.setText); } private void isNum() { tfC.setText((String.format("%.2f",Charater.isNum(C)))); } private void toUp() { tfC.setText((String.format("%.2f",Charater.toUpCase(C)))); } private void toLow() { tfC.setText((String.format("%.2f",Charater.toLowCase(C)))); } private void isUp() { tfC.setText((String.format("%.2f",Charater.isUpCase(C)))); } public static void main(String[] args) { launch(args); } }怎么修改错误的地方
时间: 2024-03-10 17:50:43 浏览: 67
这段代码中有几个错误需要修改:
1. 在 `isDight()` 方法中,应该使用 `tfC.setText()` 而不是 `tfD = Charater.isDight(tfC.setText)`。
2. 在 `isNum()`、`toUp()`、`toLow()`、`isUp()` 方法中,应该使用 `tfC.getText()` 获取文本框中的内容,而不是使用未定义的变量 `C`。
3. 在 `hBox.getChildren().addAll();` 中不应该传入任何参数。
下面是修改后的代码:
```
package application;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ACSDHomework3III extends Application {
TextField tfA = new TextField();
TextField tfB = new TextField();
TextField tfC = new TextField();
TextField tfD = new TextField();
Button btisDight = new Button(" is Dight ");
Button btisNum = new Button(" is Num ");
Button bttoUp = new Button(" to Up ");
Button bttoLow = new Button(" to Low ");
Button btisUp = new Button(" is Up ");
@Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(new Label("Class ID:"), 0, 0);
gridPane.add(tfA, 1, 0);
gridPane.add(new Label("Name:"), 0, 1);
gridPane.add(tfB, 1, 1);
gridPane.add(new Label("A Character"), 1, 2);
gridPane.add(new Label("Result"), 5, 3);
gridPane.add(tfC, 1, 4);
gridPane.add(tfD, 5, 4);
gridPane.setAlignment(Pos.CENTER);
tfD.setAlignment(Pos.BOTTOM_RIGHT);
HBox hBox = new HBox(btisDight, btisNum, bttoUp, bttoLow, btisUp);
hBox.setAlignment(Pos.CENTER);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(gridPane);
borderPane.setBottom(hBox);
btisDight.setOnAction(e -> isDight());
btisNum.setOnAction(e -> isNum());
bttoUp.setOnAction(e -> toUp());
bttoLow.setOnAction(e -> toLow());
btisUp.setOnAction(e -> isUp());
Scene scene = new Scene(borderPane, 500, 200);
primaryStage.setTitle("William 201045");
primaryStage.setScene(scene);
primaryStage.show();
}
private void isDight() {
tfD.setText(String.valueOf(Character.isDigit(tfC.getText().charAt(0))));
}
private void isNum() {
tfC.setText(String.valueOf(Character.isDigit(tfC.getText().charAt(0))));
}
private void toUp() {
tfC.setText(String.valueOf(Character.toUpperCase(tfC.getText().charAt(0))));
}
private void toLow() {
tfC.setText(String.valueOf(Character.toLowerCase(tfC.getText().charAt(0))));
}
private void isUp() {
tfC.setText(String.valueOf(Character.isUpperCase(tfC.getText().charAt(0))));
}
public static void main(String[] args) {
launch(args);
}
}
```
阅读全文