Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package notatnik;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.*;
- import javax.swing.*;
- public class Notatnik extends JFrame implements ActionListener, KeyListener {
- private int ile;
- public JDialog wybor = new JDialog();
- public int bufor1, bufor2;
- public int czcionkaWielkosc = 14;
- public String wybranaCzcionka = "Arial";
- public Font czcionkaGlowna = new Font(wybranaCzcionka, Font.PLAIN, czcionkaWielkosc);
- final JLabel licznik = new JLabel("0");
- final JTextArea tekst = new JTextArea();
- void setIle(int ile) {
- this.ile = ile;
- }
- int getIle() {
- return ile;
- }
- public Notatnik() {
- tekst.setWrapStyleWord(true);
- tekst.setLineWrap(true);
- tekst.addKeyListener(this);
- tekst.setFont(czcionkaGlowna);
- Toolkit tk = Toolkit.getDefaultToolkit();
- Dimension roz = tk.getScreenSize();
- int sz = roz.width;
- int wys = roz.height;
- bufor1 = sz;
- bufor2 = wys;
- setSize(sz / 2, wys / 2);
- setLocation(sz / 4, wys / 4);
- setTitle("Notatnik");
- setResizable(true);
- setJMenuBar(menu());
- JScrollPane scroll = new JScrollPane(tekst, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- setLayout(new BorderLayout());
- add(scroll, BorderLayout.CENTER);
- add(dolny(), BorderLayout.SOUTH);
- setIle(tekst.getText().length());
- }
- public JPanel dolny() {
- JPanel dolny = new JPanel(new FlowLayout(FlowLayout.LEFT));
- JLabel iloscZnakow = new JLabel("Ilość znaków :");
- dolny.add(iloscZnakow);
- dolny.add(licznik);
- return dolny;
- }
- public JMenuBar menu() {
- JMenuBar menu = new JMenuBar();
- JMenu plik = new JMenu("Plik");
- menu.add(plik);
- plik.setMnemonic('P');
- JMenu edycja = new JMenu("Edycja");
- menu.add(edycja);
- edycja.setMnemonic('E');
- JMenu format = new JMenu("Format");
- menu.add(format);
- format.setMnemonic('F');
- JMenu autor = new JMenu("O autorze");
- menu.add(autor);
- JMenuItem otworz = new JMenuItem("Otwórz");
- otworz.setMnemonic('O');
- JMenuItem zapisz = new JMenuItem("Zapisz");
- zapisz.setMnemonic('S');
- JMenuItem zakoncz = new JMenuItem("Zakończ");
- plik.add(otworz);
- otworz.setActionCommand("1");
- otworz.addActionListener(this);
- plik.add(zapisz);
- zapisz.setActionCommand("2");
- zapisz.addActionListener(this);
- plik.addSeparator();
- plik.add(zakoncz);
- zakoncz.setActionCommand("3");
- zakoncz.addActionListener(this);
- JMenuItem szukaj = new JMenuItem("Wyszukaj");
- JMenuItem zamien = new JMenuItem("Zamień");
- JMenu czcionka = new JMenu("Czcionka");
- JMenuItem czcionkaRozmiar = new JMenuItem("Rozmiar");
- JMenuItem czcionkaStyl = new JMenuItem("Wybór czcionki");
- czcionkaRozmiar.setActionCommand("4");
- czcionkaRozmiar.addActionListener(this);
- czcionka.add(czcionkaRozmiar);
- czcionka.add(czcionkaStyl);
- czcionkaStyl.setActionCommand("5");
- czcionkaStyl.addActionListener(this);
- JMenuItem wyczysc = new JMenuItem("Wyczyść");
- JMenu styl = new JMenu("Styl");
- JMenuItem test = new JMenuItem("test");
- edycja.add(wyczysc);
- edycja.add(szukaj);
- edycja.add(zamien);
- format.add(czcionka);
- format.add(styl);
- styl.add(test);
- otworz.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
- zapisz.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
- szukaj.setAccelerator(KeyStroke.getKeyStroke("ctrl F"));
- zamien.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));
- wyczysc.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
- return menu;
- }
- public static void main(String[] args) {
- Notatnik note = new Notatnik();
- note.setVisible(true);
- note.setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent wydarzenie) {
- switch (Integer.parseInt(wydarzenie.getActionCommand())) {
- case 1: {
- JFileChooser pliki = new JFileChooser(".");
- if (JFileChooser.APPROVE_OPTION == pliki.showOpenDialog(this)) {
- try {
- File f = pliki.getSelectedFile();
- setTitle("Notatnik " + f.getAbsolutePath());
- BufferedReader br = new BufferedReader(new FileReader(f));
- String temp = "";
- while (br.ready()) {
- temp += br.readLine() + "\n";
- }
- tekst.setText(temp);
- } catch (IOException e1) {
- }
- }
- break;
- }
- case 2: {
- JFileChooser pliki = new JFileChooser(".");
- if (JFileChooser.APPROVE_OPTION == pliki.showSaveDialog(this)) {
- try {
- File f = pliki.getSelectedFile();
- try (BufferedWriter bw = new BufferedWriter(new FileWriter(f + ".txt"))) {
- bw.write(tekst.getText());
- bw.flush();
- }
- } catch (IOException e2) {
- }
- }
- }
- case 3: {
- {
- System.exit(0);
- }
- break;
- }
- case 4: {
- {
- try {
- int czcionkaWielkosc = Integer.parseInt(JOptionPane.showInputDialog(null,
- "Rozmiar",
- "Proszę wpisać rozmiar czcionki",
- JOptionPane.INFORMATION_MESSAGE));
- Font czcionkaNowa = new Font(wybranaCzcionka, Font.PLAIN, czcionkaWielkosc);
- tekst.setFont(czcionkaNowa);
- } catch (NumberFormatException e2) {
- JOptionPane.showMessageDialog(null, "Proszę podać wartość liczbową", "Błąd", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- break;
- }
- case 5: {
- {
- Object[] opcje = new Object[]{};
- JOptionPane bufor = new JOptionPane("Proszę wybrać czcionkę",
- JOptionPane.QUESTION_MESSAGE,
- JOptionPane.DEFAULT_OPTION,
- null, opcje, null);
- String czcionki[]
- = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
- JComboBox combo1 = new JComboBox(czcionki);
- combo1.setEditable(true);
- bufor.add(combo1);
- wybor.pack();
- wybor.setSize(new Dimension(250, 155));
- wybor.setResizable(false);
- wybor.setLocation((bufor1 / 2) - 100, (bufor2 / 2) - 100);
- wybor.getContentPane().add(bufor);
- wybor.setVisible(true);
- wybor.setLayout(new FlowLayout());
- JButton ok = new JButton("OK");
- wybor.add(ok);
- ok.setActionCommand("6");
- ok.addActionListener(this);
- String wybranaCzcionka = combo1.getSelectedItem().toString();
- Font czcionkaNowa = new Font(wybranaCzcionka, Font.PLAIN, czcionkaWielkosc);
- tekst.setFont(czcionkaNowa);
- }
- }
- break;
- case 6: {
- wybor.dispose();
- }
- break;
- }
- }
- @Override
- public void keyTyped(KeyEvent ke) {
- }
- @Override
- public void keyReleased(KeyEvent ke) {
- licznik.setText(Integer.toString(tekst.getText().length()));
- }
- @Override
- public void keyPressed(KeyEvent ke) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement