Advertisement
VIKTOR21

Programiranje 27.11.2015

Nov 27th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. public class Win1 extends JFrame{
  4.  
  5. public Win1(){
  6. super("Nasa prva Win aplikacija"); // naslov prozora(aplikacije)
  7. setSize(300, 100);
  8. }
  9.  
  10. public static void main(String[] args) {
  11.  
  12. Win1 w1;
  13. w1 = new Win1();
  14. //w1.show(); ili ovo ili ovo ispod za prikazivanje
  15. w1.show(true);
  16.  
  17. }
  18.  
  19. }
  20.  
  21.  
  22. import javax.swing.*;
  23. import java.awt.*;
  24. import java.awt.event.*;
  25.  
  26. public class Win2 extends JFrame{
  27.  
  28. JButton btRadi; //pravimo dugme
  29.  
  30. class RadiListener implements ActionListener{
  31. public void actionPerformed(ActionEvent e){
  32. System.out.println("Pritisnuto je dugme");
  33. }
  34. }
  35.  
  36.  
  37.  
  38. public Win2(){
  39. super("Aplikacija sa jednim dugmetom"); // naslov prozora(aplikacije)
  40. setSize(400, 100); // velicina prozora
  41. Container cp = this.getContentPane(); // pravlejnje kontejnera
  42. cp.setLayout(new FlowLayout()); // FlowLayout dodaje sve od gsve jedne do druge i onda ih centrira uz gornju ivicu
  43. btRadi = new JButton("Radi"); // pravimo samo dugme na kojem pise Radi
  44. btRadi.addActionListener(new RadiListener()); // pozivamo da radi RadiListener
  45. cp.add(btRadi); // dodajemo to dugme na layout u prozor
  46. //cp.add(btRadi = new JButton("Radi")); // moze i ovako da se napravi dugme koje odma prosledjujemo u cp
  47. }
  48.  
  49. public static void main(String[] args) {
  50.  
  51. Win2 w2 = new Win2();
  52. w2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ovo pravi da kad se klikne na x da se tad i zatvori aplikacija da prestane da radi
  53. w2.show();
  54.  
  55. }
  56.  
  57. }
  58.  
  59.  
  60.  
  61. import javax.swing.*;
  62. import java.awt.*;
  63. import java.awt.event.*;
  64.  
  65.  
  66.  
  67. public class Win3 extends JFrame implements ActionListener {
  68.  
  69. JButton btRadi1, btRadi2, btRadi3;
  70. public Win3(){
  71. super("Win3");
  72. setSize(400, 200);
  73. Container cp = this.getContentPane();
  74. cp.setLayout(new FlowLayout());
  75. cp.add(btRadi1 = new JButton("Radi1"));
  76. btRadi1.addActionListener(this);
  77. cp.add(btRadi2 = new JButton("Radi2"));
  78. btRadi2.addActionListener(this);
  79. cp.add(btRadi3 = new JButton("Radi3"));
  80. btRadi3.addActionListener(this);
  81.  
  82. }
  83. public void actionPerformed(ActionEvent e){
  84. System.out.println("Pritusnuto je dugme "+e.getActionCommand());
  85. System.out.println("Pritusnuto je dugme "+e.getSource()); // prikazuje nformacije o dugmetu
  86.  
  87. }
  88.  
  89. public static void main(String[] args) {
  90.  
  91. Win3 w3 = new Win3();
  92. w3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93. w3.show();
  94.  
  95.  
  96. }
  97.  
  98. }
  99.  
  100.  
  101.  
  102. import javax.swing.*;
  103.  
  104. import java.awt.*;
  105. import java.awt.event.*;
  106.  
  107.  
  108.  
  109. public class Win4 extends JFrame implements ActionListener{
  110.  
  111. private JLabel lb1, lb2, lbRez;
  112. private JTextField tf1, tf2, tfRez;
  113. private JButton btSaberi, btOduzmi;
  114. public Win4(){
  115. super("Wabirac/Oduzimac");
  116. setSize(250, 180);
  117. Container cp = this.getContentPane();
  118. //cp.setLayout(new FlowLayout());
  119. cp.setLayout(new GridLayout(4, 2));
  120. cp.add(lb1 = new JLabel("Broj 1: "));
  121. cp.add(tf1 = new JTextField(15));
  122. cp.add(lb2 = new JLabel("Broj 2: "));
  123. cp.add(tf2 = new JTextField(15));
  124. cp.add(lbRez = new JLabel("Rezultat : "));
  125. cp.add(tfRez = new JTextField(15));
  126. tfRez.setEditable(false); // zabranjuje upis u to polje
  127. cp.add(btSaberi = new JButton("Saberi"));
  128. btSaberi.addActionListener(this);
  129. cp.add(btOduzmi = new JButton("Oduzmi"));
  130. btOduzmi.addActionListener(this);
  131. }
  132. public void actionPerformed(ActionEvent e){
  133.  
  134. double broj1, broj2, rez;
  135. String str;
  136. try{
  137. str = tf1.getText();
  138. broj1 = Double.parseDouble(str);
  139. str = tf2.getText();
  140. broj2 = Double.parseDouble(str);
  141. /*
  142. if (e.getActionCommand().equals("Saberi"))
  143. rez = broj1 + broj2;
  144. else
  145. rez = broj1 - broj2;
  146. */
  147. if(e.getSource() == btSaberi)
  148. rez = broj1 + broj2;
  149. else
  150. rez = broj1 - broj2;
  151. tfRez.setText(Double.toString(rez));
  152.  
  153. }catch(Exception ee){
  154. tfRez.setText("Nepravilan unos");
  155. }
  156. }
  157.  
  158. public static void main(String[] args) {
  159.  
  160. Win4 w4 = new Win4();
  161. w4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  162. w4.show();
  163. }
  164.  
  165. }
  166.  
  167. //dodati za mnozenje i dijeljenje na ovu aplikaciju, kod dijeljenja ne smije se dijeliti sa 0 pa exception
  168.  
  169.  
  170.  
  171.  
  172. import javax.swing.*;
  173. import java.awt.*;
  174. import java.awt.event.*;
  175.  
  176.  
  177. public class Win5 extends JFrame {
  178.  
  179. JComboBox cb;
  180. String imena[] = {"Miko", "Pero", "Lazo", "Ziko", "Mika"};
  181.  
  182.  
  183. public Win5(){
  184. super("Win 5");
  185. setSize(200, 100);
  186. Container cp = this.getContentPane();
  187. cp.setLayout(new FlowLayout());
  188. cb = new JComboBox();
  189. for(int k = 0; k < imena.length; k++)
  190. cb.addItem(imena[k]);
  191. cb.setSelectedIndex(3); // koji ce bit prvi selektovan
  192. cp.add(cb);
  193. }
  194.  
  195.  
  196. public static void main(String[] args) {
  197.  
  198. Win5 w5 = new Win5();
  199. w5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  200. w5.show();
  201.  
  202.  
  203. }
  204.  
  205. }
  206.  
  207.  
  208.  
  209. public class Ucenik {
  210. private String prezime, ime;
  211. private int razred;
  212. private char odeljenje;
  213. private int ocene[];
  214.  
  215. public Ucenik(String prezime, String ime, int razred,
  216. char odeljenje, int oc[]){
  217. this.prezime = prezime;
  218. this.ime = ime;
  219. this.razred = razred;
  220. this.odeljenje = odeljenje;
  221. ocene = new int[5];
  222. for (int k = 0; k<5; k++){
  223. ocene[k] = oc[k];
  224. }
  225. }
  226.  
  227. public String getPrezime() {
  228. return prezime;
  229. }
  230.  
  231. public void setPrezime(String prezime) {
  232. this.prezime = prezime;
  233. }
  234.  
  235. public String getIme() {
  236. return ime;
  237. }
  238.  
  239. public void setIme(String ime) {
  240. this.ime = ime;
  241. }
  242.  
  243. public int getRazred() {
  244. return razred;
  245. }
  246.  
  247. public void setRazred(int razred) {
  248. this.razred = razred;
  249. }
  250.  
  251. public char getOdeljenje() {
  252. return odeljenje;
  253. }
  254.  
  255. public void setOdeljenje(char odeljenje) {
  256. this.odeljenje = odeljenje;
  257. }
  258.  
  259. public int getOcena(int k){
  260. if ((k < 0) || (k >=5)) return 0;
  261. return ocene[k];
  262. }
  263. public void setOcene(int k, int oc){
  264. if((k >= 0) && (k < 5))
  265. ocene[k] = oc;
  266. }
  267.  
  268. }
  269. import javax.swing.*;
  270. import java.awt.*;
  271. import java.awt.event.*;
  272. import java.util.*;
  273. import java.io.*;
  274.  
  275. public class WinUcenici extends JFrame implements ActionListener{
  276. private JLabel lbPrezime, lbIme, lbRazred, lbOdeljenje, lbOcene[];
  277. private JTextField tfPrezime, tfIme, tfRazred, tfOdeljenje, tfOcene[];
  278. private JLabel lbIzbor;
  279. private JComboBox cbIzbor;
  280. private Vector<Ucenik> vecu;
  281.  
  282. public WinUcenici(){
  283.  
  284. super("Ucenici");
  285. setSize(200,400);
  286. Container cp = this.getContentPane();
  287. cp.setLayout(new GridLayout(10, 2));
  288. cp.add(lbIzbor = new JLabel("Biraj"));
  289. cp.add(cbIzbor = new JComboBox());
  290. cbIzbor.addActionListener(this);
  291. cp.add(lbPrezime = new JLabel("Prezime"));
  292. cp.add(tfPrezime = new JTextField(15));
  293. tfPrezime.setEditable(false);
  294. cp.add(lbIme = new JLabel("Ime"));
  295. cp.add(tfIme = new JTextField(15));
  296. tfIme.setEditable(false);
  297. cp.add(lbRazred = new JLabel("Razred"));
  298. cp.add(tfRazred = new JTextField(15));
  299. tfRazred.setEditable(false);
  300. cp.add(lbOdeljenje = new JLabel("Odeljenje"));
  301. cp.add(tfOdeljenje = new JTextField(15));
  302. tfOdeljenje.setEditable(false);
  303. lbOcene = new JLabel[5];
  304. tfOcene = new JTextField[5];
  305. for (int k = 0 ; k < 5; k++){
  306. cp.add(lbOcene[k] = new JLabel("Ocena "+(k+1)));
  307. cp.add(tfOcene[k] = new JTextField(15));
  308. tfOcene[k].setEditable(false);
  309. }
  310. vecu = new Vector<Ucenik>();
  311. try{
  312. FileReader fr = new FileReader("Ucenici.txt");
  313. Scanner sc = new Scanner(fr);
  314. while (sc.hasNext()){
  315. String pr, ime;
  316. int razr;
  317. char od;
  318. int oc[]= new int[5];
  319. pr = sc.next();
  320. ime = sc.next();
  321. razr = sc.nextInt();
  322. od = sc.next().charAt(0);
  323. for(int k = 0; k < 5; k++)
  324. oc[k]= sc.nextInt();
  325. vecu.add(new Ucenik(pr, ime, razr, od, oc));
  326. cbIzbor.addItem(pr+" "+ime);
  327. }
  328. fr.close();
  329. }catch(Exception ee){
  330.  
  331. }
  332.  
  333. }
  334. public void actionPerformed(ActionEvent e){
  335. int rb;
  336. rb = cbIzbor.getSelectedIndex();
  337. Ucenik u = vecu.elementAt(rb);
  338. tfPrezime.setText(u.getPrezime());
  339. tfIme.setText(u.getIme());
  340. tfRazred.setText(Integer.toString(u.getRazred()));
  341. tfOdeljenje.setText(Character.toString(u.getOdeljenje()));
  342. for(int k = 0; k < 5; k++)
  343. tfOcene[k].setText(Integer.toString(u.getOcena[k]));
  344.  
  345. }
  346.  
  347. public static void main(String[] args) {
  348.  
  349. WinUcenici w = new WinUcenici();
  350. w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  351. w.show();
  352.  
  353. }
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement