gpt4 book ai didi

java - JavaFX 中的复选框问题

转载 作者:行者123 更新时间:2023-12-01 09:14:29 28 4
gpt4 key购买 nike

我正在做一项作业,我正在使用复选框将文本从普通文本转换为粗体、斜体或两者兼而有之。但是,当我运行代码时,出现一些错误,提示我的复选框没有在 Oracle 上检查时接受字符串的构造函数,并且有一个接受字符串的构造函数,并且当我尝试将复选框添加到节点时,复选框无法转换为节点当我认为复选框天真地继承节点属性时,我的网格 Pane 。这是我的代码。感谢您提供任何可能的帮助。

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import java.util.*;
import javafx.scene.text.*;
import javafx.scene.control.*;
import java.lang.*;
import javafx.beans.binding.*;
import javafx.event.*;
//import javafx.scene.control.CheckBox;
//import javafx.scene.control.Button;

public class test extends Application{
protected Text name = new Text(50, 50, "Hello World");

protected BorderPane getPane() {
BorderPane masterPane = new BorderPane();

Pane textPane = new Pane();
textPane.getChildren().add(name);
//masterPane.setCenter(textPane);

GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(10));
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(textPane,0,0);
GridPane.setHalignment(textPane, HPos.CENTER);
GridPane.setValignment(textPane, VPos.CENTER);
masterPane.setCenter(gridPane);

// HBox buttonPane = new HBox(1000);
Button upBtn = new Button("Up");
Button downBtn = new Button("Down");
GridPane buttonGridPane = new GridPane();
buttonGridPane.add(upBtn,0,0);
buttonGridPane.add(downBtn,1,0);
buttonGridPane.setPadding(new Insets(10));
buttonGridPane.setHgap(5);
buttonGridPane.setVgap(5);
buttonGridPane.setHalignment(upBtn, HPos.CENTER);
buttonGridPane.setValignment(upBtn, VPos.CENTER);
buttonGridPane.setHalignment(downBtn, HPos.CENTER);
buttonGridPane.setValignment(downBtn, VPos.CENTER);
// buttonPane.getChildren().addAll(upBtn, downBtn);
// buttonPane.setAlignment(Pos.CENTER);
buttonGridPane.setStyle("-fx-border-color: cyan");
masterPane.setBottom(buttonGridPane);

upBtn.setOnAction(e -> name.setY(name.getY() - 10));
downBtn.setOnAction(e -> name.setY(name.getY() + 10));

return masterPane;
}

protected StackPane setPane(){

BorderPane newMasterPane = new BorderPane(getPane());
GridPane gridPane = new GridPane();
gridPane.setMinSize(1250, 500);
gridPane.setMaxSize(1250, 500);
StackPane root = new StackPane(gridPane);
NumberBinding maxScale = Bindings.min(root.widthProperty().divide(1250), root.heightProperty().divide(500));
gridPane.scaleXProperty().bind(maxScale);
gridPane.scaleYProperty().bind(maxScale);
gridPane.add(newMasterPane,0,0);

return root;
}

@Override
public void start(Stage primoStage) {

Scene scene = new Scene(setPane(), 1250, 500);
primoStage.setTitle("Assignment #7");
primoStage.setScene(scene);
primoStage.show();

}

public static void main(String[] args) {
launch(args);
}
}

class CheckBox extends test {
@Override
protected BorderPane getPane() {
BorderPane pane = super.getPane();

Font fontBoldItalic = Font.font("Comic Sans MS",
FontWeight.BOLD, FontPosture.ITALIC, 20);
Font fontBold = Font.font("Comic Sans MS",
FontWeight.BOLD, FontPosture.REGULAR, 20);
Font fontItalic = Font.font("Comic Sans MS",
FontWeight.NORMAL, FontPosture.ITALIC, 20);
Font fontNormal = Font.font("Comic Sans MS",
FontWeight.NORMAL, FontPosture.REGULAR, 20);

name.setFont(fontNormal);

GridPane fontGridPane = new GridPane();
fontGridPane.setPadding(new Insets(10));
fontGridPane.setHgap(5);
fontGridPane.setVgap(5);
fontGridPane.setStyle("-fx-border-color: cyan");

// VBox checkPane = new VBox(20);
// checkPane.setPadding(new Insets(5, 5, 5, 5));
// checkPane.setStyle("-fx-border-color: cyan");
CheckBox chkBold = new CheckBox("Bold");
CheckBox chkItalic = new CheckBox("Italic");
// checkPane.getChildren().addAll(chkBold, chkItalic);
// pane.setLeft(checkPane);
fontGridPane.add(chkBold,0,0);
fontGridPane.add(chkItalic,0,1);
fontGridPane.setHalignment(chkBold, HPos.CENTER);
fontGridPane.setValignment(chkBold, VPos.CENTER);
fontGridPane.setHalignment(chkItalic, HPos.CENTER);
fontGridPane.setValignment(chkItalic, VPos.CENTER);

EventHandler<ActionEvent> handler = e -> {
if (chkBold.isSelected() && chkItalic.isSelected()) {
text.setFont(fontBoldItalic); // Both check boxes checked
}
else if (chkBold.isSelected()) {
text.setFont(fontBold); // The Bold check box checked
}
else if (chkItalic.isSelected()) {
text.setFont(fontItalic); // The Italic check box checked
}
else {
text.setFont(fontNormal); // Both check boxes unchecked
}
};

chkBold.setOnAction(handler);
chkItalic.setOnAction(handler);

return pane;
}
}

最佳答案

使用您自己的CheckBox类,而不是javafx.scene.control.CheckBox

您仍然可以使用完全限定名称来引用 javafx.scene.control.CheckBox,例如

javafx.scene.control.CheckBox chkBold = new javafx.scene.control.CheckBox("Bold");

但也许您应该重新考虑命名您的类 CheckBox,因为这个名称无论如何都会产生误导(这是一个使用 CheckBoxes 的应用程序,而不是 CheckBox >).

关于java - JavaFX 中的复选框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40669834/

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