Create a class named App2 which provides a GUI window for entering & saving ques
ID: 3710202 • Letter: C
Question
Create a class named App2 which provides a GUI window for entering & saving question & answer text for a flashcard application.
1) The window will contain one panel that holds three text boxes (JTextFields), one button (JButton), and one QAData object.
a) The first two text boxes will be for answers and questions and should be labeled
b) Users can enter text into the text boxes
c) The third text box will contain a user-specified file path, e.g., C:/myfolder/myfile.dat
d) When the application launches, all text boxes will be empty
e) The button will be labeled update, i.e., JButton.setText("update");
f) Whenever the button is is clicked, the text boxes will be cleared
2) The App2 file should contain a supplementary class, QAData, which contains separate arrays of question and answer Strings and implements the Serializable interface. This class should follow the App2 class in the same file. When the button is clicked, the application will:
a) Load a QAData object from the user-specified file path (note: if no file is found with the path, the question & answer arrays should be created with size zero)
b) Add the String in the question text box to the question array
c) Add the String in the answer box to the answer array
d) Save the updated QAData object to the user-specified file path
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package swingprojects;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Prashant Tomer
*/
class QADate implements Serializable
{
List<String> Questions=new ArrayList();
List<String> Answers=new ArrayList();
public void updatepath(QADate data,String path,String Question,String Answer)
{
FileOutputStream file=null;
try{
//QADate data=new QADate();
Questions.add(Question);
Answers.add(Answer);
try {
file = new FileOutputStream(path);
} catch (FileNotFoundException ex) {
Logger.getLogger(QADate.class.getName()).log(Level.SEVERE, null, ex);
}
ObjectOutputStream write = new ObjectOutputStream(file);
write.writeObject(data);
System.out.println(Questions);
System.out.println(Answers);
write.close();
file.close();
}catch(Exception ex)
{
System.err.println("exception in your program"+ex);
}
}
}
public class QuizApp extends javax.swing.JPanel {
/**
* Creates new form QuizApp
*/
public QuizApp() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField5 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField1.setText("jTextField1");
jTextField3.setText("jTextField3");
jTextField2.setText("jTextField2");
jTextField4.setText("jTextField4");
jTextField4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField4ActionPerformed(evt);
}
});
jLabel1.setText("Enter Question");
jLabel2.setText("Enter Answer");
jButton1.setText("Update");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField5.setText("jTextField5");
jLabel3.setText("File path");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(56, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(183, 183, 183))
.addComponent(jTextField2)
.addComponent(jTextField4, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jTextField5)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(54, 54, 54))
);
}// </editor-fold>
private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String q=jTextField2.getText();
String a=jTextField4.getText();
String path=jTextField5.getText();
QADate obj=new QADate();
obj.updatepath(obj, path, q, a);
jTextField2.setText("");
jTextField4.setText("");
jTextField5.setText("");
// TODO add your handling code here:
}
public static void main(String[] args) {
QuizApp obj=new QuizApp();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
// End of variables declaration
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.