我正在尝试使用javafx-8创建一个CheckBox,但是没有定义与CheckBox(String str)相关联的标签的构造函数的定义类似于setSelected方法和setText方法未在库中定义。 我已将java更新到最新版本的Java SE 8u131,但问题仍然存在。 这是代码
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;import javafx.stage.Stage;
public class CheckBox extends Application {
Stage window;
Scene scene;
Button button;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("JavaFX");
//Checkboxes
CheckBox box1 = new CheckBox("apples");
CheckBox box2 = new CheckBox("Tuna");
box2.setSelected(true);
//Button
button = new Button("Order Now!");
button.setOnAction(e -> handleOptions(box1, box2));
//Layout
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20, 20));
layout.getChildren().addAll(box1, box2, button);
scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
//Handle checkbox options
private void handleOptions(CheckBox box1, CheckBox box2){
String message = "Users order:\n";
if(box1.isSelected())
message += "Bacon\n";
if(box2.isSelected())
message += "Tuna\n";
System.out.println(message);
}
}
这里是错误日志
CheckBox.java:24: error: constructor CheckBox in class CheckBox cannot be applied to given types;
CheckBox box1 = new CheckBox("apples");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
CheckBox.java:25: error: constructor CheckBox in class CheckBox cannot be applied to given types;
CheckBox box2 = new CheckBox("Tuna");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
CheckBox.java:26: error: cannot find symbol
box2.setSelected(true);
^
symbol: method setSelected(boolean)
location: variable box2 of type CheckBox
CheckBox.java:35: error: method addAll in interface ObservableList<E> cannot be applied to given types;
layout.getChildren().addAll(box1, box2, button);
^
required: Node[]
found: CheckBox,CheckBox,Button
reason: varargs mismatch; CheckBox cannot be converted to Node
where E is a type-variable:
E extends Object declared in interface ObservableList
CheckBox.java:46: error: cannot find symbol
if(box1.isSelected())
^
symbol: method isSelected()
location: variable box1 of type CheckBox
CheckBox.java:49: error: cannot find symbol
if(box2.isSelected())
^
symbol: method isSelected()
location: variable box2 of type CheckBox
我正在使用eclipse ide,但它也没有问题,因为我已经尝试通过ubuntu的shell编译它。 我该如何纠正这个? 在此先感谢!
答案 0 :(得分:2)
CheckBox是您的班级名称,因此请重命名您的班级名称并导入javafx复选框
import javafx.scene.control.CheckBox;
或使用完全合格的名称。
javafx.scene.control.CheckBox box1 = new javafx.scene.control.CheckBox("apples");
javafx.scene.control.CheckBox box2 = new javafx.scene.control.CheckBox("Tuna");
在handleOptions()方法
中private void handleOptions(javafx.scene.control.CheckBox box1, javafx.scene.control.CheckBox box2) {