Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1.     import javax.swing.*;
  2.     import java.awt.*;
  3.     import java.awt.event.ActionEvent;
  4.     import java.awt.event.ActionListener;
  5.     import java.util.ArrayList;
  6.    
  7.     public class Ramka extends JFrame implements ActionListener {
  8.         public Ramka(ArrayList <Dokument> lista) {
  9.             this.dokumenty = lista;
  10.             this.mojPanel=new JPanel();
  11.             this.add(mojPanel);
  12.             wypelnijPanel();
  13.    
  14.         }
  15.         public ArrayList<Dokument> dokumenty;
  16.         public ArrayList<JTextField> polaTekstoweTytuly;
  17.         public ArrayList<JTextField> polaTekstoweTresci;
  18.         public JPanel mojPanel;
  19.         public ArrayList<JButton> przyciski;
  20.         public JButton dodaj;
  21.    
  22.         public void wypelnijPanel() {
  23.             mojPanel.removeAll();
  24.             GridLayout siatka = new GridLayout(dokumenty.size()+1, 3);
  25.             mojPanel.setLayout(siatka);
  26.             this.polaTekstoweTytuly = new ArrayList<JTextField>();
  27.             this.polaTekstoweTresci = new ArrayList<JTextField>();
  28.             this.przyciski = new ArrayList<JButton>();
  29.    
  30.             for (int i=0; i<dokumenty.size(); i++) {
  31.                 JTextField tytul = new JTextField();
  32.                 JTextField tresc = new JTextField();
  33.                 mojPanel.add(tytul);
  34.                 mojPanel.add(tresc);
  35.                 tytul.setText(dokumenty.get(i).getTytul());
  36.                 tresc.setText(dokumenty.get(i).getTresc());
  37.                 polaTekstoweTresci.add(tresc);
  38.                 polaTekstoweTytuly.add(tytul);
  39.                 JButton przycisk = new JButton("usuń");
  40.                 przycisk.addActionListener(this);
  41.                 przyciski.add(przycisk);
  42.                 mojPanel.add(przycisk);
  43.                 }
  44.                 JButton dodaj = new JButton("dodaj");
  45.                 mojPanel.add(dodaj);
  46.                 dodaj.addActionListener(this);
  47.             pack();
  48.         }
  49.    
  50.    
  51.         @Override
  52.         public void actionPerformed(ActionEvent e) {
  53.             Object zrodlo = e.getSource();
  54.             if (zrodlo==dodaj) {
  55.                 String tytul = JOptionPane.showInputDialog(null, "Nazwa dokumentu", "Nowy dokument", JOptionPane.OK_CANCEL_OPTION);
  56.                 String tresc = JOptionPane.showInputDialog(null, "Treść dokumentu", "Nowy dokument", JOptionPane.OK_CANCEL_OPTION);
  57.                 Dokument dokument = new Dokument (tytul, tresc);
  58.                 dokumenty.add(dokument);
  59.                 wypelnijPanel();
  60.             }
  61.                 else {
  62.                     for (int i=0; i<przyciski.size(); i++) {
  63.                          if (zrodlo.equals(przyciski.get(i))) {
  64.                             dokumenty.remove(i);
  65.                                 break;
  66.                          }
  67.                      }
  68.             wypelnijPanel(); }
  69.         }
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement