Advertisement
Guest User

Untitled

a guest
May 27th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 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.io.*;
  6.  
  7. /**
  8.  * class extend Jframe
  9.  * class implements Actionlistner
  10.  */
  11. public class GUI extends JFrame implements ActionListener {
  12.     /**
  13.      * lage GUI
  14.      * four buttons
  15.      * create modelList
  16.      */
  17.     JButton leggTil, fjern, lagre, open;
  18.     DefaultListModel m;
  19.     JList list;
  20.  
  21.     GUI() {
  22.         this.setSize(450, 400);
  23.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.         this.setTitle("Bibliotek");
  25.         this.setLayout(new FlowLayout());
  26.  
  27.         leggTil = new JButton("Legg til en ny bok");
  28.         fjern = new JButton("Fjern bok");
  29.         lagre = new JButton("Lagre");
  30.         open = new JButton("Åpen");
  31.         m = new DefaultListModel();
  32.         list = new JList(m);
  33.  
  34.         this.add(list);
  35.         this.add(leggTil);
  36.         this.add(fjern);
  37.         this.add(lagre);
  38.         this.add(open);
  39.  
  40.         leggTil.addActionListener(this);
  41.         lagre.addActionListener(this);
  42.         fjern.addActionListener(this);
  43.         open.addActionListener(this);
  44.  
  45.         this.setVisible(true);
  46.     }
  47.  
  48.     @Override
  49.     public void actionPerformed(ActionEvent e) {
  50.         /**
  51.          * to add elements to the list
  52.          */
  53.         if (e.getSource().equals(leggTil)) {
  54.  
  55.             //create an object of Bok class
  56.  
  57.             Bok b = new Bok();
  58.  
  59.             String navn = JOptionPane.showInputDialog(this, "" +
  60.                     "Skriv bok navn?");
  61.             b.setNavn(navn);
  62.             String Forfatter = JOptionPane.showInputDialog(this, "" +
  63.                     "Skriv forfatters navn?");
  64.             b.setForfatter(Forfatter);
  65.             int sider = Integer.parseInt(JOptionPane.showInputDialog(this, "" +
  66.                     "Skriv antall sider!"));
  67.             b.setSider(sider);
  68.             m.addElement(b);
  69.         }
  70.  
  71.         /**
  72.          * to save the added elements
  73.          */
  74.         if (e.getSource().equals(lagre)) {
  75.             try {
  76.                 FileOutputStream fileOutputStream = new FileOutputStream("Bibliotek.txt");
  77.                 ObjectOutputStream o = new ObjectOutputStream(fileOutputStream);
  78.                 o.writeObject(m);
  79.                 o.close();
  80.                 fileOutputStream.close();
  81.             } catch (FileNotFoundException e1) {
  82.                 e1.printStackTrace();
  83.             } catch (IOException e1) {
  84.                 e1.printStackTrace();
  85.             }
  86.         }
  87.  
  88.         /**
  89.          * to open the saved lists
  90.          */
  91.         if (e.getSource().equals(open)) {
  92.             try {
  93.                 FileInputStream f = new FileInputStream("Bibliotek.txt");
  94.                 ObjectInputStream o = new ObjectInputStream(f);
  95.                 m = (DefaultListModel) o.readObject();
  96.                 list.setModel(m);
  97.             } catch (FileNotFoundException e1) {
  98.                 e1.printStackTrace();
  99.             } catch (IOException e1) {
  100.                 e1.printStackTrace();
  101.             } catch (ClassNotFoundException e1) {
  102.                 e1.printStackTrace();
  103.             }
  104.         }
  105.  
  106.         /**
  107.          * to remov one or more selected from the list
  108.          */
  109.         if (e.getSource().equals(fjern)) {
  110.             if (list.getSelectedIndex() != -1) {
  111.                 m.remove(list.getSelectedIndex());
  112.  
  113.             }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement