Question
Please add on to code: JTextArea appending to answers written JScrollPane attached to the JTextArea import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import javax.swing.*; import java.awt.event.*;
Please add on to code:
JTextArea appending to answers written
JScrollPane attached to the JTextArea
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Project2 extends JFrame {
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JLabel jLabel1;
private JTextField jTextField1;
private JTextArea textarea;
private JLabel label;
private JTextField jtf;
String[] answers = new String[100];
//ArrayList
String greatest = "", smallest = "";
public int i = 0;
private int countQuest = 1;
private int countSession = 1;
private static final String filename = "test.txt";
public Project2() {
JOptionPane.showMessageDialog(null, "Welcome!Please press 'OK' to start.");
initComponents();
init();
}
public void init()
{
jButton2.setVisible(false);
jButton3.setVisible(false);
jTextField1.setVisible(false);
}
@SuppressWarnings("unchecked")
private void initComponents() {
jButton1 = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Start Session");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1ActionPerformed(e);
}
});
textarea = new JTextArea(40,40);
textarea.setEditable(false);
JScrollPane scrollpane = new JScrollPane(textarea);
getContentPane().add(textarea);
getContentPane().add(scrollpane);
getContentPane().setLayout(new GridLayout(0, 1,1, 1));
JPanel jpBottom = new JPanel();
jpBottom.setBackground(Color.YELLOW);
jpBottom.setLayout(new BorderLayout());
getContentPane().add(jpBottom);
jLabel1 = new javax.swing.JLabel();
getContentPane().add(jLabel1);
jTextField1 = new javax.swing.JTextField();
getContentPane().add(jTextField1);
jButton3 = new javax.swing.JButton();
jButton3.setText("Finish Session");
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton2 = new javax.swing.JButton();
jButton2.setText("Next Question");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
getContentPane().add(jButton1);
getContentPane().add(jpBottom);
pack();
}
private void jButton1ActionPerformed(ActionEvent evt) {
this.setSize(700, 500);
jTextField1.setVisible(true);
jButton2.setVisible(true);
jButton1.setVisible(false);
String Question = getNextQuestion();
jLabel1.setText("Session #" + countSession + " Q" + countQuest + ": " + Question);
countQuest++;
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
BufferedWriter bw = null;
FileWriter fw = null;
String answer = jTextField1.getText();
if(answer.length() > greatest.length())
greatest = answer;
if(answer.length() < smallest.length())
smallest = answer;
jTextField1.setText("");
String Question = getNextQuestion();
jLabel1.setText("Session #" + countSession + " Q" + countQuest + ": " + Question);
countQuest++;
if(countQuest > 10)
{
countSession++;
countQuest = 1;
jButton1.setVisible(true);
}
i=i+1;
answers[i] = answer;
if(i==9)
{
jButton3.setVisible(true);
jButton2.setVisible(false);
jButton1.setVisible(true);
}
}
private void jButton3ActionPerformed(ActionEvent evt) {
jTextField1.setVisible(false);
jButton1.setVisible(true);
jButton2.setVisible(false);
jButton3.setVisible(true);
jLabel1.setText("wow " + greatest + " and " + smallest + " seem important to you");
System.exit(0);
}
public void jButton2actionPerformed(ActionEvent e) {
JButton jbtnClicked = (JButton) e.getSource();
String textFromField = jtf.getText();
if(jbtnClicked.equals(jButton2)){
//write to a file
textarea.append(textFromField);
}
}
public String QuestionBank(int randomQuest){
String[] questions = new String[10]; //increase array size if you will add more questions
questions[0]= "Which three words describe you best?";
questions[1]= "Which is your best feature?";
questions[2]= "Which common saying or phrase describes you?";
questions[3]= "Whats the best thing thats happened to you this week?";
questions[4]= "Who was your role model when you were a child?";
questions[5]= "Who was your favorite teacher and why?";
questions[6]= "What was your favorite subject at school?";
questions[7]= "What did you want to be when you grew up?";
questions[8]= "If you could have one wish come true what would it be?";
questions[9]= "Which would you prefer three wishes over five years or one wish right now?";
return questions[randomQuest];
}
public String getNextQuestion()
{
Random r = new Random();
int my_random = r.nextInt((9-0)+1)+0;
String question = QuestionBank(my_random);
return question;
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Project2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Project2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Project2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Project2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Project2().setVisible(true);
}
});
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started