Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xxx;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Label;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Random;
- import java.util.Scanner;
- import javax.swing.*;
- import javax.swing.border.BevelBorder;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- import com.l2fprod.common.swing.JTipOfTheDay;
- import com.l2fprod.common.swing.TipModel;
- import com.l2fprod.common.swing.tips.DefaultTip;
- import com.l2fprod.common.swing.tips.DefaultTipModel;
- import com.toedter.calendar.JDateChooser;
- import com.toedter.calendar.demo.DateChooserPanel;
- public class Licznik extends JFrame implements ActionListener, ChangeListener
- {
- /**
- *
- */
- JMenuBar menuBar;
- JMenu menuPlik, menuNarzedzia, menuObliczenia, menuPomoc;
- JMenuItem mOtworz, mZapisz, mWyjscie, mNarz1, mNarz2, mSuma, mSrednia,mMin,mMax, mOProgramie, mOAutorze;
- JButton jbDodaj, jbWyzeruj, jbWypelnij, jbZapisz, jbOblicz, button, jUtworz,jWyzeruj, jDrukuj, jUsun, jWypelnij, jSuma, jSrednia, jMin, jMax, jPomoc, jInfo;
- JLabel lWprowadzLiczbe, lNumerWiersza, lNumerKolumny, lObliczenia, author,status;
- JTextField fWprowadz, fNrWiersza, fNrKolumny;
- JSlider sNrWiersza, sNrKolumny;
- JTextArea area;
- JComboBox combo;
- JDialog dialog, Pomoc;
- JToolBar toolbar;
- JPanel panel;
- JTipOfTheDay tip;
- private String pole;
- int tempNrWiersza, tempNrKolumny;
- JTable table = new JTable (5,5);
- int [][] tab = new int[table.getColumnCount()][table.getRowCount()];
- String[] optionJOptionPane = { "Tak", "Nie" };
- tip=new JTipOfTheDay(tipList);
- Licznik()
- {
- addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- koniec_programu();
- }
- });
- zerowanie();
- setTitle("Licznik by Adrian Wieczorek");
- setDefaultCloseOperation(Licznik.EXIT_ON_CLOSE);
- setSize(570, 540);
- setLocationRelativeTo(null);
- panel = new JPanel();
- status = new JLabel("Status: ");
- status.setBounds(40, 630, 20, 20);
- panel.add(status);
- menuBar = new JMenuBar();
- menuPlik = new JMenu("Plik");
- mZapisz = new JMenuItem("Zapisz");
- mZapisz.addActionListener(this);
- mWyjscie = new JMenuItem("Wyjscie");
- menuPlik.add(mZapisz);
- menuPlik.addSeparator();
- menuPlik.add(mWyjscie);
- mWyjscie.addActionListener(this);
- mWyjscie.setAccelerator(KeyStroke.getKeyStroke("ctrl Z"));
- menuPomoc = new JMenu("Pomoc");
- mOAutorze = new JMenuItem("O Autorze");
- mOAutorze.addActionListener(this);
- menuPomoc.add(mOAutorze);
- mOProgramie = new JMenuItem("O Programie");
- mOProgramie.addActionListener(this);
- menuPomoc.add(mOProgramie);
- setJMenuBar(menuBar);
- menuBar.add(menuPlik);
- menuBar.add(menuPomoc);
- //JDialog - info o autorze
- dialog = new JDialog();
- dialog.setTitle("O autorze");
- dialog.setLocationRelativeTo(null);
- dialog.setSize(300, 180);
- JPanel aaa = new JPanel();
- aaa = (JPanel)dialog.getContentPane();
- BoxLayout grid = new BoxLayout(aaa,BoxLayout.PAGE_AXIS);
- aaa.setLayout(grid);
- aaa.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));// góra,lewo,dół,prawo
- aaa.add(new JLabel("Program: Projekt cw 1"));
- aaa.add(new JLabel("Wersja programu: v1.0"));
- aaa.add(new JLabel("Wykonawca: Adrian Wieczorek"));
- aaa.add(new JLabel("Numer indeksu: U-11935"));
- aaa.add(new JLabel("Kontakt: adjimmy10wie@gmail.com"));
- aaa.add(new JLabel("Licencja: Freeware"));
- button = new JButton("OK");
- button.addActionListener(this);
- dialog.add(button);
- //koniec JDialog
- setLayout(null);
- jbDodaj = new JButton("Dodaj");
- jbDodaj.addActionListener(this);
- add(jbDodaj);
- jbDodaj.setBounds(430, 100, 100, 30);
- jbWyzeruj = new JButton("Wyzeruj");
- jbWyzeruj.addActionListener(this);
- add(jbWyzeruj);
- jbWyzeruj.setBounds(430, 140, 100, 30);
- jbWypelnij = new JButton("Wypelnij");
- jbWypelnij.addActionListener(this);
- add(jbWypelnij);
- jbWypelnij.setBounds(430, 180, 100, 30);
- jbZapisz = new JButton("Zapisz");
- add(jbZapisz);
- jbZapisz.addActionListener(this);
- jbZapisz.setBounds(430, 220, 100, 30);
- lWprowadzLiczbe = new JLabel("Wprowadz liczbe: ");
- add(lWprowadzLiczbe);
- lWprowadzLiczbe.setBounds(170, 10, 300, 30);
- lNumerWiersza = new JLabel("Numer wiersza: ");
- add(lNumerWiersza);
- lNumerWiersza.setBounds(320, 10, 300, 30);
- lNumerKolumny = new JLabel("Numer kolumny: ");
- add(lNumerKolumny);
- lNumerKolumny.setBounds(320, 30, 300, 30);
- fWprowadz = new JTextField();
- add(fWprowadz);
- fWprowadz.setBounds(170, 35, 100, 25);
- sNrWiersza = new JSlider(JSlider.VERTICAL,0,4,0);
- sNrWiersza.setBounds(100, 125, 50, 85);
- sNrWiersza.setMajorTickSpacing(1);
- sNrWiersza.setMinorTickSpacing(1);
- sNrWiersza.setPaintTicks(true);
- sNrWiersza.setPaintLabels(true);
- sNrWiersza.addChangeListener(this);
- add(sNrWiersza);
- sNrKolumny = new JSlider(JSlider.HORIZONTAL,0,4,0);
- sNrKolumny.setBounds(150, 80, 250, 50);
- sNrKolumny.setMajorTickSpacing(1);
- sNrKolumny.setMinorTickSpacing(1);
- sNrKolumny.setPaintLabels(true);
- sNrKolumny.setPaintTicks(true);
- sNrKolumny.addChangeListener(this);
- add(sNrKolumny);
- area = new JTextArea();
- area.setBounds(20, 280, 520, 150);
- add(area);
- lObliczenia = new JLabel("Obliczenia: ");
- add(lObliczenia);
- lObliczenia.setBounds(40, 230, 300, 30);
- combo = new JComboBox();
- combo.setBounds(120, 230, 150, 30);
- combo.addItem("Wybierz operacje");
- combo.addItem("Suma elementow");
- combo.addItem("Srednia elementow");
- combo.addItem("Wartosc MIN");
- combo.addItem("Wartosc MAX");
- add(combo);
- combo.addActionListener(this);
- table.setBounds(150, 130, 250, 80);
- for (int i =0; i <table.getColumnCount();i++)
- {
- table.getColumnModel().getColumn(i).setMinWidth(50);
- table.getColumnModel().getColumn(i).setMaxWidth(50);
- }
- table.setVisible(true);
- table.setPreferredScrollableViewportSize(new Dimension(300,50));
- table.setFillsViewportHeight(true);
- add(table);
- // JScrollPane scrollPane = new JScrollPane(table);
- // add(scrollPane);
- // this.add(table);
- toolbar = new JToolBar();
- toolbar.setRollover(true);
- add(toolbar, BorderLayout.NORTH);
- toolbar.addSeparator();
- jUtworz = new JButton(new ImageIcon("utworz.jpg"));
- toolbar.add(jUtworz);
- jUtworz.addActionListener(this);
- jWypelnij = new JButton(new ImageIcon("wypelnij.jpg"));
- toolbar.setRollover(true);
- toolbar.add(jWypelnij);
- jWypelnij.addActionListener(this);
- jWyzeruj = new JButton(new ImageIcon("usun.jpg"));
- toolbar.setRollover(true);
- toolbar.add(jWyzeruj);
- jWyzeruj.addActionListener(this);
- toolbar.addSeparator();
- jSuma = new JButton(new ImageIcon("suma.jpg"));
- toolbar.setRollover(true);
- jSuma.addActionListener(this);
- toolbar.add(jSuma);
- jSrednia = new JButton("Srednia");
- toolbar.setRollover(true);
- jSrednia.addActionListener(this);
- toolbar.add(jSrednia);
- jMin = new JButton("Min");
- toolbar.setRollover(true);
- jMin.addActionListener(this);
- toolbar.add(jMin);
- jMax = new JButton("Max");
- toolbar.setRollover(true);
- jMax.addActionListener(this);
- toolbar.add(jMax);
- toolbar.addSeparator();
- jPomoc = new JButton(new ImageIcon("pomoc.jpg"));
- jPomoc.addActionListener(this);
- toolbar.setRollover(true);
- toolbar.add(jPomoc);
- jInfo = new JButton(new ImageIcon("info.jpg"));
- jInfo.addActionListener(this);
- toolbar.setRollover(true);
- toolbar.add(jInfo);
- JDateChooser dateChooser = new JDateChooser();
- dateChooser.setBounds(40, 40, 90, 20);
- getContentPane().add(dateChooser);
- //menuBar.add(toolbar);
- DefaultTipModel Lista=new DefaultTipModel();
- Lista.add(new DefaultTip("nazwaTipu1","Ciekawostka nr 1""));
- Lista.add(new DefaultTip("nazwaTipu2","Ciekawostka nr 1""));
- }
- private void koniec_programu() {
- int value = JOptionPane.showOptionDialog(this,
- "Czy napewno chcesz zamknac ?",
- "Quit",
- JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE,
- null,
- optionJOptionPane,
- optionJOptionPane[0]);
- if(value == JOptionPane.YES_OPTION) {
- dispose();
- System.exit(0);
- }
- }
- private void uzupelnijTabele()
- {
- int w=0;
- switch(sNrWiersza.getValue())
- {
- case 0: w=4; break;
- case 1: w=3; break;
- case 2: w=2; break;
- case 3: w=1; break;
- case 4: w=0; break;
- default: w=0; break;
- }
- table.getModel().setValueAt(Integer.valueOf(fWprowadz.getText()), w, sNrKolumny.getValue());
- }
- private void zerowanie()
- {
- for(int i=0; i<table.getColumnCount(); i++)
- {
- for(int j = 0; j < table.getRowCount(); j++)
- {
- table.getModel().setValueAt(0, j, i);
- }
- }
- }
- private void wypelnianie()
- {
- Random gen = new Random();
- for(int i=0; i<table.getColumnCount(); i++)
- {
- for(int j = 0; j < table.getRowCount(); j++)
- {
- table.getModel().setValueAt(gen.nextInt(100), j, i);
- }
- }
- }
- public int wyborSUMA()
- {
- int suma = 0;
- for(int i=0; i<table.getColumnCount(); i++)
- {
- for(int j=0; j< table.getRowCount();j++)
- {
- if ( table.getModel().getValueAt(j,i) instanceof String)
- {
- tab[j][i]=Integer.valueOf((String) table.getModel().getValueAt(j, i));
- }
- else
- {
- tab[j][i] = (int)table.getModel().getValueAt(j, i);
- }
- suma+=tab[j][i];
- area.setText("Suma elementow = " +suma );
- }
- }
- return suma;
- }
- private double wyborSrednia()
- {
- double suma=wyborSUMA();
- double srednia = suma/(table.getColumnCount()*table.getRowCount());
- area.setText("Srednia elementow = " +srednia );
- return suma;
- }
- public int wyborMin()
- {
- int min=Integer.MAX_VALUE;
- for(int i=0;i<table.getColumnCount();i++)
- {
- for(int j=0;j<table.getRowCount();j++)
- {
- if(table.getModel().getValueAt(j, i) instanceof String)
- {
- tab[j][i]=Integer.valueOf((String) table.getModel().getValueAt(j, i));
- }
- else
- {
- tab[j][i]=(int)table.getModel().getValueAt(j, i);
- }
- if(tab[j][i]<min)
- {
- min=tab[j][i];
- area.setText("Wartosc min = " +min);
- }
- }
- }
- return min;
- }
- public int wyborMax()
- {
- int max=Integer.MIN_VALUE;
- for(int i=0;i<table.getColumnCount();i++)
- {
- for(int j=0;j<table.getRowCount();j++)
- {
- if(table.getModel().getValueAt(j, i) instanceof String)
- {
- tab[j][i]=Integer.valueOf((String) table.getModel().getValueAt(j, i));
- }
- else
- {
- tab[j][i]=(int)table.getModel().getValueAt(j, i);
- }
- if(tab[j][i]>=max)
- {
- max=tab[j][i];
- area.setText("Wartosc max = "+max);
- }
- }
- }
- return max;
- }
- public void zapisz()
- {
- JFileChooser fc = new JFileChooser();
- if (fc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
- {
- File plik = fc.getSelectedFile();
- try
- {
- PrintWriter pw = new PrintWriter(plik);
- Scanner skaner = new Scanner(area.getText());
- while (skaner.hasNext())
- {
- pw.println(skaner.nextLine() + "\n");
- pw.close();
- }
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void actionPerformed(ActionEvent e)
- {
- Object z = e.getSource();
- if (z == mOAutorze || z == jInfo)
- {
- dialog.setVisible(true);
- }
- if (z == button)
- {
- dialog.setVisible(false);
- }
- if (z == jbDodaj || z == jUtworz)
- {
- uzupelnijTabele();
- }
- if( z== jbWyzeruj || z == jWyzeruj)
- {
- zerowanie();
- }
- if( z== jbWypelnij || z == jWypelnij)
- {
- wypelnianie();
- }
- if (z == jbZapisz || z == mZapisz)
- {
- zapisz();
- }
- if ( z == combo || z == jSuma || z == jSrednia || z == jMin || z == jMax)
- {
- String comboo = combo.getSelectedItem().toString();
- if ( comboo.equals("Suma elementow") || z == jSuma)
- {
- wyborSUMA();
- }
- else if (comboo.equals("Srednia elementow") || z == jSrednia)
- {
- wyborSrednia();
- }
- else if (comboo.equals("Wartosc MIN") || z == jMin)
- {
- wyborMin();
- }
- else if ( comboo.equals("Wartosc MAX") || z == jMax)
- {
- wyborMax();
- }
- }
- if( z == mWyjscie )
- {
- int odp = JOptionPane.showConfirmDialog(null, "Czy na pewno wyjsc ?", "Pytanie", JOptionPane.YES_NO_OPTION);
- if (odp == JOptionPane.YES_OPTION)
- {
- dispose();
- }
- else if (odp == JOptionPane.NO_OPTION) {}
- }
- }
- @Override
- public void stateChanged(ChangeEvent e)
- {
- tempNrWiersza = sNrWiersza.getValue();
- lNumerWiersza.setText("Numer wiersza " +tempNrWiersza);
- tempNrKolumny = sNrKolumny.getValue();
- lNumerKolumny.setText("Numer kolumny " +tempNrKolumny);
- }
- public static void main(String[] args)
- {
- Licznik okno = new Licznik();
- okno.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement