Results 1 to 7 of 7

Thread: JFrame problem

  1. #1
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default JFrame problem

    I am making a quiz, where questions are stored in a textfile. I wish that when i call the file containing a set of 10 questions, the questions instead of being displayed in a dialogue box using JOptionPane, i wish that they are displayed instead in a JFrame, where the user can also type his answers. Can someone pls help me? Thanks a lot.

    This is the code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.Calendar;
     
    public class GQ extends JFrame implements ActionListener {
        private static final int FRAME_WIDTH = 1024;
        private static final int FRAME_HEIGHT = 763;
        private static final int FRAME_X_ORIGIN =0;
        private static final int FRAME_Y_ORIGIN = 0;
        
         private JTextField Title;
         private JLabel prompt;
         private JLabel prompt1; 
        AnswerStore answerStore = new AnswerStore();
        boolean timeForMore;
    
     
        
        public static void main (String[] args) {
            
            
           
    
            JOptionPane.showMessageDialog(null, "This is a Geography Quiz");
            JOptionPane.showMessageDialog(null, "Good Luck");
     
            
     
                GQ frame = new GQ();
                frame.setVisible(true);
           
        }
     
        public GQ() {
            
           
            
            Container contentPane;
            JButton button1, button2, button3, button4, button5;
            JTextField inputLine;
     
            setSize (FRAME_WIDTH, FRAME_HEIGHT);
            setTitle("Geography Quiz");
            setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
     
            contentPane = getContentPane();
           
            contentPane.setLayout(null);
     
            button1 = new JButton("Plate Tectonics");
            button2 = new JButton("Rivers");
            button3 = new JButton("Rocks");
            button4 = new JButton("Quit");
     
            button1.setBounds(270,225,900,50);
            button2.setBounds(270,325,900,50);
            button3.setBounds(270,425,900,50);
            button4.setBounds(270,525,900,50);
            
            button1.setFont(new Font("Arial", Font.BOLD, 30));
            button2.setFont(new Font("Arial", Font.BOLD, 30));
            button3.setFont(new Font("Arial", Font.BOLD, 30));
            button4.setFont(new Font("Arial", Font.BOLD, 30));
            
            button1.setBorder(BorderFactory.createRaisedBevelBorder());
            button2.setBorder(BorderFactory.createRaisedBevelBorder());
            button3.setBorder(BorderFactory.createRaisedBevelBorder());
            button4.setBorder(BorderFactory.createRaisedBevelBorder());
            
            
            button1.setBackground (Color.pink);
            button1.setForeground (Color.blue);
            button2.setBackground (Color.pink);
            button2.setForeground (Color.blue);
            button3.setBackground (Color.pink);
            button3.setForeground (Color.blue);
            button4.setBackground (Color.pink);
            button4.setForeground (Color.blue);
         
            
            prompt = new JLabel( );
    
            prompt.setText("This is a Geography Quiz");
            prompt.setSize(900,50);
            prompt.setLocation(540,30);
            prompt.setFont(new Font("Times New Roman", Font.BOLD, 40));
            prompt.setForeground(Color.orange);
            
            prompt1 = new JLabel( );
    
            prompt1.setText("Choose any of the following:");
    
            prompt1.setSize(900,80);
    
            prompt1.setLocation(600,100);
            
            prompt1.setFont(new Font("Times New Roman", Font.BOLD, 30));
            
    
            contentPane.add(prompt);
            contentPane.add(prompt1);
            contentPane.add(button1);
            contentPane.add(button2);
            contentPane.add(button3);
            contentPane.add(button4);
            
           
        
            button1.addActionListener(this);  // to open the textfiles
            button1.setActionCommand("b1");
            button2.addActionListener(this);
            button2.setActionCommand("b2");
            button3.addActionListener(this);
            button3.setActionCommand("b3");
            button4.addActionListener(this);
            button4.setActionCommand("b4");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            
        }
      
         public void actionPerformed(ActionEvent e) {
            String ac = e.getActionCommand();
            String[] questions = null;
            String[] answers = null;
            if (ac.equals("b1")) {
                questions = readFile("plate_tectonics.txt");
              
                answers = answerStore.tectonicAnswers;
            } else if(ac.equals("b2")) {
                questions = readFile("rivers.txt");
                answers = answerStore.riverAnswers;
            } else if(ac.equals("b3")) {
                questions = readFile("rocks.txt");
                answers = answerStore.rockAnswers;
            } else if (ac.equals("b4")) {
                System.exit(0);
            }
              
            
             askQuestions(questions, answers);
           
           
        }
        public void stopAndShowResults() {
            
            timeForMore = false;
        }
           
        
    
        
     
        private String[] readFile(String path) {
            Scanner s = null;
            StringBuilder sb = new StringBuilder();
            String separator = "\n";
            try {
                s = new Scanner(new BufferedReader(new FileReader(path)));
    //            s.useDelimiter(",\\s*");
                while (s.hasNext()) {
                    sb.append(s.nextLine() + separator);
    //                JOptionPane.showMessageDialog(null,s.next());
                }
            } catch(IOException e) {
                System.out.println("read error: " + e.getMessage());
            } finally {
                if (s != null) 
                    s.close();
    //            break;
            }
      
        }
     
        private void askQuestions(String[] questions, String[] answers) {
            
            int count = 0;
            int point = 0;
            
             
            for(int j = 0; j < questions.length; j++) {
               
               
                String input = JOptionPane.showInputDialog(null, questions[j]);
                
                if(answers[j].equals(input))
                  { 
                    
                    count++;
                    point++;
                    timeForMore = true;
                   
                  }
                else if(timeForMore==false)
                    break; 
         }
             
                
             
               
            
                JOptionPane.showMessageDialog(null, "You answered " + count +
                                          " out of " + questions.length +
                                          " questions correctly.");
                JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + ((point*100)/10) + " &#37; ");
            
              if(point>=0 && point<=3)
              {
                 
                  JOptionPane.showMessageDialog(null, "You need to Improve");
               }
               
               
             if(point>=4 && point<=7)
              {
                 
                  JOptionPane.showMessageDialog(null, "Good");
               }
               
               if(point>=8 && point<=10)
              {
                 
                  JOptionPane.showMessageDialog(null, "You did Great");
               }
                                        
          }
        }
     
    class AnswerStore  {
        String[] tectonicAnswers = {
            "Hellenic", "destructive", "100km", "Italy", "Wegner",
            "Mariana", "Sicily", "created", "constructive", "Mediterranean"
        };
        
        String[] riverAnswers = {
            "Gorges", "Meanders", "Levees", "Yes", "Less Economic Developed Countries",
            "crescent shaped lakes", "More Economic Developed Countries", "No", "River Discharge", "No"
        };
        
        String[] rockAnswers = {
            "40km", "Igneous Rock", "Sedimentary", "Basalt", "Organic",
            "pressure", "Oolites", "Igneous", "dark black", "basalt"
        };
    
     
    
    }
    Thanks. :)
    Last edited by saytri; 01-11-2008 at 08:07 AM.

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    What exactly do you need help with? Do you need help with reading the text file for the questions? If so, you should learn about FileReader.

    Java Code:
    try
    {
         String question = "";
         FileReader reader = new FileReader(plate_techtonics.txt);
         Scanner in = new Scanner(reader);
         //read the file and get the question
    }
    
    catch(IOException e)
    {
         System.out.println("Error processing file");
    }

  3. #3
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default

    No, i know how to read a textfile from Java. The problem is that when java reads the textfile, i want it to open the questions(which are saved on a textfile) in a JFrame instead of opening them in a JoptionPane.

    Thanks a lot.

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Ohhh ok. Um, i'm not much of a GUI expert, but maybe if, once you have extracted the questions to a String (or however you want to store them), you just construct a new JFrame. JFrame frame = new JFrame(); , put the questions on as labels, and then make it visible. I'm not totally sure if you can construct a JFrame in a class that extends JFrame, maybe, but I'm not sure. Give it a shot and see what happens. I'll also try to figure it out.

  5. #5
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default

    Yeah i understood. But i was wondering, is there maybe a method, that instead of making all the questions with labels, you can call the textfile and it will open all the questions in just one label, not having to assign labels for each and every question. Because i think if i use labels for every question, that the textfile become useless, i think.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default GUI options

    Hello saytri.

    What I would do, is create a JScrollPane that contains the questions. You can create a data structure to store all the labels for each question, like a Vector. Then, underneath each label you can have some component to take the input. All the components can be in the JScrollPane. This makes the program appear like a form that the user has to fill in. Then you can add a JButton underneath the JScrollPane to accept the input.

    An easier approach would be to create one JLabel and one input component for input. Then, you can have a "next" JButton. When the user reaches the last question it can change to a "finnish" button.

    I hope these ideas might be useful. Just ask if you need help to create a GUI. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default

    Ok, thanks. You ideas were very useful. I have written this code so far (code below), but my problem is do i have to create a JLabel for each and every question. I had already wrote the questions in a textfile, and so i was wondering if there is a way by which i could call this textfile, instead of having to write the questions all over again, because if so, i think than the textfile has become useless if i have to creat labels for each question.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class Ch14TextFrame1 extends JFrame implements ActionListener{
    
      private static final int FRAME_WIDTH = 1024;
      private static final int FRAME_HEIGHT= 768;
      private static final int FRAME_X_ORIGIN = 0;
      private static final int FRAME_Y_ORIGIN = 0;
      
      private static final int BUTTON_WIDTH = 80;
      private static final int BUTTON_HEIGHT = 30;
     
       private JLabel prompt;
      private JTextField inputLine;
    
      
      public static void main(String[] args){
          Ch14TextFrame1 frame = new Ch14TextFrame1();
          frame.setVisible(true);
          
        }
        
      public Ch14TextFrame1(){
          Container contentPane;
          JButton button1, button2, button3, button4, button5;
          setSize (FRAME_WIDTH, FRAME_HEIGHT);
          setResizable (true);
          setTitle ("Program Ch14TextFrame3");
          setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
          
          contentPane = getContentPane();
          contentPane.setLayout(null);
           
         
          
          
          
    
          
          
         
            button1 = new JButton("Finish");
              
          
            
            button1.setBounds(650,600,200,50);
       
           
            
            button1.setFont(new Font("Arial", Font.BOLD, 30));
           
            
            
            button1.setBorder(BorderFactory.createRaisedBevelBorder());
           
           
            
            
            button1.setBackground (Color.pink);
            button1.setForeground (Color.blue);
       
           
         
            
            prompt = new JLabel( );
    
            prompt.setText("Plate Tectonics Quiz");
            prompt.setSize(900,50);
            prompt.setLocation(300,30);
            prompt.setFont(new Font("Times New Roman", Font.BOLD, 40));
            prompt.setForeground(Color.red);
            contentPane.add(prompt);
            
            
            prompt = new JLabel( );
    
            prompt.setText(" 1)When the continents were joined together they were called  a) Hellenic b) Pangaea c) Mariana");
            prompt.setSize(900,70);
            prompt.setLocation(10,90);
            prompt.setFont(new Font("Times New Roman", Font.BOLD, 14));
            prompt.setForeground(Color.black);
            contentPane.add(prompt);
            inputLine = new JTextField();
            inputLine.setSize(100,20);
            inputLine.setLocation(625,115);
            contentPane.add(inputLine);
            inputLine.addActionListener(this);
            
            prompt = new JLabel( );
    
            prompt.setText(" 2) When continents move towards each other they are called   a) Constructive b) Destructive c) Conservative");
            prompt.setSize(900,70);
            prompt.setLocation(10,120);
            prompt.setFont(new Font("Times New Roman", Font.BOLD, 14));
            prompt.setForeground(Color.black);
            contentPane.add(prompt);
            inputLine = new JTextField();
            inputLine.setSize(100,20);
            inputLine.setLocation(700,145);
            contentPane.add(inputLine);
            inputLine.addActionListener(this);
            
             prompt = new JLabel( );
    
            prompt.setText(" 3) At what depth does a subducted plate melt? a) 100km b) 300km c) 20km");
            prompt.setSize(900,70);
            prompt.setLocation(10,150);
            prompt.setFont(new Font("Times New Roman", Font.BOLD, 14));
            prompt.setForeground(Color.black);
            contentPane.add(prompt);
            inputLine = new JTextField();
            inputLine.setSize(100,20);
            inputLine.setLocation(500,175);
            contentPane.add(inputLine);
            inputLine.addActionListener(this);
            
            
           
           
    
     
            contentPane.add(button1);
            button1.addActionListener(this);
            button1.setActionCommand("b1");
    
            
          
          setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() instanceof JButton) {
                JButton clickedButton = (JButton) event.getSource();
                
                String buttonText = clickedButton.getText();
                setTitle("You clicked" + buttonText);
                
            String ac = event.getActionCommand();
            if (ac.equals("b1")) {
            
            }
           
    
    }
      }
    }
    Thanks again. :)
    Last edited by saytri; 01-12-2008 at 01:52 PM.

Similar Threads

  1. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 04:09 PM
  2. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 03:53 AM
  3. Picture in a JFrame problem
    By saytri in forum New To Java
    Replies: 3
    Last Post: 01-12-2008, 10:44 AM
  4. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:33 PM
  5. Help with JFrame
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •