Advertisement
Guest User

xxx

a guest
May 24th, 2017
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.76 KB | None | 0 0
  1. package xxx;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.FlowLayout;
  8. import java.awt.Label;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.MouseAdapter;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.io.BufferedWriter;
  16. import java.io.File;
  17. import java.io.FileNotFoundException;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.io.PrintWriter;
  21. import java.util.Random;
  22. import java.util.Scanner;
  23.  
  24.  
  25. import javax.swing.*;
  26. import javax.swing.border.BevelBorder;
  27. import javax.swing.event.ChangeEvent;
  28. import javax.swing.event.ChangeListener;
  29.  
  30. import com.l2fprod.common.swing.JTipOfTheDay;
  31. import com.l2fprod.common.swing.TipModel;
  32. import com.l2fprod.common.swing.tips.DefaultTip;
  33. import com.l2fprod.common.swing.tips.DefaultTipModel;
  34. import com.toedter.calendar.JDateChooser;
  35. import com.toedter.calendar.demo.DateChooserPanel;
  36.  
  37.  
  38. public class Licznik extends JFrame implements ActionListener, ChangeListener
  39. {
  40.  
  41. /**
  42. *
  43. */
  44. JMenuBar menuBar;
  45. JMenu menuPlik, menuNarzedzia, menuObliczenia, menuPomoc;
  46. JMenuItem mOtworz, mZapisz, mWyjscie, mNarz1, mNarz2, mSuma, mSrednia,mMin,mMax, mOProgramie, mOAutorze;
  47. JButton jbDodaj, jbWyzeruj, jbWypelnij, jbZapisz, jbOblicz, button, jUtworz,jWyzeruj, jDrukuj, jUsun, jWypelnij, jSuma, jSrednia, jMin, jMax, jPomoc, jInfo;
  48. JLabel lWprowadzLiczbe, lNumerWiersza, lNumerKolumny, lObliczenia, author,status;
  49. JTextField fWprowadz, fNrWiersza, fNrKolumny;
  50. JSlider sNrWiersza, sNrKolumny;
  51. JTextArea area;
  52. JComboBox combo;
  53. JDialog dialog, Pomoc;
  54. JToolBar toolbar;
  55. JPanel panel;
  56. JTipOfTheDay tip;
  57. private String pole;
  58.  
  59. int tempNrWiersza, tempNrKolumny;
  60. JTable table = new JTable (5,5);
  61. int [][] tab = new int[table.getColumnCount()][table.getRowCount()];
  62.  
  63.  
  64. String[] optionJOptionPane = { "Tak", "Nie" };
  65.  
  66.  
  67. tip=new JTipOfTheDay(tipList);
  68.  
  69.  
  70. Licznik()
  71. {
  72.  
  73. addWindowListener(new WindowAdapter()
  74. {
  75. public void windowClosing(WindowEvent e)
  76. {
  77. koniec_programu();
  78.  
  79. }
  80. });
  81.  
  82. zerowanie();
  83.  
  84. setTitle("Licznik by Adrian Wieczorek");
  85. setDefaultCloseOperation(Licznik.EXIT_ON_CLOSE);
  86. setSize(570, 540);
  87. setLocationRelativeTo(null);
  88.  
  89.  
  90. panel = new JPanel();
  91.  
  92. status = new JLabel("Status: ");
  93. status.setBounds(40, 630, 20, 20);
  94. panel.add(status);
  95.  
  96.  
  97. menuBar = new JMenuBar();
  98. menuPlik = new JMenu("Plik");
  99.  
  100. mZapisz = new JMenuItem("Zapisz");
  101. mZapisz.addActionListener(this);
  102. mWyjscie = new JMenuItem("Wyjscie");
  103.  
  104. menuPlik.add(mZapisz);
  105. menuPlik.addSeparator();
  106. menuPlik.add(mWyjscie);
  107.  
  108. mWyjscie.addActionListener(this);
  109. mWyjscie.setAccelerator(KeyStroke.getKeyStroke("ctrl Z"));
  110.  
  111. menuPomoc = new JMenu("Pomoc");
  112.  
  113. mOAutorze = new JMenuItem("O Autorze");
  114. mOAutorze.addActionListener(this);
  115. menuPomoc.add(mOAutorze);
  116.  
  117. mOProgramie = new JMenuItem("O Programie");
  118. mOProgramie.addActionListener(this);
  119. menuPomoc.add(mOProgramie);
  120.  
  121. setJMenuBar(menuBar);
  122. menuBar.add(menuPlik);
  123. menuBar.add(menuPomoc);
  124.  
  125. //JDialog - info o autorze
  126. dialog = new JDialog();
  127. dialog.setTitle("O autorze");
  128. dialog.setLocationRelativeTo(null);
  129. dialog.setSize(300, 180);
  130. JPanel aaa = new JPanel();
  131. aaa = (JPanel)dialog.getContentPane();
  132. BoxLayout grid = new BoxLayout(aaa,BoxLayout.PAGE_AXIS);
  133. aaa.setLayout(grid);
  134. aaa.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));// góra,lewo,dół,prawo
  135. aaa.add(new JLabel("Program: Projekt cw 1"));
  136. aaa.add(new JLabel("Wersja programu: v1.0"));
  137. aaa.add(new JLabel("Wykonawca: Adrian Wieczorek"));
  138. aaa.add(new JLabel("Numer indeksu: U-11935"));
  139. aaa.add(new JLabel("Kontakt: adjimmy10wie@gmail.com"));
  140. aaa.add(new JLabel("Licencja: Freeware"));
  141.  
  142.  
  143. button = new JButton("OK");
  144. button.addActionListener(this);
  145. dialog.add(button);
  146. //koniec JDialog
  147.  
  148.  
  149.  
  150.  
  151. setLayout(null);
  152.  
  153. jbDodaj = new JButton("Dodaj");
  154. jbDodaj.addActionListener(this);
  155. add(jbDodaj);
  156. jbDodaj.setBounds(430, 100, 100, 30);
  157.  
  158. jbWyzeruj = new JButton("Wyzeruj");
  159. jbWyzeruj.addActionListener(this);
  160. add(jbWyzeruj);
  161. jbWyzeruj.setBounds(430, 140, 100, 30);
  162.  
  163. jbWypelnij = new JButton("Wypelnij");
  164. jbWypelnij.addActionListener(this);
  165. add(jbWypelnij);
  166. jbWypelnij.setBounds(430, 180, 100, 30);
  167.  
  168. jbZapisz = new JButton("Zapisz");
  169. add(jbZapisz);
  170. jbZapisz.addActionListener(this);
  171. jbZapisz.setBounds(430, 220, 100, 30);
  172.  
  173. lWprowadzLiczbe = new JLabel("Wprowadz liczbe: ");
  174. add(lWprowadzLiczbe);
  175. lWprowadzLiczbe.setBounds(170, 10, 300, 30);
  176.  
  177. lNumerWiersza = new JLabel("Numer wiersza: ");
  178. add(lNumerWiersza);
  179. lNumerWiersza.setBounds(320, 10, 300, 30);
  180.  
  181. lNumerKolumny = new JLabel("Numer kolumny: ");
  182. add(lNumerKolumny);
  183. lNumerKolumny.setBounds(320, 30, 300, 30);
  184.  
  185. fWprowadz = new JTextField();
  186. add(fWprowadz);
  187. fWprowadz.setBounds(170, 35, 100, 25);
  188.  
  189.  
  190. sNrWiersza = new JSlider(JSlider.VERTICAL,0,4,0);
  191. sNrWiersza.setBounds(100, 125, 50, 85);
  192. sNrWiersza.setMajorTickSpacing(1);
  193. sNrWiersza.setMinorTickSpacing(1);
  194. sNrWiersza.setPaintTicks(true);
  195. sNrWiersza.setPaintLabels(true);
  196. sNrWiersza.addChangeListener(this);
  197. add(sNrWiersza);
  198.  
  199. sNrKolumny = new JSlider(JSlider.HORIZONTAL,0,4,0);
  200. sNrKolumny.setBounds(150, 80, 250, 50);
  201. sNrKolumny.setMajorTickSpacing(1);
  202. sNrKolumny.setMinorTickSpacing(1);
  203. sNrKolumny.setPaintLabels(true);
  204. sNrKolumny.setPaintTicks(true);
  205.  
  206. sNrKolumny.addChangeListener(this);
  207. add(sNrKolumny);
  208.  
  209. area = new JTextArea();
  210.  
  211. area.setBounds(20, 280, 520, 150);
  212. add(area);
  213.  
  214. lObliczenia = new JLabel("Obliczenia: ");
  215. add(lObliczenia);
  216. lObliczenia.setBounds(40, 230, 300, 30);
  217.  
  218. combo = new JComboBox();
  219. combo.setBounds(120, 230, 150, 30);
  220. combo.addItem("Wybierz operacje");
  221. combo.addItem("Suma elementow");
  222. combo.addItem("Srednia elementow");
  223. combo.addItem("Wartosc MIN");
  224. combo.addItem("Wartosc MAX");
  225. add(combo);
  226. combo.addActionListener(this);
  227.  
  228.  
  229. table.setBounds(150, 130, 250, 80);
  230.  
  231. for (int i =0; i <table.getColumnCount();i++)
  232. {
  233. table.getColumnModel().getColumn(i).setMinWidth(50);
  234. table.getColumnModel().getColumn(i).setMaxWidth(50);
  235. }
  236.  
  237. table.setVisible(true);
  238. table.setPreferredScrollableViewportSize(new Dimension(300,50));
  239. table.setFillsViewportHeight(true);
  240. add(table);
  241.  
  242.  
  243. // JScrollPane scrollPane = new JScrollPane(table);
  244. // add(scrollPane);
  245. // this.add(table);
  246.  
  247. toolbar = new JToolBar();
  248. toolbar.setRollover(true);
  249. add(toolbar, BorderLayout.NORTH);
  250. toolbar.addSeparator();
  251.  
  252. jUtworz = new JButton(new ImageIcon("utworz.jpg"));
  253. toolbar.add(jUtworz);
  254. jUtworz.addActionListener(this);
  255.  
  256.  
  257. jWypelnij = new JButton(new ImageIcon("wypelnij.jpg"));
  258. toolbar.setRollover(true);
  259. toolbar.add(jWypelnij);
  260. jWypelnij.addActionListener(this);
  261.  
  262.  
  263. jWyzeruj = new JButton(new ImageIcon("usun.jpg"));
  264. toolbar.setRollover(true);
  265. toolbar.add(jWyzeruj);
  266. jWyzeruj.addActionListener(this);
  267. toolbar.addSeparator();
  268.  
  269. jSuma = new JButton(new ImageIcon("suma.jpg"));
  270. toolbar.setRollover(true);
  271. jSuma.addActionListener(this);
  272. toolbar.add(jSuma);
  273.  
  274. jSrednia = new JButton("Srednia");
  275. toolbar.setRollover(true);
  276. jSrednia.addActionListener(this);
  277. toolbar.add(jSrednia);
  278.  
  279. jMin = new JButton("Min");
  280. toolbar.setRollover(true);
  281. jMin.addActionListener(this);
  282. toolbar.add(jMin);
  283.  
  284. jMax = new JButton("Max");
  285. toolbar.setRollover(true);
  286. jMax.addActionListener(this);
  287. toolbar.add(jMax);
  288. toolbar.addSeparator();
  289.  
  290. jPomoc = new JButton(new ImageIcon("pomoc.jpg"));
  291. jPomoc.addActionListener(this);
  292. toolbar.setRollover(true);
  293. toolbar.add(jPomoc);
  294.  
  295. jInfo = new JButton(new ImageIcon("info.jpg"));
  296. jInfo.addActionListener(this);
  297. toolbar.setRollover(true);
  298. toolbar.add(jInfo);
  299.  
  300. JDateChooser dateChooser = new JDateChooser();
  301. dateChooser.setBounds(40, 40, 90, 20);
  302. getContentPane().add(dateChooser);
  303.  
  304. //menuBar.add(toolbar);
  305.  
  306. DefaultTipModel Lista=new DefaultTipModel();
  307. Lista.add(new DefaultTip("nazwaTipu1","Ciekawostka nr 1&quot"));
  308. Lista.add(new DefaultTip("nazwaTipu2","Ciekawostka nr 1&quot"));
  309.  
  310.  
  311. }
  312.  
  313.  
  314. private void koniec_programu() {
  315.  
  316. int value = JOptionPane.showOptionDialog(this,
  317. "Czy napewno chcesz zamknac ?",
  318. "Quit",
  319. JOptionPane.YES_NO_OPTION,
  320. JOptionPane.QUESTION_MESSAGE,
  321. null,
  322. optionJOptionPane,
  323. optionJOptionPane[0]);
  324.  
  325. if(value == JOptionPane.YES_OPTION) {
  326. dispose();
  327. System.exit(0);
  328. }
  329.  
  330. }
  331.  
  332. private void uzupelnijTabele()
  333. {
  334. int w=0;
  335. switch(sNrWiersza.getValue())
  336. {
  337. case 0: w=4; break;
  338. case 1: w=3; break;
  339. case 2: w=2; break;
  340. case 3: w=1; break;
  341. case 4: w=0; break;
  342. default: w=0; break;
  343. }
  344.  
  345. table.getModel().setValueAt(Integer.valueOf(fWprowadz.getText()), w, sNrKolumny.getValue());
  346. }
  347.  
  348. private void zerowanie()
  349. {
  350. for(int i=0; i<table.getColumnCount(); i++)
  351. {
  352. for(int j = 0; j < table.getRowCount(); j++)
  353. {
  354. table.getModel().setValueAt(0, j, i);
  355. }
  356. }
  357. }
  358.  
  359. private void wypelnianie()
  360. {
  361. Random gen = new Random();
  362. for(int i=0; i<table.getColumnCount(); i++)
  363. {
  364.  
  365. for(int j = 0; j < table.getRowCount(); j++)
  366. {
  367. table.getModel().setValueAt(gen.nextInt(100), j, i);
  368. }
  369. }
  370. }
  371.  
  372. public int wyborSUMA()
  373. {
  374. int suma = 0;
  375.  
  376. for(int i=0; i<table.getColumnCount(); i++)
  377. {
  378. for(int j=0; j< table.getRowCount();j++)
  379. {
  380. if ( table.getModel().getValueAt(j,i) instanceof String)
  381. {
  382. tab[j][i]=Integer.valueOf((String) table.getModel().getValueAt(j, i));
  383. }
  384. else
  385. {
  386. tab[j][i] = (int)table.getModel().getValueAt(j, i);
  387. }
  388. suma+=tab[j][i];
  389. area.setText("Suma elementow = " +suma );
  390. }
  391. }
  392. return suma;
  393. }
  394.  
  395.  
  396. private double wyborSrednia()
  397. {
  398. double suma=wyborSUMA();
  399.  
  400. double srednia = suma/(table.getColumnCount()*table.getRowCount());
  401. area.setText("Srednia elementow = " +srednia );
  402. return suma;
  403.  
  404. }
  405.  
  406.  
  407. public int wyborMin()
  408. {
  409.  
  410. int min=Integer.MAX_VALUE;
  411. for(int i=0;i<table.getColumnCount();i++)
  412. {
  413. for(int j=0;j<table.getRowCount();j++)
  414. {
  415.  
  416. if(table.getModel().getValueAt(j, i) instanceof String)
  417. {
  418. tab[j][i]=Integer.valueOf((String) table.getModel().getValueAt(j, i));
  419. }
  420. else
  421. {
  422. tab[j][i]=(int)table.getModel().getValueAt(j, i);
  423. }
  424.  
  425. if(tab[j][i]<min)
  426. {
  427. min=tab[j][i];
  428. area.setText("Wartosc min = " +min);
  429. }
  430. }
  431. }
  432. return min;
  433. }
  434.  
  435. public int wyborMax()
  436. {
  437. int max=Integer.MIN_VALUE;
  438. for(int i=0;i<table.getColumnCount();i++)
  439. {
  440. for(int j=0;j<table.getRowCount();j++)
  441. {
  442.  
  443. if(table.getModel().getValueAt(j, i) instanceof String)
  444. {
  445. tab[j][i]=Integer.valueOf((String) table.getModel().getValueAt(j, i));
  446. }
  447. else
  448. {
  449. tab[j][i]=(int)table.getModel().getValueAt(j, i);
  450. }
  451.  
  452. if(tab[j][i]>=max)
  453. {
  454. max=tab[j][i];
  455. area.setText("Wartosc max = "+max);
  456. }
  457. }
  458. }
  459. return max;
  460. }
  461.  
  462.  
  463. public void zapisz()
  464. {
  465. JFileChooser fc = new JFileChooser();
  466. if (fc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
  467. {
  468. File plik = fc.getSelectedFile();
  469. try
  470. {
  471. PrintWriter pw = new PrintWriter(plik);
  472. Scanner skaner = new Scanner(area.getText());
  473.  
  474. while (skaner.hasNext())
  475. {
  476. pw.println(skaner.nextLine() + "\n");
  477.  
  478. pw.close();
  479. }
  480. }
  481. catch (FileNotFoundException e)
  482. {
  483. e.printStackTrace();
  484. }
  485. }
  486. }
  487.  
  488. @Override
  489. public void actionPerformed(ActionEvent e)
  490. {
  491. Object z = e.getSource();
  492.  
  493. if (z == mOAutorze || z == jInfo)
  494. {
  495. dialog.setVisible(true);
  496. }
  497. if (z == button)
  498. {
  499. dialog.setVisible(false);
  500. }
  501.  
  502. if (z == jbDodaj || z == jUtworz)
  503. {
  504. uzupelnijTabele();
  505. }
  506.  
  507. if( z== jbWyzeruj || z == jWyzeruj)
  508. {
  509. zerowanie();
  510. }
  511.  
  512. if( z== jbWypelnij || z == jWypelnij)
  513. {
  514. wypelnianie();
  515. }
  516.  
  517. if (z == jbZapisz || z == mZapisz)
  518. {
  519. zapisz();
  520. }
  521.  
  522.  
  523. if ( z == combo || z == jSuma || z == jSrednia || z == jMin || z == jMax)
  524. {
  525. String comboo = combo.getSelectedItem().toString();
  526. if ( comboo.equals("Suma elementow") || z == jSuma)
  527. {
  528. wyborSUMA();
  529.  
  530. }
  531. else if (comboo.equals("Srednia elementow") || z == jSrednia)
  532. {
  533. wyborSrednia();
  534.  
  535. }
  536. else if (comboo.equals("Wartosc MIN") || z == jMin)
  537. {
  538. wyborMin();
  539.  
  540. }
  541. else if ( comboo.equals("Wartosc MAX") || z == jMax)
  542. {
  543. wyborMax();
  544.  
  545. }
  546. }
  547.  
  548.  
  549. if( z == mWyjscie )
  550. {
  551. int odp = JOptionPane.showConfirmDialog(null, "Czy na pewno wyjsc ?", "Pytanie", JOptionPane.YES_NO_OPTION);
  552. if (odp == JOptionPane.YES_OPTION)
  553. {
  554. dispose();
  555. }
  556. else if (odp == JOptionPane.NO_OPTION) {}
  557. }
  558.  
  559.  
  560.  
  561. }
  562.  
  563.  
  564. @Override
  565. public void stateChanged(ChangeEvent e)
  566. {
  567. tempNrWiersza = sNrWiersza.getValue();
  568. lNumerWiersza.setText("Numer wiersza " +tempNrWiersza);
  569.  
  570.  
  571. tempNrKolumny = sNrKolumny.getValue();
  572. lNumerKolumny.setText("Numer kolumny " +tempNrKolumny);
  573.  
  574. }
  575.  
  576. public static void main(String[] args)
  577. {
  578. Licznik okno = new Licznik();
  579. okno.setVisible(true);
  580.  
  581.  
  582. }
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement