Thread: GUI problems.
View Single Post
  #1 (permalink)  
Old 12-16-2007, 09:41 PM
saytri saytri is offline
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
GUI problems.
I have a problem where i need to display the contents of a textfile in a dialogue box. I'm making a quiz, and after the user enters the password and choose which quiz to take, java calls the required texfiles and opens it. I have managed to call the textfile through java by using the buffered reader, but i wish to display it in a GUI box. Can you pls help me? Thanks a lot!


This is the code i've written:

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

import java.io.*;
import java.util.Scanner;

import java.util.Arrays;

public class GeographyQuiz_Menu
{


public static void main(String[] args)
{
JFrame jFrame;
jFrame = new JFrame();



JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz");
JOptionPane.showMessageDialog(null, "Good Luck");



char choice;
int i, choice1;
String yourChoice;
int Password;
String passString;



passString = JOptionPane.showInputDialog("Enter the Password");


//Password = passString.nextInt();
Password = Integer.parseInt(passString);


if (Password == 123)
{
JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu");
JOptionPane.showMessageDialog(null, "Welcome!\nThis is a Geography Quiz\nChoose from the following Menu:");


JOptionPane.showMessageDialog(null, "1. Plate Tectonics\n2. Rivers\n3. Rocks\n4. Quit");

passString = JOptionPane.showInputDialog("Enter your choice");
choice1 = Integer.parseInt(passString);

// Convert variable from string to integer
if(choice1<1 || choice1>4)
{
JOptionPane.showMessageDialog(null, "This is an invalid choice");
}

switch(choice1)
{
case 1: JOptionPane.showMessageDialog(null,"You are taking the Plate Tectonics Quiz");
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt")));

while (s.hasNext()) {
s.useDelimiter(",\\s*");
JOptionPane.showMessageDialog(null,s.next());
}
} finally {
if (s != null)
s.close();

break;

}
}

}

else
{
JOptionPane.showMessageDialog(null, "Invalid Password. Try Again");
}

}
}
Reply With Quote
Sponsored Links