I created a program that will display a question, then the user has to answer the question within a time limit. I want to make it so for long questions, the question will just go to the next line instead of cutting off.

Is there another way to display the text so it just goes on the next line below it?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Write a description of class Question here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Question extends JFrame
{
    JTextField ansF;
    JLabel que, label, output;
    JPanel panel;
    String q, a, guess;
    Timer countdownTimer;
    int timeRemaining = 10;

    /**
     * Constructor for objects of class Question
     */
    public Question(String question, String answer)
    {
        super ("Question");
        JPanel panel = new JPanel();
        panel.setLayout (new GridLayout(0,1));
        panel.setBorder(BorderFactory.createEmptyBorder(10, // top
                30, // left
                10, // bottom
                30) // right
        );

        q = question;
        a = answer;

        que = new JLabel ("Question: " + q);
        ansF = new JTextField (15);
        output = new JLabel ("");

        label = new JLabel(String.valueOf(timeRemaining));
        label.setHorizontalAlignment(SwingConstants.CENTER ); 
        countdownTimer = new Timer(1000, new CountdownTimerListener());

        panel.add(que);
        panel.add(ansF);
        panel.add(output);
        panel.add(label);
        add(panel);

        TextFieldHandler handler2 = new TextFieldHandler ();
        ansF.addActionListener (handler2);

        countdownTimer.start();

        setLocation (400, 300);
        setSize (400, 200);
        setBackground(new Color (238,238,238));
        int state = super.getExtendedState();
        state &= ~JFrame.ICONIFIED;
        super.setExtendedState(state);
        super.setAlwaysOnTop(true);
        super.toFront();
        super.requestFocus();
        setResizable(false);
        setVisible (true);
    }

    class CountdownTimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (--timeRemaining > 0) {
                label.setText(String.valueOf(timeRemaining));
            } else {
                countdownTimer.stop();
                ansF.setEnabled(false);
                label.setText("Time's up!");
                output.setText("Answer: " + a);
            }
        }
    }

    private class TextFieldHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            if (event.getSource () == ansF)
            {
                countdownTimer.stop();
                ansF.setEnabled(false);
                guess = event.getActionCommand ();
                if (guess.equalsIgnoreCase(a)) {
                    output.setText("Correct!");
                }
                else {
                    output.setText("Incorrect: the answer is " + a);
                }
            }
        }
    }

    private class ButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
        }
    }

    public static void main () {
        Question app = new Question("I want to make this text fit on the screen, how do I do that?", "The answer to this question");
    }
}

I updated the code so the screen's width will depend on how long the question is. I multiplied the length of the question by 8 and used that as the width of the screen. But I still want a better method to make the questions fit on the screen.

Anybody?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Write a description of class Question here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Question extends JFrame
{
    JTextField ansF;
    JLabel que, label, output;
    JPanel panel;
    String q, a, guess;
    Timer countdownTimer;
    int timeRemaining = 10;

    /**
     * Constructor for objects of class Question
     */
    public Question(String question, String answer)
    {
        super ("Question");
        JPanel panel = new JPanel();
        panel.setLayout (new GridLayout(0,1));
        panel.setBorder(BorderFactory.createEmptyBorder(15, // top
                30, // left
                15, // bottom
                30) // right
        );

        q = question;
        a = answer;
        
        String text = ("Question: " + q);

        que = new JLabel (text);
        ansF = new JTextField (10);
        output = new JLabel ("");

        label = new JLabel(String.valueOf(timeRemaining));
        label.setHorizontalAlignment(SwingConstants.CENTER ); 
        countdownTimer = new Timer(1000, new CountdownTimerListener());

        panel.add(que);
        panel.add(ansF);
        panel.add(output);
        panel.add(label);
        add(panel);

        TextFieldHandler handler2 = new TextFieldHandler ();
        ansF.addActionListener (handler2);

        countdownTimer.start();

        setLocation (400, 300);
        setSize(8*text.length(), 200);
//         setSize (400, 200);
        setBackground(new Color (238,238,238));
        int state = super.getExtendedState();
        state &= ~JFrame.ICONIFIED;
        super.setExtendedState(state);
        super.setAlwaysOnTop(true);
        super.toFront();
        super.requestFocus();
//         setResizable(false);
        setVisible (true);
    }

    class CountdownTimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (--timeRemaining > 0) {
                label.setText(String.valueOf(timeRemaining));
            } else {
                countdownTimer.stop();
                ansF.setEnabled(false);
                label.setText("Time's up!");
                output.setText("Answer: " + a);
            }
        }
    }

    private class TextFieldHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            if (event.getSource () == ansF)
            {
                countdownTimer.stop();
                ansF.setEnabled(false);
                guess = event.getActionCommand ();
                if (guess.equalsIgnoreCase(a)) {
                    output.setText("Correct!");
                }
                else {
                    output.setText("Incorrect: the answer is " + a);
                }
            }
        }
    }

    private class ButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
        }
    }

    public static void main () {
        Question app = new Question("I want to make this text fit on the screen, how do I do that?", "The answer to this question");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.