本篇博客是关于javafx的一个小尝试。
共有张无忌、赵敏、张三丰、殷素素、殷天正五人,用界面左侧的按钮代表他们。每个按钮被点击一次,表示对应的人被打了,此时在界面右侧要有相对应的文字描述,与此同时,其他人要做出相应的回应,比如说去帮忙。
效果图展示如下:
主界面的代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import static javafx.geometry.HPos.RIGHT;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Game extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Quiana Welcome");
Text scenetitle = new Text("Welcome!");
scenetitle.setId("welcome-text");
HBox hbScene = new HBox(10);
hbScene.setAlignment(Pos.CENTER);
hbScene.setPadding(new Insets(25, 25, 25, 25));
Button zhangwuji = new Button("张无忌");
Button zhaomin = new Button("赵敏");
Button zhangsanfeng = new Button("张三丰");
Button yinsusu = new Button("殷素素");
Button yintianzheng = new Button("殷天正");
VBox vbBtn = new VBox(10);
vbBtn.getChildren().add(scenetitle);
vbBtn.getChildren().add(zhangwuji);
vbBtn.getChildren().add(zhaomin);
vbBtn.getChildren().add(zhangsanfeng);
vbBtn.getChildren().add(yinsusu);
vbBtn.getChildren().add(yintianzheng);
hbScene.getChildren().add(vbBtn);
Button btn = new Button("Sign in");
final TextArea sentenses = new TextArea ();
sentenses.setId("sentenses");
hbScene.getChildren().add(sentenses);
zhangwuji.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
sentenses.setText(sentenses.getText()+Action.Beat(1));
}
});
zhaomin.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
sentenses.setText(sentenses.getText()+Action.Beat(2));
}
});
zhangsanfeng.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
sentenses.setText(sentenses.getText()+Action.Beat(3));
}
});
yinsusu.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
sentenses.setText(sentenses.getText()+Action.Beat(4));
}
});
yintianzheng.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
sentenses.setText(sentenses.getText()+Action.Beat(5));
}
});
Scene scene = new Scene(hbScene, 400, 275);
primaryStage.setScene(scene);
scene.getStylesheets().add(Game.class.getResource("Game.css").toExternalForm());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
动作的代码:
public class Action {
public static String Beat(int id) {
String string = "\n我是";
switch (id) {
case 1:
string = string + "张无忌,我被打了!\n";
break;
case 2:
string = string + "赵敏,我被打了!\n";
break;
case 3:
string = string + "张三丰,我被打了!\n";
break;
case 4:
string = string + "殷素素,我被打了!\n";
break;
case 5:
string = string + "殷天正,我被打了!\n";
break;
default:
break;
}
for (int i = 1; i < 6; i++) {
if (i != id) {
string = string + getHelp(i);
} else {
continue;
}
}
return string;
}
public static String getHelp(int id) {
String string = "";
switch (id) {
case 1:
string = string + "我是张无忌,我来帮你!\n";
break;
case 2:
string = string + "我是赵敏,我来帮你!\n";
break;
case 3:
string = string + "我是张三丰,我来帮你!\n";
break;
case 4:
string = string + "我是殷素素,我来帮你!\n";
break;
case 5:
string = string + "我是殷天正,我来帮你!\n";
break;
default:
break;
}
return string;
}
}
这只是最简单的一个demo,后续可能还会有改进。大家有什么想法的话,请尽管提。另外,所需要的的jar包的下载链接:
链接:https://pan.baidu.com/s/16mH1t-rsb6FaZj8GXhWmIg 提取码:pq34
复制这段内容后打开百度网盘手机App,操作更方便哦。