Advertisement
janevim

test PROG 21.3.

Apr 3rd, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.*;
  5.  
  6. public class Test extends JFrame{
  7. private JPanel mainPanel;
  8. private JTextField nameField;
  9. private JTextField surnameField;
  10. private JCheckBox checkBox;
  11. private JButton button1;
  12. private JMenu menuFile;
  13. private JMenuItem chooseFile;
  14. private JMenuItem reset;
  15.  
  16. public Test(){
  17. initComponents();
  18. button1.addActionListener(new ActionListener() {
  19. @Override
  20. public void actionPerformed(ActionEvent e) {
  21. nameField.setText("Karel");
  22. surnameField.setText("Dvořák");
  23. }
  24. });
  25. }
  26. public void initComponents(){
  27. setContentPane(mainPanel);
  28. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29. JMenuBar menuBar = new JMenuBar();
  30. setJMenuBar(menuBar);
  31. menuFile = new JMenu("Operace");
  32. menuBar.add(menuFile);
  33. chooseFile = new JMenuItem("Ulož do souboru...");
  34. menuFile.add(chooseFile);
  35. reset = new JMenuItem("Resetuj formulář");
  36. menuFile.add(reset);
  37. chooseFile.addActionListener(e -> openFile());
  38. reset.addActionListener(new ActionListener() {
  39. @Override
  40. public void actionPerformed(ActionEvent e) {
  41. nameField.setText(null);
  42. surnameField.setText(null);
  43. checkBox.setSelected(false);
  44. }
  45. });
  46. }
  47. public void openFile() {
  48. JFileChooser fc = new JFileChooser();
  49. fc.showOpenDialog(this);
  50. File vysledek = fc.getSelectedFile();
  51. try {
  52. Writer writer = new FileWriter(vysledek);
  53. writer.append(nameField+"\n");
  54. writer.append(surnameField+"\n");
  55. if(checkBox.isSelected()){
  56. writer.append("umí\n");
  57. }
  58. else {
  59. writer.append("neumí\n");
  60. }
  61.  
  62. } catch (IOException e) {
  63. throw new RuntimeException();
  64. }
  65. }
  66.  
  67.  
  68. public static void main(String[] args) {
  69. Test t = new Test();
  70. t.setTitle("Operace");
  71. t.pack();
  72. t.setVisible(true);
  73. }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement