java界面随机点名程序

项目包下载在这里插入图片描述
界面部分

public class demo extends Application {
	
	public void start (Stage primaryStage) {
		
		BorderPane pane=new BorderPane();
	    //背景图片
	    ImageView imageView1=new ImageView(new Image("lucky.jpg"));
	    imageView1.setFitHeight(400);
	    imageView1.setFitWidth(390);
	    pane.getChildren().add(imageView1);	    	    
	    pane.setRight(getVBox());	   
	    Scene scene = new Scene(pane,500,400);
       
	    
	    primaryStage.setResizable(false);
		primaryStage.setTitle("随机点名程序 ( by ************ )");
		primaryStage.setScene(scene);	
		primaryStage.show();
		
	}
	
	private VBox getVBox() {
		
		VBox vBox=new VBox(15);
		vBox.setPadding(new Insets(100,10,15,15));
		vBox.setAlignment(Pos.TOP_RIGHT);
		Text text1=new Text("Name");
		vBox.getChildren().add(text1);
		text1.setFill(Color.BLUE);
		text1.setFont(Font.font("Verdana", FontWeight.BOLD, 40));
		Button btAdd=new Button("开始点名");
		vBox.getChildren().add(btAdd);
        btAdd.setOnAction(e -> {			
        	String Name = null;
			try {
				Name = GetLuckyName();
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
    		 text1.setText(Name);					
			
		});
				
		VBox.setMargin(btAdd, new Insets(100,0,0,45));
		return vBox;
		
		
	}

随机点名函数


public static String GetLuckyName() throws Exception {
		int i,j;
		int a=1;int b=107;//根据名单中有多少人数定义的,自行修改,下面的108也是
		class Student{
			String name;
		}
		
	   Student[] stu=new Student[108];
		java.io.File file=new java.io.File("stu.txt");
		Scanner input = new Scanner(file);
		for (i=0;i<108;i++) {
			stu[i]=new Student(); 
			stu[i].name=input.next();}
		j=a+(int)(Math.random()*b);
		input.close();
		return stu[j].name;
	}
	

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值