MrDoyle

storing GUI info in an array

Sep 9th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. class Quiz extends JFrame implements ActionListener
  5. {
  6.     JButton answerButton;
  7.     JPanel panel;
  8.     JLabel labelQs,label2, label3;
  9.     final JTextField text, text2;
  10.     JLabel scoreLabel;
  11.     int score = 0;
  12.     Quiz() {
  13.         labelQs = new JLabel();
  14.         labelQs.setText("Question: How many pickles did Peter Piper pick?");//questions label
  15.         scoreLabel = new JLabel("SCORE: ");
  16.  
  17.         label2 = new JLabel();
  18.         label2.setText("Answer:");//answer label
  19.         text = new JTextField(15);//answer textfield
  20.         text2 = new JTextField(15);//answer textfield
  21.         label3 = new JLabel();
  22.         label3.setText("Answer:");//answer label
  23.  
  24.         answerButton=new JButton("ANSWER");
  25.  
  26.         panel=new JPanel(new GridLayout(0,2));
  27.         panel.add(scoreLabel);
  28.         panel.add(labelQs);
  29.  
  30.         panel.add(label2);
  31.         panel.add(text);
  32.  
  33.         panel.add(label3);
  34.         panel.add(text2);
  35.         panel.add(answerButton);
  36.         add(panel,BorderLayout.CENTER);
  37.  
  38.         answerButton.addActionListener(this);
  39.         setTitle("QUIZ");
  40.     }
  41.  
  42.     public void LoadQuestion(String question)
  43.     {
  44.         labelQs.setText(question);
  45.     }
  46.     public void actionPerformed(ActionEvent ae)
  47.     {
  48.         String [] myStringArray = new String[2];
  49.         myStringArray[0] =text.getText();
  50.         myStringArray[1] =text2.getText();
  51.  
  52.         //JOptionPane.showMessageDialog(this,myStringArray[0]);
  53.         System.out.println(myStringArray[0]);
  54.         System.out.println(myStringArray[1]);
  55.  
  56.         JOptionPane.showMessageDialog(this,myStringArray);
  57.  
  58.  
  59.         /*if(answer.equals(" "))
  60.         {
  61.             JOptionPane.showMessageDialog(this,"Correct Answer");
  62.             score++;
  63.             scoreLabel.setText("SCORE: "+score);
  64.         }
  65.         else{
  66.             JOptionPane.showMessageDialog(this,"Wrong Answer");
  67.             scoreLabel.setText("SCORE: "+score);
  68.         } */
  69.     }
  70. }
  71. class QuizDemo
  72. {
  73.     public static void main(String arg[])
  74.     {
  75.         try
  76.         {
  77.             Quiz frame=new Quiz();
  78.             frame.setSize(800,100);
  79.             frame.setVisible(true);
  80.         }
  81.         catch(Exception e)
  82.         {JOptionPane.showMessageDialog(null, e.getMessage());}
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment