Results 1 to 2 of 2
Thread: GUI problems.
- 12-16-2007, 08:41 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
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");
}
}
}
- 12-16-2007, 10:27 PM #2
A pseudo code suggestion:
Java Code:import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class GQM { 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\n" + "Choose 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"); String s = readFile("plate_tectonics.txt"); JOptionPane.showMessageDialog(null, s); // Or you can write this into a JTextArea in your JFrame center section. } } else { JOptionPane.showMessageDialog(null, "Invalid Password. Try Again"); } } private static String readFile(String path) { Scanner s = null; StringBuilder sb = new StringBuilder(); String separator = " "; // or "\n" try { s = new Scanner(new BufferedReader(new FileReader(path))); s.useDelimiter(",\\s*"); while (s.hasNext()) { sb.append(s.next(), separator); // JOptionPane.showMessageDialog(null,s.next()); } } finally { if (s != null) s.close(); break; } return sb.toString(); } }
Similar Threads
-
Problems in JSP : Need help
By raj4u in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 02-07-2008, 10:06 AM -
gui problems
By bluebirdjc in forum Advanced JavaReplies: 2Last Post: 07-23-2007, 05:38 PM -
a few problems
By gary in forum AWT / SwingReplies: 0Last Post: 07-11-2007, 04:57 PM -
problems with JPA
By Ed in forum New To JavaReplies: 2Last Post: 07-04-2007, 05:34 AM -
Problems with the jar
By Freddie in forum NetBeansReplies: 2Last Post: 05-29-2007, 03:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks