Created
June 28, 2021 07:44
-
-
Save SvyatoslavMedov/9a099d99b7615989684fdc09620c1b3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
public class QuizCardBuilder { | |
private JTextArea question; | |
private JTextArea answer; | |
private ArrayList<QuizCard> cardList; | |
private JFrame frame; | |
public static void main(String[] args) { | |
QuizCardBuilder builder = new QuizCardBuilder(); | |
builder.go(); | |
// write your code here | |
} | |
public void go() { | |
frame = new JFrame("Quiz Card Builder"); | |
JPanel mainPanel = new JPanel(); | |
Font bigFont = new Font("sanserif", Font.BOLD,24); | |
question = new JTextArea(6,20); | |
question.setLineWrap(true); | |
question.setWrapStyleWord(true); | |
question.setFont(bigFont); | |
JScrollPane qScroller = new JScrollPane(question); | |
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); | |
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); | |
answer = new JTextArea(6,20); | |
answer.setLineWrap(true); | |
answer.setWrapStyleWord(true); | |
answer.setFont(bigFont); | |
JScrollPane aScroller = new JScrollPane(answer); | |
aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); | |
aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); | |
JButton nextButton = new JButton("Next Card"); | |
cardList = new ArrayList<QuizCard>(); | |
JLabel qLabel = new JLabel("Question: "); | |
JLabel aLabel = new JLabel("Answer: "); | |
mainPanel.add(qLabel); | |
mainPanel.add(qScroller); | |
mainPanel.add(aLabel); | |
mainPanel.add(aScroller); | |
mainPanel.add(nextButton); | |
nextButton.addActionListener(new NextCardListener()); | |
JMenuBar menuBar = new JMenuBar(); | |
JMenu fileMenu = new JMenu("File"); | |
JMenuItem newMenuItem = new JMenuItem("New"); | |
JMenuItem saveMenuItem = new JMenuItem("Save"); | |
newMenuItem.addActionListener(new NewMenuListener()); | |
saveMenuItem.addActionListener(new SaveMenuListener()); | |
fileMenu.add(newMenuItem); | |
fileMenu.add(saveMenuItem); | |
menuBar.add(fileMenu); | |
frame.setJMenuBar(menuBar); | |
frame.getContentPane().add(BorderLayout.CENTER, mainPanel); | |
frame.setSize(500,600); | |
frame.setVisible(true); | |
} | |
public class NextCardListener implements ActionListener { | |
public void actionPerformed(ActionEvent ev){ | |
QuizCard card = new QuizCard(question.getText(), answer.getText()); | |
cardList.add(card); | |
clearCard(); | |
} | |
} | |
public class SaveMenuListener implements ActionListener { | |
public void actionPerformed(ActionEvent ev) { | |
QuizCard card = new QuizCard(question.getText(), answer.getText()); | |
cardList.add(card); | |
JFileChooser fileSave = new JFileChooser(); | |
fileSave.showSaveDialog(frame); | |
saveFile(fileSave.getSelectedFile()); | |
} | |
} | |
public class NewMenuListener implements ActionListener { | |
public void actionPerformed (ActionEvent ev) { | |
cardList.clear(); | |
clearCard(); | |
} | |
} | |
private void clearCard() { | |
question.setText(""); | |
answer.setText(""); | |
question.requestFocus(); | |
} | |
private void saveFile(File file) { | |
try { | |
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
for (QuizCard card:cardList) { | |
writer.write(card.getQuestion() + "/"); | |
writer.write(card.getAnswer() + "\n"); | |
} | |
writer.close(); | |
}catch (IOException ex){ | |
System.out.println("couldn't write the cardList out"); | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment