Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.*;
- public class Test extends JFrame{
- private JPanel mainPanel;
- private JTextField nameField;
- private JTextField surnameField;
- private JCheckBox checkBox;
- private JButton button1;
- private JMenu menuFile;
- private JMenuItem chooseFile;
- private JMenuItem reset;
- public Test(){
- initComponents();
- button1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- nameField.setText("Karel");
- surnameField.setText("Dvořák");
- }
- });
- }
- public void initComponents(){
- setContentPane(mainPanel);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JMenuBar menuBar = new JMenuBar();
- setJMenuBar(menuBar);
- menuFile = new JMenu("Operace");
- menuBar.add(menuFile);
- chooseFile = new JMenuItem("Ulož do souboru...");
- menuFile.add(chooseFile);
- reset = new JMenuItem("Resetuj formulář");
- menuFile.add(reset);
- chooseFile.addActionListener(e -> openFile());
- reset.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- nameField.setText(null);
- surnameField.setText(null);
- checkBox.setSelected(false);
- }
- });
- }
- public void openFile() {
- JFileChooser fc = new JFileChooser();
- fc.showOpenDialog(this);
- File vysledek = fc.getSelectedFile();
- try {
- Writer writer = new FileWriter(vysledek);
- writer.append(nameField+"\n");
- writer.append(surnameField+"\n");
- if(checkBox.isSelected()){
- writer.append("umí\n");
- }
- else {
- writer.append("neumí\n");
- }
- } catch (IOException e) {
- throw new RuntimeException();
- }
- }
- public static void main(String[] args) {
- Test t = new Test();
- t.setTitle("Operace");
- t.pack();
- t.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement