Frage

I am making a simple true or false quiz game, but I dont know which text field i should use to display quiz questions. i tried the JTextField but you can edit the text in it.. I just want the question Strings to be taken from question class and displayed in a text area.

Quiz class

public class Quiz
{
    private JFrame frame;
    private JButton yesButton;
    private JButton noButton;

    /**
     * Creates game interface
     */
    public Quiz()
    {
        makeFrame();

    }
     /**
     * Receive notification of an action.
     */
    public void actionPerformed(ActionEvent event) 
    { 
        System.out.println("Menu item: " + event.getActionCommand());
    }      
    /**
     * Quits the application.
     */
    private void quit()
    {
        System.exit(0);
    }
    /**
     * About pop up.
     */
    private void about()
    {
        JOptionPane.showMessageDialog(frame, 
                    "Quiz game version 1.0",
                    "About Quiz",
                    JOptionPane.INFORMATION_MESSAGE);
    }
      /**
     * About pop up.
     */
    private void howToPlay()
    {
        JOptionPane.showMessageDialog(frame, 
                    "This is a simple quiz game where you answer questions by pressing yes or no buttons",
                    "How to Play",
                    JOptionPane.INFORMATION_MESSAGE);
    }
     /**
     * Enable's or disable's buttons.
     */
    private void setButtonsEnabled(boolean status)
    {
        yesButton.setEnabled(status);
        noButton.setEnabled(status);
    }
    /**
     * Creates the frame and its content.
     */
    private void makeFrame()
    {
        frame = new JFrame("Quiz");    
        Container contentPane = frame.getContentPane();
        makeMenuBar(frame);

        //JPanel
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 0));

        yesButton = new JButton("True");
        panel.add(yesButton);

        noButton = new JButton("False");
        panel.add(noButton);

        contentPane.add(panel,BorderLayout.CENTER);
        // end of JPanel

        //score
        JLabel label = new JLabel("Score: ");
        contentPane.add(label, BorderLayout.NORTH);
        //end of score

        //combo box
        JComboBox noOfPlayers = new JComboBox();
        contentPane.add(noOfPlayers, BorderLayout.SOUTH);
        noOfPlayers.addItem("1 Player");
        noOfPlayers.addItem("2 Player");
        noOfPlayers.addItem("3 Player");
        noOfPlayers.addItem("4 Player");
        noOfPlayers.addItem("5 Player");
         // end of combo box

         //


         //


        //Shows the frame and places frame at the center of the screen
        frame.pack();
        setButtonsEnabled(false);

        //makes application start in the center of the screen
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);

        frame.setVisible(true);
War es hilfreich?

Lösung

You can't edit text in a JTextField if you call setEditable(false). You can't even tab onto it if you call setFocusable(false) on it as well.


Edit
You ask:

where do i set it? as i just tried JTextField qText = new JTextField(setEditable(false)); but its not working.. or do i have to make a new method and then call the method?

Your qText field should be a private instance field. Then you can call the methods in your Quiz class's constructor.

Andere Tipps

you can set property textfield.Editable = false -> user cannot edit it

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top