How do I do this The overall goal of this program will be to

How do I do this?

The overall goal of this program will be to have resource directory in which you place a text file with definitions of questions, answers, and picture names. The resource directory will also have all the pictures you will refer to in the setup.txt file. You will have JButtons that will give another question. And one that will show the answer. There will be no score-keeping at all. When you slick the question, it will choose another questions (cycling or random) and it will show the question and associated picture. When you click the answer button, it will show the answer. Please make an array to hold the questions, and one to hold the answers, and a third Image array to hold all the pictures - they should be read when you start the program.

Solution


package game;

import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Game extends JFrame implements ActionListener{

ImageIcon im = null;
JPanel qp = new JPanel();
JLabel theLabel = null;
JTextField thePicture= null;
JTextField theQuestion = null;
JTextField theAnswer = null;
JLabel xlabel = null;
int counter = -1;
int maxQ = 0;
JPanel middle = null;;
JLabel dlabel = null;
String[] question= new String[3];
String[] answer= new String[3];
String[] picture= new String[3];
public Game() throws FileNotFoundException
{
  
Scanner in = new Scanner( new File (\"resource/set.txt\"));
for (int i =0 ; in.hasNextLine();i++)
{
question[i] = in.nextLine();
answer[i] = in.nextLine();
picture[i] = in.nextLine();
}
setSize(600,600);//Size of JFrame
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setVisible(true);//Sets if its visible.
       /*
       * JButton.
       */
       JButton startButton = new JButton(\"Answer\");//The JButton name.
       add(startButton);//Add the button to the JFrame.
       startButton.addActionListener(this);
JButton startButton = new JButton(\"Next\");//The JButton name.
       add(startButton);//Add the button to the JFrame.
       startButton.addActionListener(this);
  
}
public static void main(String[] args) throws FileNotFoundException
{
Game g = new Game();
  
}

@Override
public void actionPerformed(ActionEvent e) {
String x = e.getActionCommand();
if (x.equals(\"Quit\")) { System.exit(0); }
if (x.equals(\"Next\")) {
if (counter<0) { counter = 0; }
if (counter > maxQ) { counter = 0; }
theAnswer.setText(\"\");
theQuestion.setText(question[counter]);
String pp = String.format(\"rs/%s\",picture[counter]);
System.out.printf(\"Getting [%s]\ \",pp);
try { im = new ImageIcon(getClass().getClassLoader().getResource(pp)); }
catch(Exception xu) { im = new ImageIcon(getClass().getClassLoader().getResource(\"resource/Header.jpg\")); }
  
theLabel.setIcon(im);
// getContentPane().invalidate();
// getContentPane().validate();
counter++;
}
if (x.equals(\"Show\")) {
theAnswer.setText(answer[counter-1]);
}
}
}

How do I do this? The overall goal of this program will be to have resource directory in which you place a text file with definitions of questions, answers, and
How do I do this? The overall goal of this program will be to have resource directory in which you place a text file with definitions of questions, answers, and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site