我正在尝试为剪刀石头布创建一个简单的程序,但是我无法在窗格中添加任何内容。我在做pane.getChildren()。add(goButton);它不会编译,并说不能应用。我敢肯定这很简单,但是与我在网上看到的代码相比,我的代码看起来是一样的。
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.*;
import java.awt.*;
public class Main extends Application {
// private String[] answers = {"rock", "paper", "scissors"};
// private TextField userOutputTF, compOutputTF;
// private Button goButton;
// private Label title;
// private VBox pane;
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.setTitle("Rock, Paper, Scissors");
VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);
String[] answers = {"rock", "paper", "scissors"};
Button goButton = new Button("Go");
pane.getChildren().add(goButton);
TextField userOutputTF = new TextField("Enter rock, paper, or scissors");
TextField compOutputTF = new TextField("");
compOutputTF.setEditable(false);
userOutputTF.setEditable(true);
goButton.addActionListener(e -> {
System.out.println("Handled Lambda listener");
System.out.println("Have fun!");
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void Deal() {
System.out.println("Hi");
}
}
答案 0 :(得分:2)
您正在使用AWT控件。 更改导入以使用javafx控件,您还需要更改按钮侦听器:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class Sample extends Application {
// private String[] answers = {"rock", "paper", "scissors"};
// private TextField userOutputTF, compOutputTF;
// private Button goButton;
// private Label title;
// private VBox pane;
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.setTitle("Rock, Paper, Scissors");
VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);
String[] answers = {"rock", "paper", "scissors"};
Button goButton = new Button("Go");
pane.getChildren().add(goButton);
TextField userOutputTF = new TextField("Enter rock, paper, or scissors");
TextField compOutputTF = new TextField("");
compOutputTF.setEditable(false);
userOutputTF.setEditable(true);
goButton.onMouseClickedProperty().addListener((obs, oldValue, newValue) -> {
System.out.println("Handled Lambda listener");
System.out.println("Have fun!");
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void Deal() {
System.out.println("Hi");
}
}
您还需要将VBox添加到FXML内的其他容器中,或仅将其添加到根中。