Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for an intro to java class: The goal of this project is to create a GUI

ID: 3832101 • Letter: T

Question

This is for an intro to java class: The goal of this project is to create a GUI flashcard program that reads data from a text file. It is expected that you will base your work on your previous projects. Create a data structure to hold a series of flashcard objects. For this project, read the data from a text file. It is recommended but not required to either have the data in CSV format or have the questions and answers on alternating lines. To select the file, use a JFileChooser as discussed in class examples. Also submit an example file of your questions and answers. There should be at least 5 questions and answers in the text file.

When the program is launched the user should be presented with a JFileChooser which they will be use to select the data file to read. The GUI should consist of a single JLabel and JButton. On starting, the JLabel should display a welcome message and the JButton should display “Start.” After starting the first question should been displayed and the button should display “Click for Answer.” Upon the next click the label should display the answer and the button should display “Next Question.”

After the last question has been answered the program should display a dialog box asking the user if they want to go through the questions again. The main button should be disabled.

Just to be clear, in this project I want the data to be read from a text file. It is fine and expected to base your work on your previous project.

This is the program I'm starting with:

package fc;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

class FlashCard{
String question;
String answer;

public FlashCard( String q, String a ){
question = q;
answer = a;
}
}

public class FlashCardProgram{

static int q = 0;
static int nums[] = { 0,1,2,3 };

static int[] shuffleArray( int[] shuffle ){

Random rnd = new Random();
for (int i = shuffle.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = shuffle[index];
shuffle[index] = shuffle[i];
shuffle[i] = a;
}
return shuffle;
}

public static void main( String[] args ){
ArrayList< FlashCard > fc = new ArrayList<>();
fc.add( new FlashCard( "Question 1", "Answer 1" ) );
fc.add( new FlashCard( "Question 2", "Answer 2" ) );
fc.add( new FlashCard( "Question 3", "Answer 3" ) );
fc.add( new FlashCard( "Question 4", "Answer 4" ) );

nums = shuffleArray( nums );
JFrame f = new JFrame( "Flash Cards" );
JLabel lbl01 = new JLabel( "Welcome to Flashcards" );
JButton btn01 = new JButton( "Start" );

btn01.addActionListener( new ActionListener(){

@Override
public void actionPerformed( ActionEvent ae ) {
if( btn01.getText().equals( "Start" ) ){
lbl01.setText( fc.get( nums[q]).question );
btn01.setText( "Click for Answer" );
}
else if( btn01.getText().equals( "Click for Answer" ) ){
lbl01.setText( fc.get(nums[q]).answer );
q++;
btn01.setText( "Next Question" );
}
else if(btn01.getText().equals( "Next Question" )){
String input = "";
if(q>3){
input = JOptionPane.showInputDialog( "Want to start over?(Yes/No)" );
if(input.equalsIgnoreCase( "yes" )){
btn01.setText( "Start" );
lbl01.setText( "Welcome back" );
q = 0;
nums = shuffleArray( nums );
}
else System.exit( 0 );
}else{
lbl01.setText( fc.get(nums[q]).question );
btn01.setText( "Click for Answer" );
}
}
}
});

lbl01.setBounds( 50, 10, 500, 30 );
btn01.setBounds( 150, 200, 200, 30 );
f.getContentPane().setLayout( null );
f.getContentPane().add( lbl01 );
f.getContentPane().add( btn01 );
f.setVisible( true );
f.setSize( 500, 400 );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}


}

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** QuizCardBuilder - This class allows the user to create, edit and save a Deck of QuizCards. */
public class QuizCardBuilder {
private Deck deck;
private JButton button;
private JFileChooser fileChooser = new JFileChooser();
private JFrame frame;
private JTextArea answerText = new JTextArea();
private JTextArea questionText = new JTextArea();
private JPanel panel;

private QuizCardPlayer quizCardPlayer;


public QuizCardBuilder(Deck deck) {
this.deck = deck;
createQuizCardPlayer();
}

/** addCard - adds a QuizCard to the current Deck. */
private void addCard(){
deck.addQuizCard(getQuestionText().getText(), getAnswerText().getText());
setQuestionText(null);
setAnswerText(null);
}

void build() {
SwingUtilities.invokeLater(
() -> {
buildFrame();
buildContentPane();
buildMenuBar();
buildLabel(new JLabel("Question:"));
buildTextArea(questionText);
buildLabel(new JLabel("Answer:"));
buildTextArea(answerText);
buildButtonPanel();
displayFrame();
questionText.requestFocusInWindow();
}
);

}

private void buildButtonPanel() {
button = new JButton("Add");
button.setAlignmentX(Component.LEFT_ALIGNMENT);
button.addActionListener(ev -> addCard());
panel.add(button);
}

private void buildContentPane() {
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(5, 15, 0, 15));
frame.setContentPane(panel);
}

private void buildFrame() {
frame = new JFrame("Quiz card builder - " + deck.getFileName());
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setMinimumSize(new Dimension(400, 400));
frame.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
close();
}
}
);
}

private void buildLabel(JLabel label) {
label.setAlignmentX(Component.LEFT_ALIGNMENT);
label.setFont(FontConstants.labelFont);
panel.add(label);
}

private void buildMenuBar() {
JMenuBar jMenuBar = new JMenuBar();
JMenu file = new JMenu("File");
file.add(Open);
file.add(Save);
file.add(SaveAs);
file.add(Exit);

JMenu card = new JMenu("Deck");
card.add(ShuffleDeck);
card.add(Play);

jMenuBar.add(file);
jMenuBar.add(card);
frame.setJMenuBar(jMenuBar);
}

private void buildTextArea(JTextArea jTextArea) {
jTextArea.setWrapStyleWord(true);
jTextArea.setLineWrap(true);
jTextArea.setFont(FontConstants.textAreaFont);
jTextArea.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
deck.setIsModified(true);
}
});
JScrollPane jsp = new JScrollPane(jTextArea);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(jsp);
}

private void close(){
if (deck.getIsModified()) {
// Automatically closes the program if there's nothing to be saved.
if(deck.getQuizCardList().size() == 0 && getQuestionText().getText().length() == 0
&& getAnswerText().getText().length() == 0) {
System.exit(0);
}else {
int optionChosen = JOptionPane.showConfirmDialog(frame, "Do you want to save this deck?", "Save",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (optionChosen == JOptionPane.YES_OPTION) {
save();
}
if (optionChosen != JOptionPane.CANCEL_OPTION) {
System.exit(0);
}
}
}else{
System.exit(0);
}
}

/** createQuizCardPlayer - safely creates an instance of QuizCardPlayer, whilst allowing QuizCardPlayer to
* have a callback */
private void createQuizCardPlayer(){
quizCardPlayer = new QuizCardPlayer(deck);
quizCardPlayer.registerQuizCardBuilder(this); // registers the callback
}

private void displayFrame() {
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

/** openFile - opens a saved Deck */
private void openFile(){
int optionChosen = JOptionPane.YES_OPTION;
if(deck.getIsModified()){
optionChosen = JOptionPane.showConfirmDialog(frame, "Do you want to save this deck before " +
"opening another?", "Save", JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
if(optionChosen == JOptionPane.YES_OPTION){
save();
}
}

if(optionChosen != JOptionPane.CANCEL_OPTION && fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
deck = new Deck();
deck.readFile(fileChooser.getSelectedFile().getAbsolutePath());
setTitle(deck.getFileName());
setQuestionText(null);
setAnswerText(null);
}
}

/** save - Saves the current Deck under the same name, if previously saved. If the Deck is new,
* then saveAs is invoked */
private void save(){
if(deck.getFileName().equals("Untitled")){
saveAs();
}else{
if(getQuestionText().getText().length() > 0){
addCard();
}
deck.save(deck.getFileLocation());
deck.setIsModified(false);
}
}

/** saveAs - User gets to choose the filename that stores the current Deck */
private void saveAs(){
if(fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
if(getQuestionText().getText().length() > 0){
addCard();
}
deck.save(fileChooser.getSelectedFile().getAbsolutePath());
deck.setFileName(fileChooser.getSelectedFile().getName());
setTitle(deck.getFileName());
deck.setIsModified(false);
}
}


// GETTERS
private JTextArea getAnswerText() {
return answerText;
}

JTextArea getQuestionText() {
return questionText;
}


// SETTERS
private void setAnswerText(String text) {
SwingUtilities.invokeLater(() -> answerText.setText(text));
}

void setTextAreaEditability(boolean isEditable){
questionText.setEditable(isEditable);
answerText.setEditable(isEditable);
button.setEnabled(isEditable);
}

private void setTitle(String newTitle){
SwingUtilities.invokeLater(() -> frame.setTitle("Quiz Card Builder - " + newTitle));
}

private void setQuestionText(String text) {
SwingUtilities.invokeLater(() -> questionText.setText(text));
}


// ACTIONS
private Action Exit = new AbstractAction("Quit"){
@Override
public void actionPerformed(ActionEvent ev){
close();
}
};

private Action Open = new AbstractAction("Open"){
@Override
public void actionPerformed(ActionEvent ev){
openFile();
}
};

private Action Play = new AbstractAction("Begin test"){
@Override
public void actionPerformed(ActionEvent ev){
// Allows the user to open a file if no file is already open
if(deck.getQuizCardList().size() == 0) {
openFile();
}

// Prevents window from popping up if there's no QuizCards to use
if(deck.getQuizCardList().size() > 0) {
if (deck.getIsTestRunning()) {
Toolkit.getDefaultToolkit().beep();
quizCardPlayer.toFront();
} else {
deck.setIsTestRunning(true);
setTextAreaEditability(false);
createQuizCardPlayer();
quizCardPlayer.build();
}
}
}
};

private Action Save = new AbstractAction("Save"){
@Override
public void actionPerformed(ActionEvent ev){
save();
}
};

private Action SaveAs = new AbstractAction("Save as...") {
@Override
public void actionPerformed(ActionEvent e) {
saveAs();
}
};

private Action ShuffleDeck = new AbstractAction("Shuffle deck"){
@Override
public void actionPerformed(ActionEvent ev){
deck.shuffle();
}
};
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote