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");
}
}