Java语言程序设计基础篇_编程练习题**16.28(显示幻灯片)

目录

题目:**16.28(显示幻灯片)

 习题思路

代码示例 

 结果展示


题目:**16.28(显示幻灯片)

  编程练习题15.30使用图像开发了一个幻灯片显示程序。使用文本文件重写编程练习题15.30来开发一个幻灯片显示程序。假设十个名为slide0.txt,slide1,...,slide9.txt的文本文件都存储在text目录下。每张幻灯片显示一个文件的文本,每张幻灯片持续显示一秒。幻灯片依次显示。当显示完最后一张幻灯片时,重新显示第一张,以此类推。使用一个文本区域显示幻灯片。

  •  习题思路
  1. 创建一个长度为10的文本列表File[10] ,用一个int类型的参数代表幻灯片当前的片数。
  2. 创建一个TextArea。
  3. 创建一个HBox,放置一个按钮Button,用于开始幻灯片。
  4. 创建一个BorderPane,将TextArea设置在中心,Button设置在底部。
  5. 定义一个获取字符串的方法(假设名字为getString()),获取传入的file参数,逐行读取文件中的文本,存入一个字符串中,最后返回字符串。
  6. 定义一个方法,每一次调用这个方法都会让int幻灯片片数的参数+1,获取File[int]中的文本并显示在TextArea上(使用⑤中定义的方法)。若int参数大于一个值,那么将int归零。
  7. 创建一个TimeLine,每隔一秒就调用一次⑥中定义的方法。
  8. 为③中创建方法注册一个事件,当按钮被按下后触发TimeLine.play()方法。
  • 代码示例 

编程练习题16_28DisplayTextSlides.java 

package chapter_16;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;

public class 编程练习题16_28DisplayTextSlides extends Application{
	private Timeline timeline;
	private TextArea textArea;
	private File[] fileList;
	private int count = 0;
	@Override
	public void start(Stage primaryStage) throws Exception {
		fileList = new File[10];
		for(int i = 0;i < 10;i++) {
			fileList[i] = new File("src/Text/slide"+i+".txt");
		}
		textArea = new TextArea();
		textArea.setFont(new Font(48));
		textArea.setEditable(false);
		Button btStart = new Button("Start");
		HBox hBox = new HBox(btStart);
		hBox.setAlignment(Pos.CENTER);
		BorderPane borderPane = new BorderPane();
		borderPane.setCenter(textArea);
		borderPane.setBottom(hBox);
		
		EventHandler<ActionEvent> handler = e->showNextSlide();
		timeline = new Timeline(new KeyFrame(Duration.millis(1000),handler));
		timeline.setCycleCount(Animation.INDEFINITE);
		btStart.setOnMouseClicked(e ->{
			timeline.play();
		});
		
		Scene scene = new Scene(borderPane,300, 300);
		primaryStage.setTitle("编程练习题16_28DisplayTextSlides");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public static void main(String[] args) {
		Application.launch(args);
	}
	public void showNextSlide() {
		String str = getString(fileList[count]);
		textArea.setText(str);
		count++;
		if(count > 9) {
			count = 0;
		}
	}
	public String getString(File file) {
		String str = "";
		try {
		Scanner scanner = new Scanner(file);
		while(scanner.hasNextLine()) {
			String line = scanner.nextLine();
			str += line;
		}
		scanner.close();
		}	
		catch (FileNotFoundException e) {
			System.out.println(e);
		}
		return str;
	}
}
  •  结果展示

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值