随机一个吧
package com.hwd.campus.trade.biz.controller;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.Random;
public class MenuApp extends Application {
private String[] menu = {"麻辣香锅", "盖饭", "饺子", "拉条子", "蘸水面", "刀削面", "魏家凉皮", "米线", "麻辣拌"};
private Label mondayLabel, tuesdayLabel, wednesdayLabel, thursdayLabel, fridayLabel;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("点餐---吧");
Button generateButton = new Button("生成菜单");
generateButton.setOnAction(event -> generateMenu());
HBox buttonBox = new HBox(10);
buttonBox.setAlignment(Pos.CENTER);
buttonBox.getChildren().add(generateButton);
mondayLabel = new Label();
tuesdayLabel = new Label();
wednesdayLabel = new Label();
thursdayLabel = new Label();
fridayLabel = new Label();
BorderPane root = new BorderPane();
root.setTop(buttonBox);
root.setCenter(createMenuLabels());
primaryStage.setScene(new Scene(root, 800, 200));
primaryStage.show();
}
private void generateMenu() {
Random random = new Random();
String[] weekMenu = new String[5];
weekMenu[0] = menu[random.nextInt(menu.length)];
weekMenu[1] = menu[random.nextInt(menu.length)];
while (weekMenu[1].equals(weekMenu[0])) {
weekMenu[1] = menu[random.nextInt(menu.length)];
}
weekMenu[2] = menu[random.nextInt(menu.length)];
while (weekMenu[2].equals(weekMenu[0]) || weekMenu[2].equals(weekMenu[1])) {
weekMenu[2] = menu[random.nextInt(menu.length)];
}
weekMenu[3] = menu[random.nextInt(menu.length)];
while (weekMenu[3].equals(weekMenu[0]) || weekMenu[3].equals(weekMenu[1]) || weekMenu[3].equals(weekMenu[2])) {
weekMenu[3] = menu[random.nextInt(menu.length)];
}
weekMenu[4] = menu[random.nextInt(menu.length)];
while (weekMenu[4].equals(weekMenu[0]) || weekMenu[4].equals(weekMenu[1]) || weekMenu[4].equals(weekMenu[2]) || weekMenu[4].equals(weekMenu[3])) {
weekMenu[4] = menu[random.nextInt(menu.length)];
}
mondayLabel.setText("周一:" + weekMenu[0]);
tuesdayLabel.setText("周二:" + weekMenu[1]);
wednesdayLabel.setText("周三:" + weekMenu[2]);
thursdayLabel.setText("周四:" + weekMenu[3]);
fridayLabel.setText("周五:" + weekMenu[4]);
}
private HBox createMenuLabels() {
HBox menuLabels = new HBox(50);
menuLabels.setAlignment(Pos.CENTER);
menuLabels.getChildren().addAll(mondayLabel, tuesdayLabel, wednesdayLabel, thursdayLabel, fridayLabel);
return menuLabels;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!