Jump to content

Java problems


PC-2011

Recommended Posts

ok so at my school we use this website for practice quizes but half the time its down and half the time there a bug or something that crashed and you have to restart. so im trying to write a group of programs that we be the quizes but i have run into a few problems the first is the Jlabel dose not update and secound is when you click the button it dose not put the data entered into the textfield into the string.

Heres the code

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class GUI extends Frame implements ActionListener

{

    int i=0;

    JLabel l1 = new JLabel();

    JTextField tf1 = new JTextField(2);

    JButton b1;

    String temp;

    public GUI()

    {

        GUI_1();

    }

 

    public void GUI_1()

    {

        int i=0;

        String temp;

        Game game = new Game(26);

        JFrame jf=new JFrame();

        jf.setTitle("testGUI");

        jf.setSize(500,300);

        JLabel l1=new JLabel("Enter the "+game.getE(i)+"?");

        JTextField tf1=new JTextField();

        JButton b1=new JButton("Submit");

        b1.addActionListener(this);

        temp=tf1.getText();

        Container pane=jf.getContentPane();

        pane.setLayout(new GridLayout(3,1) );

        pane.add(l1);

        pane.add(tf1);

        pane.add(b1);

        temp=tf1.getText();

        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

        jf.setVisible(true);

    }

 

    public void actionPerformed(ActionEvent e)

    {

        if(true==e.getActionCommand().equals("Submit"))

        {

            submit();

        }

    }

 

    public void submit()

    {

        JFrame jf=new JFrame();

        Game game = new Game(26);

        System.out.println("Passed button");

        l1.setText("Enter the"+game.getE(i+1)+"?");

        repaint();

        temp=tf1.getText();

        System.out.println("Temp = "+temp);

        if(temp!=null || temp != "" || temp != " ")

        {

            System.out.println("Passed if Else");

            game.play(temp,i);

            System.out.println("Passed Answer");

            if(game.getB()==false)

            {

                JOptionPane.showMessageDialog(jf,"You entered "+temp+" for "+game.getE(i)+" and the answer was "+game.getS(i),"Incorret",JOptionPane.ERROR_MESSAGE);

            }

            else

            {

                JOptionPane.showMessageDialog(jf,"Correct");

            }

            i++;

        }

        else

        {

            JOptionPane.showMessageDialog(jf,"You must answer","Invalid Answer",JOptionPane.ERROR_MESSAGE);

        }

    }

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...