Skip to content

Instantly share code, notes, and snippets.

@gabrielruiu
Created May 3, 2014 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielruiu/b5e293a98c908995ab92 to your computer and use it in GitHub Desktop.
Save gabrielruiu/b5e293a98c908995ab92 to your computer and use it in GitHub Desktop.
package com.gabrielruiu.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Driver {
int i;
List<String> lines;
String s = "Welcome Students!";
String b = "Start!";
private JFrame f;
private JPanel p;
JFrame frame = new JFrame();
JButton b1 = new JButton(b);
JLabel jl = new JLabel(s);
private int clicked;
public Driver() {
gui();
}
public void gui() {
lines = readLinesFromFile();
i = 0;
f = new JFrame("Flash Card Program");
p = new JPanel();
f.setLayout(new GridLayout(2, 1));
f.add(jl);
f.add(p);
p.setLayout(new GridLayout(2, 1));
p.add(b1);
jl.setHorizontalAlignment(JLabel.CENTER);
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(500, 400); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jl.setText(lines.get(i));
i++;
if ( i > lines.size()-1 ) {
i = 0;
}
if (b1.getText().equals("Click For Answer")) {
b1.setText("Next Question");
} else {
b1.setText("Click For Answer");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (clicked++ == 10) {
Object[] options = { "No, thanks", "Yes, please" };
int response = JOptionPane.showOptionDialog(frame,
"Would you like more math questions? ",
"Math Questions", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[1]);
if (response == 1)
clicked = 1; // reset
else
System.exit(0);
}
}
});
}
public static List<String> readLinesFromFile() {
List<String> lines = new ArrayList<String>();
try {
Scanner scanner = new Scanner(new File("upload.txt"));
while (scanner.hasNext()){
lines.add(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return lines;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Driver();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment