Advertisement
Guest User

Untitled

a guest
May 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.05 KB | None | 0 0
  1. package a;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.FileNotFoundException;
  8. import java.io.PrintWriter;
  9. import java.util.Random;
  10.  
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JComboBox;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JMenu;
  17. import javax.swing.JMenuBar;
  18. import javax.swing.JMenuItem;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JSpinner;
  23. import javax.swing.JTable;
  24. import javax.swing.JTextArea;
  25. import javax.swing.JTextField;
  26. import javax.swing.JToolBar;
  27. import javax.swing.KeyStroke;
  28. import javax.swing.SpinnerNumberModel;
  29. import javax.swing.event.ChangeEvent;
  30. import javax.swing.event.ChangeListener;
  31.  
  32. import org.freixas.jcalendar.JCalendar;
  33. import org.freixas.jcalendar.JCalendarCombo;
  34.  
  35. public class Kalkulator extends JFrame implements ActionListener{
  36.  
  37.     JMenuBar menuBar;
  38.     JMenu menuPlik, menuNarzędzia, menuPomoc, menuObliczenia;
  39.     JMenuItem  mNowy, mOtworz, mZapisz, mWyjscie, mNarz1, mNarz2, mNarz3, mNarz4,  nOProgramie,mObl1,mObl2,mObl3,mObl4,mObl5,mObl6,mObl7;
  40.     JTable tabela;
  41.     JComboBox comboWombo;
  42.     JToolBar tb;
  43.     JButton dodajdotablicy, zerowanie, zapiszdopliku, oblicz;
  44.     JSpinner kolumna, wiersz;
  45.     JTextField wprowadzonaliczba;
  46.     JTextArea wynikiwszystkie;
  47.     JButton tbZapisz, tbWyjdz,tbZerowanie,tbLosowe,tbDodanie,tbMin,tbMax,tbSrednia,tbSuma,tbPomoc,tbInfo;
  48.     Integer wierszyk, kolumienka;
  49.     Integer sum=0,max,min,obliczeniowaZmienna;
  50.     Double srednia ;
  51.     JPanel pasekStatusu;
  52.     JLabel lStatusu;
  53.     JCalendarCombo cKalendarz;
  54.    
  55.    
  56.     public Kalkulator()
  57.     {
  58.         setSize(930,331);
  59.         setTitle("Kalkulator v1.001");
  60.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61.         setLayout(null);
  62.        
  63.         zerowanie = new JButton("Zerowanie tablicy");
  64.         zerowanie.setBounds(0, 80, 150, 20);
  65.         add(zerowanie);
  66.         zerowanie.addActionListener(this);
  67.        
  68.         dodajdotablicy = new JButton("Dodaj do tablicy");
  69.         dodajdotablicy.setBounds(300, 80, 150, 20);
  70.         add(dodajdotablicy);
  71.         dodajdotablicy.addActionListener(this);
  72.        
  73.         zapiszdopliku = new JButton("Zapisz do pliku");
  74.         zapiszdopliku.setBounds(150, 80, 150, 20);
  75.         add(zapiszdopliku);
  76.         zapiszdopliku.addActionListener(this);
  77.        
  78.         menuBar = new JMenuBar();
  79.         menuPlik = new JMenu("Plik");
  80.         mZapisz = new JMenuItem("Zapisz");
  81.         mOtworz = new JMenuItem("Otwórz");
  82.         mWyjscie = new JMenuItem("Wyjście");
  83.         mNowy = new JMenuItem("Nowy");
  84.        
  85.         menuPlik.add(mNowy);
  86.         mNowy.addActionListener(this);
  87.         menuPlik.add(mOtworz);
  88.         menuPlik.add(mZapisz);
  89.         mZapisz.addActionListener(this);
  90.         menuPlik.addSeparator();
  91.        
  92.         menuPlik.add(mWyjscie);
  93.        
  94.         mWyjscie.addActionListener(this);
  95.         mWyjscie.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));
  96.        
  97.         menuNarzędzia = new JMenu("Edycja");
  98.        
  99.         mNarz1 = new JMenuItem("Wytnij");
  100.         mNarz2 = new JMenuItem("Kopiuj");
  101.         mNarz3 = new JMenuItem("Wklej");
  102.         mNarz4 = new JMenuItem("Skasuj");
  103.        
  104.         menuNarzędzia.add(mNarz1);
  105.         menuNarzędzia.add(mNarz2);
  106.         menuNarzędzia.add(mNarz3);
  107.         menuNarzędzia.add(mNarz4);
  108.        
  109.         menuObliczenia = new JMenu("Obliczenia");
  110.        
  111.         mObl1 = new JMenuItem("Zerowanie");
  112.         mObl2 = new JMenuItem("Losowanie");
  113.         mObl3 = new JMenuItem("Dodaj do tabeli");
  114.         mObl4 = new JMenuItem("Min");
  115.         mObl5 = new JMenuItem("Max");
  116.         mObl6 = new JMenuItem("Średnia");
  117.         mObl7 = new JMenuItem("Suma");
  118.        
  119.         menuObliczenia.add(mObl1);
  120.         mObl1.addActionListener(this);
  121.         menuObliczenia.add(mObl2);
  122.         mObl2.addActionListener(this);
  123.         menuObliczenia.add(mObl3);
  124.         mObl3.addActionListener(this);
  125.         menuObliczenia.addSeparator();
  126.         menuObliczenia.add(mObl4);
  127.         mObl4.addActionListener(this);
  128.         menuObliczenia.add(mObl5);
  129.         mObl5.addActionListener(this);
  130.         menuObliczenia.add(mObl6);
  131.         mObl6.addActionListener(this);
  132.         menuObliczenia.add(mObl7);
  133.         mObl7.addActionListener(this);
  134.        
  135.        
  136.         menuPomoc = new JMenu("Pomoc");
  137.        
  138.         nOProgramie = new JMenuItem("Autor");
  139.         nOProgramie.addActionListener(this);
  140.         menuPomoc.add(nOProgramie);
  141.        
  142.         setJMenuBar(menuBar);
  143.         menuBar.add(menuPlik);
  144.         menuBar.add(menuNarzędzia);
  145.         menuBar.add(menuObliczenia);
  146.         menuBar.add(menuPomoc);
  147.        
  148.         JLabel wprowadzliczbe= new JLabel("Podaj liczbę : ");
  149.         wprowadzliczbe.setBounds(0, 50, 80, 30);
  150.         add(wprowadzliczbe);
  151.        
  152.         JLabel wyniki = new JLabel("Wyniki : ");
  153.         wyniki.setBounds(0, 181, 50, 20);
  154.         add(wyniki);
  155.        
  156.         wynikiwszystkie = new JTextArea();
  157.         JScrollPane scrollPane = new JScrollPane(wynikiwszystkie);
  158.         wynikiwszystkie.setWrapStyleWord(true);
  159.         scrollPane.setBounds(45, 181, 200, 40);
  160.         add(scrollPane);
  161.        
  162.         wprowadzonaliczba = new JTextField();
  163.         wprowadzonaliczba.setBounds(80,50 ,100 ,30);
  164.         add(wprowadzonaliczba);
  165.        
  166.         oblicz = new JButton("Oblicz");
  167.         oblicz.setBounds(250, 201, 175, 20);
  168.         add(oblicz);
  169.         oblicz.addActionListener(this);
  170.        
  171.         pasekStatusu = new JPanel();
  172.         pasekStatusu.setBounds(0, 250,530, 20);
  173.         pasekStatusu.setBackground(Color.GRAY);
  174.         add(pasekStatusu);
  175.        
  176.         lStatusu = new JLabel("Uruchomiono.");
  177.         lStatusu.setBounds(0, 250, 530, 15);
  178.         pasekStatusu.add(lStatusu);
  179.        
  180.        
  181.     // KALENDARZ
  182.        
  183.         cKalendarz = new JCalendarCombo();
  184.         cKalendarz.setBounds(450, 100, 200, 200);
  185.         cKalendarz.add(cKalendarz);
  186.        
  187.        
  188.        
  189.        
  190.        
  191.   // SPINNER
  192.        
  193.        
  194.        
  195.         JLabel podajwiersz = new JLabel("Podaj wiersz:");
  196.         podajwiersz.setBounds(190, 50, 80, 30);
  197.         add(podajwiersz);
  198.        
  199.         JLabel podajkolumne = new JLabel("Podaj kolumnę:");
  200.         podajkolumne.setBounds(310, 50, 90, 30);
  201.         add(podajkolumne);
  202.        
  203.        
  204.         kolumna = new JSpinner();
  205.         kolumna.setBounds(401,50,30,30);
  206.         kolumna.setFont(new Font("Arial", Font.PLAIN, 11));
  207.         kolumna.addChangeListener(new ChangeListener() {
  208.             public void stateChanged(ChangeEvent e) {
  209.                 JSpinner source = (JSpinner) e.getSource();
  210.                 kolumienka =(Integer) source.getValue();}
  211.         });
  212.         kolumna.setModel(new SpinnerNumberModel(1, 1, 5, 1));
  213.         add(kolumna);
  214.            
  215.        
  216.         wiersz = new JSpinner();
  217.         wiersz.setBounds(271,50,30,30);
  218.         wiersz.setFont(new Font("Arial", Font.PLAIN, 11));
  219.         wiersz.addChangeListener(new ChangeListener() {
  220.             public void stateChanged(ChangeEvent e) {
  221.                 JSpinner source = (JSpinner) e.getSource();
  222.                 wierszyk =(Integer) source.getValue();}
  223.         });
  224.         wiersz.setModel(new SpinnerNumberModel(1, 1, 5, 1));
  225.         add(wiersz);
  226.        
  227.  
  228.     //TABELA   
  229.  
  230.        
  231.    
  232.         Object[][] data = { {new Integer(0),new Integer(0),new Integer(0),new Integer(0),new Integer(0)},
  233.                             {new Integer(0),new Integer(0),new Integer(0),new Integer(0),new Integer(0)},
  234.                             {new Integer(0),new Integer(0),new Integer(0),new Integer(0),new Integer(0)},
  235.                             {new Integer(0),new Integer(0),new Integer(0),new Integer(0),new Integer(0)},
  236.                             {new Integer(0),new Integer(0),new Integer(0),new Integer(0),new Integer(0)}}; 
  237.        
  238.        
  239.         Object[] columns = {"1","2","3","4","5"};
  240.        
  241.         tabela = new JTable(data, columns);
  242.         tabela.setBounds(0,100, 450, 80);
  243.         add(tabela);
  244.        
  245.        
  246.     //COMBOBOX
  247.        
  248.         comboWombo = new JComboBox();
  249.         comboWombo.setBounds(250, 181, 175, 20);
  250.         comboWombo.addItem("Suma elementów");
  251.         comboWombo.addItem("Średnia elementów");
  252.         comboWombo.addItem("Wartość max");
  253.         comboWombo.addItem("Wartość min");
  254.         add(comboWombo);
  255.        
  256.     //TOOLBAR  - IKONY
  257.        
  258.         tb = new JToolBar();
  259.         tb.setBounds(0, 0, 520, 32);
  260.         tb.setFloatable(false);
  261.        
  262.         tbZapisz = new JButton("");
  263.         tbZapisz.setIcon(new ImageIcon("img/zapisz.png"));
  264.         tb.add(tbZapisz);
  265.         tbZapisz.addActionListener(this);
  266.        
  267.         tbWyjdz = new JButton("");
  268.         tbWyjdz.setIcon(new ImageIcon("img/Exit.png"));
  269.         tb.add(tbWyjdz);
  270.         tbWyjdz.addActionListener(this);
  271.        
  272.         tb.addSeparator();
  273.        
  274.         tbZerowanie = new JButton("");
  275.         tbZerowanie.setIcon(new ImageIcon("img/zerowanie.png"));
  276.         tb.add(tbZerowanie);
  277.         tbZerowanie.addActionListener(this);
  278.        
  279.         tbLosowe = new JButton("");
  280.         tbLosowe.setIcon(new ImageIcon("img/random.png"));
  281.         tb.add(tbLosowe);
  282.         tbLosowe.addActionListener(this);
  283.        
  284.         tbDodanie = new JButton("");
  285.         tbDodanie.setIcon(new ImageIcon("img/dodaj.png"));
  286.         tb.add(tbDodanie);
  287.         tbDodanie.addActionListener(this);
  288.    
  289.         tb.addSeparator();
  290.        
  291.         tbMin = new JButton("");
  292.         tbMin.setIcon(new ImageIcon("img/min.png"));
  293.         tb.add(tbMin);
  294.         tbMin.addActionListener(this);
  295.        
  296.         tbMax = new JButton("");
  297.         tbMax.setIcon(new ImageIcon("img/max.png"));
  298.         tb.add(tbMax);
  299.         tbMax.addActionListener(this);
  300.        
  301.         tbSrednia = new JButton("");
  302.         tbSrednia.setIcon(new ImageIcon("img/srednia.png"));
  303.         tb.add(tbSrednia);
  304.         tbSrednia.addActionListener(this);
  305.        
  306.         tbSuma = new JButton("");
  307.         tbSuma.setIcon(new ImageIcon("img/suma.png"));
  308.         tb.add(tbSuma);
  309.         tbSuma.addActionListener(this);
  310.        
  311.         tb.addSeparator();
  312.        
  313.         tbPomoc = new JButton("");
  314.         tbPomoc.setIcon(new ImageIcon("img/pomoc.png"));
  315.         tb.add(tbPomoc);
  316.         tbPomoc.addActionListener(this);
  317.        
  318.         tbInfo = new JButton("");
  319.         tbInfo.setIcon(new ImageIcon("img/info.png"));
  320.         tb.add(tbInfo);
  321.         tbInfo.addActionListener(this);
  322.        
  323.         add(tb);
  324.        
  325.     }
  326.    
  327.    
  328.    
  329.     public static void main(String[] args)
  330.     {
  331.         Kalkulator okienko = new Kalkulator();
  332.         okienko.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  333.         okienko.setVisible(true);
  334.        
  335.        
  336.        
  337.     }
  338.  
  339.  
  340.  
  341.     public void dodajLiczbe()
  342.     {
  343.         try{
  344.             int liczba = (int) Double.parseDouble(wprowadzonaliczba.getText());
  345.             tabela.setValueAt(liczba, wierszyk-1, kolumienka-1);
  346.             lStatusu.setText("Wprowadzono liczbę do tablicy.");
  347.         }
  348.         catch(Exception e){
  349.             JOptionPane.showMessageDialog(null, "Wprowadzono niepoprawną wartość!");
  350.         }
  351.        
  352.        
  353.        
  354.     }
  355.    
  356.     public void zerowanieTablicy()
  357.     {
  358.         for (int i=0; i < 5; i++)
  359.         {
  360.             for (int j=0; j < 5; j++)
  361.             {
  362.                 tabela.setValueAt(0, i, j);
  363.                 wynikiwszystkie.setText("");
  364.                 wprowadzonaliczba.setText("");
  365.             }}
  366.         lStatusu.setText("Wyzerwoano tablicę oraz pola do wpisywania.");
  367.     }
  368.  
  369.     public void wypelnienieLosowymi()
  370.  
  371.     {
  372.        
  373.        
  374.        
  375.        
  376.         for (int i=0; i < 5; i++)
  377.     {
  378.         for (int j=0; j < 5; j++)
  379.         {
  380.             Random random = new Random();
  381.             int liczbaLosowa = random.nextInt(50);
  382.             tabela.setValueAt(liczbaLosowa, i, j);
  383.         }}
  384.         lStatusu.setText("Wypełniono tablice losowymi znakami.");
  385.        
  386.     }
  387.  
  388.     public void liczenieSumy()
  389.     {
  390.         if (comboWombo.getSelectedIndex() == 0)
  391.         {
  392.             for (int i=0; i < 5; i++)
  393.             {
  394.                 for (int j=0; j < 5; j++)
  395.                 {
  396.                     obliczeniowaZmienna = ((Integer)tabela.getValueAt(i,j));
  397.                     sum += obliczeniowaZmienna;
  398.                 }
  399.             }
  400.             lStatusu.setText("Policzono sumę.");
  401.             wynikiwszystkie.setText("Suma wynosi : " + sum.toString());
  402.             sum = 0;}
  403.        
  404.        
  405.     }
  406.    
  407.     public void liczenieSredniej()
  408.     {
  409.         if (comboWombo.getSelectedIndex() == 1)
  410.         {
  411.            
  412.         for (int i=0; i < 5; i++)
  413.         {
  414.             for (int j=0; j < 5; j++)
  415.             {
  416.                 obliczeniowaZmienna = ((Integer)tabela.getValueAt(i,j));
  417.                 sum += obliczeniowaZmienna;
  418.             }
  419.         }
  420.        
  421.         srednia = (double)sum / 25;
  422.  
  423.         wynikiwszystkie.setText("Średnia wynosi : " + srednia.toString());
  424.         sum = 0;
  425.         lStatusu.setText("Policzono średnią.");
  426.     }}
  427.    
  428.     public void liczenieMax()
  429.     {
  430.         if (comboWombo.getSelectedIndex() == 2)
  431.         {
  432.             max = ((Integer)tabela.getValueAt(0,0));
  433.            
  434.  
  435.             for (int i=0; i < 5; i++)
  436.             {
  437.                 for (int j=0; j < 5; j++)
  438.                 {
  439.                     obliczeniowaZmienna = ((Integer)tabela.getValueAt(i,j));
  440.                     if (obliczeniowaZmienna > max) max = obliczeniowaZmienna;
  441.                    
  442.                 }
  443.             }
  444.  
  445.             wynikiwszystkie.setText("MAX wynosi : "+max.toString());
  446.             lStatusu.setText("Policzono Max.");
  447.         }
  448.            
  449.        
  450.            
  451.         }
  452.  
  453.     public void liczenieMin()
  454.     {
  455.         if (comboWombo.getSelectedIndex() == 3)
  456.         {
  457.            
  458.             min = ((Integer)tabela.getValueAt(0,0));
  459.  
  460.             for (int i=0; i < 5; i++)
  461.             {
  462.                 for (int j=0; j < 5; j++)
  463.                 {
  464.                     obliczeniowaZmienna = ((Integer)tabela.getValueAt(i,j));
  465.                     if (obliczeniowaZmienna < min) min = obliczeniowaZmienna;
  466.                 }
  467.             }
  468.  
  469.             wynikiwszystkie.setText("MIN wynosi : "+min.toString());
  470.             lStatusu.setText("Policzono Min.");
  471.         }
  472.        
  473.        
  474.        
  475.        
  476.     }
  477.    
  478.     public void zapisTab()
  479.     {
  480.         PrintWriter zapis =null;
  481.        
  482.         try{
  483.             zapis = new PrintWriter("zapisz.txt");
  484.         }
  485.         catch (FileNotFoundException e1)
  486.         {
  487.             JOptionPane.showMessageDialog(null, "Brak pliku zapis.txt!", getTitle(), JOptionPane.PLAIN_MESSAGE);
  488.         }
  489.        
  490.         for(int i=0; i<5; i++){
  491.             for(int j=0;j<5;j++)
  492.             {
  493.                 zapis.println(tabela.getValueAt(i, j));
  494.             }
  495.         }
  496.         ImageIcon zapiszIkona = new ImageIcon("img/zapisz.png");
  497.         JOptionPane.showMessageDialog(null, "Zapisano zawartość tabeli do pliku", getTitle(), JOptionPane.PLAIN_MESSAGE, zapiszIkona);
  498.         lStatusu.setText("Zapisano zawartość tablicy do pliku.");
  499.         zapis.close();
  500.     }
  501.    
  502.     public void infoAutor()
  503.     {
  504.         ImageIcon imgInfo = new ImageIcon("img/autor.jpg");
  505.         JTextArea okno1 = new JTextArea(6, 23);
  506.         okno1.setEditable(false);
  507.         okno1.setFont(new Font("Dialog", Font.BOLD, 13));
  508.         String tekst = "Program Kalkulator\n" + "Autor : Mateusz Woronko\n" + "Kontakt : mateusz89108@wp.pl\n" + "Wersja : Beta";
  509.         okno1.setText(tekst);
  510.         okno1.setEditable(false);
  511.        
  512.         JScrollPane scrollpane = new JScrollPane(okno1);
  513.         JOptionPane.showMessageDialog(null, scrollpane, "Informacje o autorze", JOptionPane.PLAIN_MESSAGE, imgInfo);
  514.        
  515.        
  516.        
  517.     }
  518.    
  519.     public void opcjaPomoc()
  520.     {
  521.        
  522.     }
  523.    
  524.     public void actionPerformed(ActionEvent e)
  525.     {
  526.         Object z = e.getSource();
  527.         if(z == mWyjscie || z == tbWyjdz){
  528.            
  529.             lStatusu.setText("Zamykanie aplikacji ... ");
  530.             int n = JOptionPane.showConfirmDialog(null, "Czy na pewno chcasz zamknąć aplikację ?", "Zamykanie aplikacji", JOptionPane.YES_NO_OPTION);
  531.             if(n == JOptionPane.YES_OPTION)
  532.             {
  533.                 dispose();
  534.             }
  535.             if(n == JOptionPane.NO_OPTION)
  536.             {
  537.                 lStatusu.setText("Uruchomiono.");
  538.             }
  539.                
  540.             }
  541.            
  542.            
  543.            
  544.          
  545.         if(z == dodajdotablicy || z == tbDodanie || z == mObl3){
  546.             dodajLiczbe();
  547.         }
  548.         if(z == zerowanie || z == tbZerowanie || z == mObl1 || z == mNowy){
  549.             zerowanieTablicy();
  550.         }
  551.            
  552.         if(z == tbLosowe || z == mObl2)
  553.         {
  554.             wypelnienieLosowymi();
  555.         }
  556.        
  557.         if(z == oblicz || z == tbSuma || z == mObl7)
  558.         {
  559.             liczenieSumy();
  560.         }
  561.         if(z == oblicz || z == tbSrednia || z == mObl6)
  562.         {
  563.             liczenieSredniej();
  564.         }
  565.        
  566.         if(z == oblicz || z == tbMax || z == mObl5)
  567.         {
  568.             liczenieMax();
  569.         }
  570.        
  571.         if(z == oblicz || z == tbMin || z == mObl4)
  572.         {
  573.             liczenieMin();
  574.         }
  575.         if(z == mZapisz || z == zapiszdopliku || z == tbZapisz)
  576.         {
  577.             zapisTab();
  578.         }
  579.        
  580.         if(z == tbInfo || z == nOProgramie)
  581.         {
  582.             lStatusu.setText("Wyświetlono informacje o autorze.");
  583.             infoAutor();
  584.            
  585.         }
  586.        
  587.         ;}
  588.        
  589.        
  590. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement