Create a class named App2 which provides a GUI window for entering & saving ques
ID: 3709939 • 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
Note: There are known problems with saving to a drive root folder (e.g., C:/) in Windows 10 and later. It is strongly recommended to test your application within a nested folder (e.g., C:/myfolder/).
Additional Constraints
The program should be implemented as a single file holding the public class App2. The application should shut down when the window is closed out.
Explanation / Answer
package javatutorial.net;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class SimpleJButton {
SimpleJButton(){
JFrame f=new JFrame("Button Example");
//submit button
JButton b=new JButton("Submit");
b.setBounds(100,100,140, 40);
//enter name label
//
JLabel label = new JLabel();
label.setText("Enter Name :");
label.setBounds(10, 10, 100, 100);
//empty label which will show event after button clicked
JLabel label1 = new JLabel();
label1.setBounds(10, 110, 200, 100);
//textfield to enter name
JTextField textfield= new JTextField();
textfield.setBounds(110, 50, 130, 30);
//add to frame
f.add(label1);
f.add(textfield);
f.add(label1);
f.add(textfield);
f.add(label); f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//action listener
b.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) {
textfield.setText("");
} }); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.