运行 代码时,JavaFX 应用程序 window 不显示
JavaFX application window doesn't show when running code
我正在制作一个 javafx 应用程序,当我 运行 它时 IDE 没有给出任何错误。应用程序 window 没有显示,但我可以在任务管理器中看到程序 运行ning。
我已经尝试 运行在 Eclipse 和 IntelliJ 中使用代码。
我尝试 运行创建一个只有标题的新应用程序,它可以正常工作,所以它与特定代码有关。
package main;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class Main extends Application {
public boolean isX=true;
public Button[] bs=new Button[9];
Label turn = new Label("Turn: X");
public int i=0;
public void start(Stage stage){
BorderPane bp = new BorderPane();
bp.setPrefSize(310, 350);
turn.setFont(Font.font(20));
bp.setTop(turn);
makeButtons(bs);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.add(bs[0],0,0);
grid.add(bs[1],1,0);
grid.add(bs[2],2,0);
grid.add(bs[3],0,1);
grid.add(bs[4],1,1);
grid.add(bs[5],2,1);
grid.add(bs[6],0,2);
grid.add(bs[7],1,2);
grid.add(bs[8],2,2);
bp.setCenter(grid);
stage.setScene(new Scene(bp));
stage.setTitle("Tic tac toe");
stage.show();
}
void makeButtons(Button[] bs){
while (i<bs.length){
bs[i]=new Button(" ");
bs[i].setFont(Font.font("Monospaced", 40));
bs[i].setPrefSize(90, 90);
bs[i].setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (isX){
isX=false;
bs[i].setText("X");
turn.setText("Turn: O");
} else {
isX=true;
bs[i].setText("O");
turn.setText("Turn: X");
}
}
});
}
}
public static void main(String[] args){
launch(Main.class);
}
}
更新:我完成了零和十字
package main;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class Main extends Application {
public boolean isX=true;
public Button[] bs=new Button[9];
Label turn = new Label("Turn: X");
int goes =0;
public void start(Stage stage){
BorderPane bp = new BorderPane();
bp.setPrefSize(310, 350);
turn.setFont(Font.font(20));
bp.setTop(turn);
makeButtons(bs);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.add(bs[0],0,0);
grid.add(bs[1],1,0);
grid.add(bs[2],2,0);
grid.add(bs[3],0,1);
grid.add(bs[4],1,1);
grid.add(bs[5],2,1);
grid.add(bs[6],0,2);
grid.add(bs[7],1,2);
grid.add(bs[8],2,2);
bp.setCenter(grid);
stage.setScene(new Scene(bp));
stage.setTitle("Noughts and crosses");
stage.show();
}
void makeButtons(Button[] bs){
for (int i=0;i<bs.length;i++){
bs[i]=new Button(" ");
bs[i].setFont(Font.font("Monospaced", 40));
bs[i].setPrefSize(90, 90);
bs[i].setOnAction(this::handleTurn);
}
}
private void handleTurn(ActionEvent e){
goes++;
if (goes>4&&won()){
return;
}
Button b = (Button) e.getSource();
if (!b.getText().equals(" ")){
return;
}
if (isX) {
isX = false;
b.setText("X");
turn.setText("Turn: O");
} else {
isX=true;
b.setText("O");
turn.setText("Turn: X");
}
if (goes==9){
turn.setText("Game over: not turns left");
}
}
private boolean won(){
//rows
for (int i=0;i< 7;i+=3){
if (!bs[i].getText().equals(" ")&&bs[i].getText().equals(bs[i+1].getText())&&bs[i].getText().equals(bs[i+2].getText())){
turn.setText(bs[i].getText()+" wins!");
return true;
}
}
//columns
for (int i=0;i<3;i++){
if (!bs[i].getText().equals(" ")&&bs[i].getText().equals(bs[i+3].getText())&&bs[i].getText().equals(bs[i+6].getText())){
turn.setText(bs[i].getText()+" wins!");
return true;
}
}
//diagonals
if (!bs[0].getText().equals(" ")&&bs[0].getText().equals(bs[4].getText())&&bs[0].getText().equals(bs[8].getText())){
turn.setText(bs[0].getText()+" wins!");
return true;
}
if (!bs[2].getText().equals(" ")&&bs[2].getText().equals(bs[4].getText())&&bs[2].getText().equals(bs[6].getText())){
turn.setText(bs[2].getText()+" wins!");
return true;
}
return false;
}
public static void main(String[] args){
launch(Main.class);
}
}
您没有在方法 makeButtons
的 while
循环中更改 i
的值,因此 while
循环永远不会终止。
此外,您应该向按钮添加动作侦听器而不是鼠标侦听器。参考this tutorial.
考虑以下代码。
(注:代码后有更多解释。)
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class Main extends Application {
public boolean isX=true;
public Button[] bs=new Button[9];
Label turn = new Label("Turn: X");
public void start(Stage stage){
BorderPane bp = new BorderPane();
bp.setPrefSize(310, 350);
turn.setFont(Font.font(20));
bp.setTop(turn);
makeButtons(bs);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.add(bs[0],0,0);
grid.add(bs[1],1,0);
grid.add(bs[2],2,0);
grid.add(bs[3],0,1);
grid.add(bs[4],1,1);
grid.add(bs[5],2,1);
grid.add(bs[6],0,2);
grid.add(bs[7],1,2);
grid.add(bs[8],2,2);
bp.setCenter(grid);
stage.setScene(new Scene(bp));
stage.setTitle("Tic tac toe");
stage.show();
}
void makeButtons(Button[] bs){
int i = 0;
while (i<bs.length){
bs[i]=new Button(" ");
bs[i].setFont(Font.font("Monospaced", 40));
bs[i].setPrefSize(90, 90);
bs[i].setOnAction(this::handleTurn);
i++;
}
}
private void handleTurn(ActionEvent event) {
Button button = (Button) event.getSource();
if (isX) {
isX = false;
button.setText("X");
turn.setText("Turn: O");
}
else {
isX = true;
button.setText("O");
turn.setText("Turn: X");
}
}
public static void main(String[] args){
launch(Main.class);
}
}
上面的代码使用了 method references,在 Java 8 中介绍过。下面是上面代码中的相关行:
bs[i].setOnAction(this::handleTurn);
只要单击任何按钮,就会执行方法 handleTurn
。
请注意,在该方法中,我通过调用方法 getSource
(class ActionEvent
)获得了单击的实际按钮。
这是我 运行 上述代码时的样子。
我正在制作一个 javafx 应用程序,当我 运行 它时 IDE 没有给出任何错误。应用程序 window 没有显示,但我可以在任务管理器中看到程序 运行ning。
我已经尝试 运行在 Eclipse 和 IntelliJ 中使用代码。 我尝试 运行创建一个只有标题的新应用程序,它可以正常工作,所以它与特定代码有关。
package main;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class Main extends Application {
public boolean isX=true;
public Button[] bs=new Button[9];
Label turn = new Label("Turn: X");
public int i=0;
public void start(Stage stage){
BorderPane bp = new BorderPane();
bp.setPrefSize(310, 350);
turn.setFont(Font.font(20));
bp.setTop(turn);
makeButtons(bs);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.add(bs[0],0,0);
grid.add(bs[1],1,0);
grid.add(bs[2],2,0);
grid.add(bs[3],0,1);
grid.add(bs[4],1,1);
grid.add(bs[5],2,1);
grid.add(bs[6],0,2);
grid.add(bs[7],1,2);
grid.add(bs[8],2,2);
bp.setCenter(grid);
stage.setScene(new Scene(bp));
stage.setTitle("Tic tac toe");
stage.show();
}
void makeButtons(Button[] bs){
while (i<bs.length){
bs[i]=new Button(" ");
bs[i].setFont(Font.font("Monospaced", 40));
bs[i].setPrefSize(90, 90);
bs[i].setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (isX){
isX=false;
bs[i].setText("X");
turn.setText("Turn: O");
} else {
isX=true;
bs[i].setText("O");
turn.setText("Turn: X");
}
}
});
}
}
public static void main(String[] args){
launch(Main.class);
}
}
更新:我完成了零和十字
package main;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class Main extends Application {
public boolean isX=true;
public Button[] bs=new Button[9];
Label turn = new Label("Turn: X");
int goes =0;
public void start(Stage stage){
BorderPane bp = new BorderPane();
bp.setPrefSize(310, 350);
turn.setFont(Font.font(20));
bp.setTop(turn);
makeButtons(bs);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.add(bs[0],0,0);
grid.add(bs[1],1,0);
grid.add(bs[2],2,0);
grid.add(bs[3],0,1);
grid.add(bs[4],1,1);
grid.add(bs[5],2,1);
grid.add(bs[6],0,2);
grid.add(bs[7],1,2);
grid.add(bs[8],2,2);
bp.setCenter(grid);
stage.setScene(new Scene(bp));
stage.setTitle("Noughts and crosses");
stage.show();
}
void makeButtons(Button[] bs){
for (int i=0;i<bs.length;i++){
bs[i]=new Button(" ");
bs[i].setFont(Font.font("Monospaced", 40));
bs[i].setPrefSize(90, 90);
bs[i].setOnAction(this::handleTurn);
}
}
private void handleTurn(ActionEvent e){
goes++;
if (goes>4&&won()){
return;
}
Button b = (Button) e.getSource();
if (!b.getText().equals(" ")){
return;
}
if (isX) {
isX = false;
b.setText("X");
turn.setText("Turn: O");
} else {
isX=true;
b.setText("O");
turn.setText("Turn: X");
}
if (goes==9){
turn.setText("Game over: not turns left");
}
}
private boolean won(){
//rows
for (int i=0;i< 7;i+=3){
if (!bs[i].getText().equals(" ")&&bs[i].getText().equals(bs[i+1].getText())&&bs[i].getText().equals(bs[i+2].getText())){
turn.setText(bs[i].getText()+" wins!");
return true;
}
}
//columns
for (int i=0;i<3;i++){
if (!bs[i].getText().equals(" ")&&bs[i].getText().equals(bs[i+3].getText())&&bs[i].getText().equals(bs[i+6].getText())){
turn.setText(bs[i].getText()+" wins!");
return true;
}
}
//diagonals
if (!bs[0].getText().equals(" ")&&bs[0].getText().equals(bs[4].getText())&&bs[0].getText().equals(bs[8].getText())){
turn.setText(bs[0].getText()+" wins!");
return true;
}
if (!bs[2].getText().equals(" ")&&bs[2].getText().equals(bs[4].getText())&&bs[2].getText().equals(bs[6].getText())){
turn.setText(bs[2].getText()+" wins!");
return true;
}
return false;
}
public static void main(String[] args){
launch(Main.class);
}
}
您没有在方法 makeButtons
的 while
循环中更改 i
的值,因此 while
循环永远不会终止。
此外,您应该向按钮添加动作侦听器而不是鼠标侦听器。参考this tutorial.
考虑以下代码。
(注:代码后有更多解释。)
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class Main extends Application {
public boolean isX=true;
public Button[] bs=new Button[9];
Label turn = new Label("Turn: X");
public void start(Stage stage){
BorderPane bp = new BorderPane();
bp.setPrefSize(310, 350);
turn.setFont(Font.font(20));
bp.setTop(turn);
makeButtons(bs);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.add(bs[0],0,0);
grid.add(bs[1],1,0);
grid.add(bs[2],2,0);
grid.add(bs[3],0,1);
grid.add(bs[4],1,1);
grid.add(bs[5],2,1);
grid.add(bs[6],0,2);
grid.add(bs[7],1,2);
grid.add(bs[8],2,2);
bp.setCenter(grid);
stage.setScene(new Scene(bp));
stage.setTitle("Tic tac toe");
stage.show();
}
void makeButtons(Button[] bs){
int i = 0;
while (i<bs.length){
bs[i]=new Button(" ");
bs[i].setFont(Font.font("Monospaced", 40));
bs[i].setPrefSize(90, 90);
bs[i].setOnAction(this::handleTurn);
i++;
}
}
private void handleTurn(ActionEvent event) {
Button button = (Button) event.getSource();
if (isX) {
isX = false;
button.setText("X");
turn.setText("Turn: O");
}
else {
isX = true;
button.setText("O");
turn.setText("Turn: X");
}
}
public static void main(String[] args){
launch(Main.class);
}
}
上面的代码使用了 method references,在 Java 8 中介绍过。下面是上面代码中的相关行:
bs[i].setOnAction(this::handleTurn);
只要单击任何按钮,就会执行方法 handleTurn
。
请注意,在该方法中,我通过调用方法 getSource
(class ActionEvent
)获得了单击的实际按钮。
这是我 运行 上述代码时的样子。