QuizCardGame

news/2024/9/23 6:25:24/

文章目录

  • 前言
  • 一、QuizCard
  • 二、QuizCardBuilder
  • 三、QuizCardPlayer


前言

《Head First Java》QuizCard示例代码


一、QuizCard

/*** Title      : QuizCard.java* Description: This class contains the definition of the QuizCard.** @author    : Zhonghao Yan* @version   : 18/05/2022*/public class QuizCard {private String question;private String answer;QuizCard(String question, String answer) {this.question = question;this.answer = answer;}//    public void setQuestion(String question) { this.question = question; }
//    public void setAnswer(String answer) { this.answer = answer; }public String getQuestion() {return question;}public String getAnswer() {return answer;}
}

二、QuizCardBuilder

import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;/*** Title      : QuizCardBuilder.java* Description: This class contains the definition of the QuizCardBuilder*              which can design and store the card.** @author    : Zhonghao Yan* @version   : 18/05/2022*/public class QuizCardBuilder {private JTextArea question;private JTextArea answer;private ArrayList<QuizCard> cardList;public JFrame frame;public static void main(String[] args) {QuizCardBuilder builder = new QuizCardBuilder();builder.go();}/***  Run the builder to create and store quiz card*/public void go() {frame = new JFrame("Quiz Card Builder");JPanel mainPanel = new JPanel();Font bigFont = new Font("sanserif", Font.BOLD, 24);question = new JTextArea(6, 20);question.setLineWrap(true); // 自动换行question.setWrapStyleWord(true);question.setFont(bigFont);JScrollPane qScroller = new JScrollPane(question);qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);answer = new JTextArea(6, 20);answer.setLineWrap(true); // 自动换行answer.setWrapStyleWord(true);answer.setFont(bigFont);JScrollPane aScroller = new JScrollPane(answer);aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);JButton nextBUtton = new JButton("Next Card");cardList = new ArrayList<>();JLabel qLabel = new JLabel("Question:");JLabel aLabel = new JLabel("Answer:");/*创建菜单,把new与save项目驾到File下,然后指定frame使用这个菜单,菜单项目会触发ActionEvent*/mainPanel.add(qLabel);mainPanel.add(qScroller);mainPanel.add(aLabel);mainPanel.add(aScroller);nextBUtton.addActionListener(new NextCardListener());JMenuBar menuBar = new JMenuBar();JMenu fileMenu = new JMenu("File");JMenuItem newMenuItem = new JMenuItem("New");JMenuItem saveMenuItem = new JMenuItem("Save");newMenuItem.addActionListener(new NewMenuListener());saveMenuItem.addActionListener(new SaveMenuListener());fileMenu.add(newMenuItem);fileMenu.add(saveMenuItem);menuBar.add(fileMenu);frame.setJMenuBar(menuBar);frame.getContentPane().add(BorderLayout.CENTER, mainPanel);frame.setSize(500, 600);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}/*** Monitor the action when we create next quiz card*/public class NextCardListener implements ActionListener {public void actionPerformed(ActionEvent event) {QuizCard card = new QuizCard(question.getText(), answer.getText());cardList.add(card);clearCard();}}/*** Monitor the action when we save the quiz card we have created*/public class SaveMenuListener implements ActionListener {public void actionPerformed(ActionEvent event) {QuizCard card = new QuizCard(question.getText(), answer.getText());cardList.add(card);/*showSaveDialog(Component parent)显示保存文件的对话框参数parent: 文件选取器对话框的父组件, 对话框将会尽量显示在靠近 parent 的中心; 如果传 null, 则显示在屏幕中心。File getSelectedFile()返回一个File对象,获取选择的文件*/JFileChooser fillSave = new JFileChooser();fillSave.showSaveDialog(frame);saveFile(fillSave.getSelectedFile());}}/*** Monitor the action when we create new file*/public class NewMenuListener implements ActionListener {public void actionPerformed(ActionEvent event) {cardList.clear();clearCard();}}/*** Clear the JTextArea to wait next input*/private void clearCard() {question.setText("");answer.setText("");question.requestFocus();}/*** Save the data of the quiz card* @param file The object of File we choose in the dialog*/private void saveFile(File file) {try {/*将BufferWriter链接到FileWriter效率会更高,先在缓存区暂存一些内容,等缓存区满了后一并写入如果需要强制缓存区立即写入,调用flush()方法即可*/BufferedWriter writer = new BufferedWriter(new FileWriter((file)));for (QuizCard card : cardList) {// 将ArrayList中的卡片逐个写到文件中,一行一张卡片writer.write(card.getQuestion() + "/");writer.write(card.getAnswer() + "\n");}writer.close();} catch (IOException ex) {System.out.println("couldn't write the cardList out");ex.printStackTrace();}}}

三、QuizCardPlayer

import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;/*** Title      : QuizCardPlayer.java* Description: This class contains the definition of the QuizCardPlayer*              which can let users play the game.** @author    : Zhonghao Yan* @version   : 18/05/2022*/public class QuizCardPlayer {private JTextArea display;private JTextArea answer;private ArrayList<QuizCard> cardList;private QuizCard currentCard;private int currentCardIndex;private JFrame frame;private JButton nextButton;private boolean isShowAnswer;public static void main(String[] args) {QuizCardPlayer reader = new QuizCardPlayer();reader.go();}private void go() {frame = new JFrame("Quiz Card Player");JPanel mainPanel = new JPanel();Font bigFont = new Font("sanserif", Font.BOLD, 24);display = new JTextArea(10, 20);display.setFont(bigFont);display.setLineWrap(true);display.setEditable(false); // 禁止编辑JScrollPane qScroller = new JScrollPane(display);qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);nextButton = new JButton("Show Question");mainPanel.add(qScroller);mainPanel.add(nextButton);JMenuBar menuBar = new JMenuBar();JMenu fileMenu = new JMenu("File");JMenuItem loadMenuItem = new JMenuItem("Load card set");loadMenuItem.addActionListener(new OpenMenuListener());fileMenu.add(loadMenuItem);menuBar.add(fileMenu);frame.setJMenuBar(menuBar);frame.getContentPane().add(BorderLayout.CENTER, mainPanel);frame.setSize(640, 500);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}/*** Monitor the action when we click the next button*/public class NextCardListener implements ActionListener {public void actionPerformed(ActionEvent ev) {if (isShowAnswer) {// 显示答案display.setText(currentCard.getAnswer());nextButton.setText("Next Card");isShowAnswer = false;} else {if (currentCardIndex < cardList.size()) {showNextCard();} else {// 没有更多的卡片了display.setText("That is the last card");nextButton.setEnabled(false); // 禁用按钮}}}}/*** Monitor the action when we click menu*/public class OpenMenuListener implements ActionListener {public void actionPerformed(ActionEvent ev) {JFileChooser fileOpen = new JFileChooser();fileOpen.showSaveDialog(frame);loadFile(fileOpen.getSelectedFile());}}/*** Read the file, and split the String of line to make card* @param file The object of File we choose in the dialog*/public void loadFile(File file) {cardList = new ArrayList<>();try {BufferedReader reader = new BufferedReader(new FileReader(file));String line = null;while ((line = reader.readLine()) != null) {// 读取一行数据,传给makeCard(),将字符串解析成卡片makeCard(line);}reader.close();} catch (Exception ex ) {System.out.println("Couldn't read the card file");ex.printStackTrace();}}/*** Split the String of the line to question and answer* @param lineToParse One line of the file.*/private void makeCard(String lineToParse) {String[] result = lineToParse.split("/");QuizCard card  = new QuizCard(result[0], result[1]);cardList.add(card);System.out.println("made a card");}public void showNextCard() {currentCard = cardList.get(currentCardIndex);currentCardIndex++;display.setText(currentCard.getQuestion());nextButton.setText("Show Answer");isShowAnswer = true;}}

http://www.ppmy.cn/news/390157.html

相关文章

.Card卡片

开发工具与关键技术&#xff1a;VS .Card 作者&#xff1a;微凉之夏 撰写日期&#xff1a;2019年06月16日 .Card卡片是个灵活的可扩展的内容窗口&#xff0c;同时可以做出很多种展示效果的变体&#xff0c;而.card卡片组件在BootStrap4中是新增的一组重要样式&#xff0c;它是…

Web Components 系列(十一)—— 实现 MyCard 的可复用

前言 在上一节中&#xff0c;使用 Templates 实现了 MyCard 的基本布局&#xff0c;并且在文章结尾我也说过&#xff0c;因为不可复用&#xff0c;其实用性基本为零。 今天我们通过使用具名 Slots 在 Templates 中占位&#xff0c;然后再在自定义元素中给 Slots 传值&#xf…

Credit Card

题意&#xff1a;现在有一张银行卡&#xff0c;每天傍晚都会进行一些交易&#xff0c;如果交易金额大于0&#xff0c;则账户余额增加相应的金额&#xff0c;如果交易金额小于0&#xff0c;则账户扣除相应的金额&#xff0c;如果等于0&#xff0c;则对账户金额进行检查&#xff…

卡片....

小蓝有 &#xfffd;k 种卡片, 一个班有 &#xfffd;n 位同学, 小蓝给每位同学发了两张卡片, 一 位同学的两张卡片可能是同一种, 也可能是不同种, 两张卡片没有顺序。没有 两位同学的卡片都是一样的。 给定 &#xfffd;n, 请问小蓝的卡片至少有多少种? import java.util.Sca…

MyCart

//*********************************fragment************************************************************************************************** //购物车接口的实现 //*************************************************************** cartAdapter.setOnCartListene…

[MyCat]MyCat下载及安装

1MyCat下载及安装 1.1 MySQL安装与启动 JDK&#xff1a;要求jdk必须是1.7及以上版本 MySQL&#xff1a;推荐mysql是5.5以上版本 MySQL安装与启动步骤如下&#xff1a;( 步骤1-5省略 ) &#xff08;1&#xff09;将MySQL的服务端和客户端安装包&#xff08;RPM&#xff09;上传到…

MyPsnCard 我的奖杯卡 V1.0 发布

应用简介 本应用是一个android widget程序&#xff0c;用于在android系统中&#xff0c;根据用户输入的PSNID等信息&#xff0c; 动态生成用户的Portable ID信息&#xff0c;也就是常说的奖杯卡信息。 应用特性 无需登陆PSN&#xff0c;获取任何指定PSNID的奖杯信息 支持自定…

卡片 .....

小蓝有很多数字卡片&#xff0c;每张卡片上都是数字 00 到 99。 小蓝准备用这些卡片来拼一些数&#xff0c;他想从 11 开始拼出正整数&#xff0c;每拼一个&#xff0c;就保存起来&#xff0c;卡片就不能用来拼其它数了。 小蓝想知道自己能从 11 拼到多少。 例如&#xff0c;当…