Advertisement
barryd

Introduction to Java class and objects/function/final

Jun 10th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.21 KB | None | 0 0
  1. These are general Java programming tutorials provided for free on YouTube:
  2. http://thenewboston.org/list.php?cat=31
  3.  
  4. Java Syntax
  5.  
  6. I would personally watch as many as I could until I feel comfortable with Java. Once you get comfortable with Java, you can start Android programming:
  7. http://thenewboston.org/list.php?cat=6
  8.  
  9. I am visual so I like learning by watching videos. If you would like to read instead, there are many other websites. For example: http://docs.oracle.com/javase/tutorial/
  10.  
  11.  
  12. filenames must end with .java
  13.  
  14. javac classname.java
  15.  
  16. java classname
  17.  
  18. error: Class names, 'variables.txt', are only accepted if annotation processing is explici
  19. tly requested
  20. 1 error
  21.  
  22. C:\JAVA>
  23.  
  24. When they compile, the output file is .class and the filename corresponds with the class name in the file.
  25.  
  26.  
  27. **Make sure your class is named properaly (class java ...)
  28.  
  29.  
  30. + concatenation operator
  31.  
  32. public class
  33.  
  34. public method
  35.  
  36.  
  37. dot seperator
  38.  
  39. class.method(aurgument );
  40.  
  41. public class tuna {
  42. public void simpleMessage() {
  43. System.out.println("This is another class");
  44. }
  45. }
  46.  
  47. public class apples {
  48. public static void main(String[] args) {
  49. tuna tunaObject = new tuna();
  50. tunaObject.simpleMessage();
  51. }
  52. }
  53.  
  54.  
  55. System.out.printf("Your first gf was %s", getName());
  56. System.out.println //prints endline breaks
  57. System.out.print(tuna); //continues from the previous
  58.  
  59. public class tuna {
  60. public void simpleMessage(String name) {
  61. System.out.println("Hello " + name);
  62. }
  63. }
  64.  
  65.  
  66. if/else statments
  67.  
  68. if (argument)
  69. dothis...;
  70. else if (argument)
  71. dothat...;
  72. ...
  73. else
  74. doother...;
  75.  
  76. Nesting
  77. if () {
  78. if () {
  79. ...
  80. }
  81. }
  82.  
  83.  
  84. Average
  85.  
  86. import java.util.Scanner;
  87.  
  88. class apples {
  89.  
  90. public static void main(String[] args) {
  91.  
  92. Scanner input = new Scanner(System.in);
  93.  
  94. int total = 0;
  95. int grade;
  96. int average;
  97. int counter = 0;
  98.  
  99. while (counter < 10) {
  100.  
  101. grade = input.nextInt();
  102. total = total + grade;
  103. counter++;
  104.  
  105. }
  106.  
  107. average = total/10;
  108. System.out.println("Your average is " + average);
  109.  
  110. }
  111.  
  112. }
  113.  
  114.  
  115. Arrays
  116.  
  117. int bucky[] = new int[10];
  118.  
  119. int bucky[] = {2,4,5,7,9}; array initializer (subscript/indexer)
  120.  
  121. bucky[0]=87;
  122. bucky[1]=543;
  123. bucky[9]=65;
  124.  
  125. System.out.println(bucky[1]);
  126.  
  127.  
  128. Index
  129.  
  130. System.out.println("Index\tValue");
  131.  
  132. int bucky[] = {32, 48, 18, 54, 2};
  133.  
  134. for(int counter=0; counter < bucky.length; counter++) {
  135.  
  136. System.out.println(counter + "\t" + bucky[counter]);
  137.  
  138. }
  139.  
  140.  
  141. Total Sum
  142.  
  143. int bucky[] = {21, 16, 86, 21, 3};
  144. int sum=0;
  145.  
  146. for (int counter=0; counter<bucky.length; counter++) {
  147.  
  148. sum+=bucky[counter];
  149.  
  150. }
  151.  
  152. System.out.println("The sum of these numbers is " + sum);
  153.  
  154.  
  155.  
  156. Conditional Operators
  157.  
  158.  
  159. System.out.println(age > 50 ? "You are old" : "You are Young");
  160.  
  161.  
  162. import java.util.Scanner;
  163.  
  164. public class apples {
  165. public static void main(String[] args) {
  166.  
  167. Scanner input = new Scanner(System.in);
  168. tuna tunaObject = new tuna();
  169.  
  170. System.out.println("Enter your name here: ");
  171. String name = input.nextLine();
  172.  
  173. tunaObject.simpleMessage(name);
  174.  
  175. }
  176. }
  177.  
  178.  
  179.  
  180. toString
  181.  
  182. class apples {
  183. public static void main(String[] args) {
  184. potpie potObject = new potpie(4,5,6);
  185. }
  186. }
  187.  
  188. public class potpie {
  189. private int month;
  190. private int day;
  191. private int year;
  192.  
  193. public potpie(int m, int d, int y) {
  194. month = m;
  195. day = d;
  196. year = y;
  197. System.out.printf("The constructor for this is %s\n", this);
  198. }
  199.  
  200. public String toString() {
  201. return String.format("%d/%d/%d", month, day, year);
  202. }
  203.  
  204. }
  205.  
  206.  
  207. Composition
  208.  
  209.  
  210. class apples {
  211.  
  212. public static void main(String[] args) {
  213.  
  214. potpie potObject = new potpie(4,5,6);
  215. tuna tunaObject = new tuna("Greg", potObject);
  216.  
  217. System.out.println(tunaObject);
  218.  
  219. }
  220.  
  221. }
  222.  
  223.  
  224. public class tuna {
  225.  
  226. private String name;
  227. private potpie birthday;
  228.  
  229. public tuna(String theName, potpie theDate) {
  230.  
  231. name = theName;
  232. birthday = theDate;
  233.  
  234. }
  235.  
  236. public String toString() {
  237.  
  238. return String.format("My name is %s, my birthday is %s", name, birthday);
  239.  
  240. }
  241.  
  242. }
  243.  
  244. public class potpie {
  245.  
  246. private int month;
  247. private int day;
  248. private int year;
  249.  
  250. public potpie(int m, int d, int y) {
  251.  
  252. month = m;
  253. day = d;
  254. year = y;
  255.  
  256. System.out.printf("The constructor for this is %s\n", this);
  257.  
  258. }
  259.  
  260. public String toString() {
  261.  
  262. return String.format("%d/%d/%d", month, day, year);
  263.  
  264. }
  265.  
  266. }
  267.  
  268.  
  269. Type-safe enumerations (enum types)
  270.  
  271. JDK 1.5 is not available, type-safe enumerations can still be implemented as a regular Java class
  272. Enumeration
  273.  
  274. class apples {
  275.  
  276. public static void main(String[] args) {
  277.  
  278. for (tuna people: tuna.values())
  279.  
  280. System.out.printf("%s\t%s\t%s\n", people, people.getDesc(), people.getYear());
  281.  
  282. }
  283.  
  284. }
  285.  
  286. public enum tuna {
  287.  
  288. bucky("nice", "22"),
  289. kelsey("cutie", "10"),
  290. julia("bigmistake", "12");
  291.  
  292. private final String desc;
  293. private final String year;
  294.  
  295. tuna(String description, String birthday) {
  296. desc = description;
  297. year = birthday;
  298.  
  299. }
  300.  
  301. public String getDesc() {
  302. return desc;
  303. }
  304.  
  305. public String getYear() {
  306. return year;
  307. }
  308.  
  309. }
  310.  
  311.  
  312.  
  313. EnumSet Range
  314.  
  315. import java.util.EnumSet;
  316.  
  317. class apples {
  318.  
  319. public static void main(String[] args) {
  320. for (tuna people: tuna.values())
  321. System.out.printf("%s\t%s\t%s\n", people, people.getDesc(), people.getYear());
  322.  
  323. System.out.println("\nAnd for the range of constants!!!\n");
  324.  
  325. for (tuna people: EnumSet.range(tuna.kelsey, tuna.candy))
  326. System.out.printf("%s\t%s\t%s\n", people, people.getDesc(), people.getYear());
  327. }
  328.  
  329. }
  330.  
  331.  
  332. public enum tuna {
  333.  
  334. bucky("nice", "22"),
  335. kelsey("cutie", "10"),
  336. julia("bigmistake", "12"),
  337. nicole("italian", "13"),
  338. candy("different", "14"),
  339. erin("I wish", "16");
  340.  
  341. private final String desc;
  342. private final String year;
  343.  
  344. tuna(String description, String birthday) {
  345. desc = description;
  346. year = birthday;
  347.  
  348. }
  349.  
  350. public String getDesc() {
  351. return desc;
  352. }
  353.  
  354. public String getYear() {
  355. return year;
  356. }
  357. }
  358.  
  359.  
  360. Static
  361.  
  362. Are defined the same the whole time the program is run, and can be accessed globally, using the class name reference.
  363.  
  364. class apples {
  365. public static void main(String[] args) {
  366. tuna member1 = new tuna("Meagan", "Fox");
  367. tuna member2 = new tuna("Natalie", "Portman");
  368. tuna member3 = new tuna("Taylor", "Swift");
  369.  
  370. System.out.println();
  371. System.out.println(member2.getFirst());
  372. System.out.println(member2.getLast());
  373. System.out.println(member2.getMembers());
  374. System.out.println();
  375.  
  376. System.out.println(tuna.getMembers());
  377. }
  378.  
  379. }
  380.  
  381. public class tuna {
  382.  
  383. private String first;
  384. private String last;
  385. private static int members = 0;
  386.  
  387. public tuna(String fn, String ln) {
  388.  
  389. first = fn;
  390. last = ln;
  391. members++;
  392. System.out.printf("Constructor for %s %s, members in the club: %d\n", first, last, members);
  393.  
  394. }
  395.  
  396.  
  397. public String getFirst () {
  398. return first;
  399. }
  400.  
  401. public String getLast () {
  402. return last;
  403. }
  404.  
  405. public static int getMembers() {
  406. return members;
  407. }
  408.  
  409. }
  410.  
  411.  
  412. Final
  413.  
  414. class apples {
  415. public static void main(String[] args) {
  416. tuna tunaObject = new tuna(10);
  417.  
  418. for(int i = 0; i < 5; i++) {
  419. tunaObject.add();
  420. System.out.printf("%s", tunaObject);
  421. }
  422. }
  423. }
  424.  
  425. public class tuna {
  426.  
  427. private int sum;
  428. private final int NUMBER; //(final) initializer ( = 2; )
  429.  
  430. public tuna(int x) {
  431. NUMBER = x;
  432. }
  433.  
  434. public void add() {
  435. sum+=NUMBER;
  436. }
  437.  
  438. public String toString() {
  439. return String.format("sum = %d\n", sum);
  440. }
  441.  
  442. }
  443.  
  444.  
  445.  
  446. Building Objects for Constructors
  447.  
  448. Variable length arguments
  449.  
  450. class apples {
  451. public static void main(String[] args) {
  452. //tuna tunaObject = new tuna();
  453. //System.out.println(tunaObject.toMilitary());
  454. //System.out.println(tunaObject.toString());
  455.  
  456. //tunaObject.setTime(13, 27, 6);
  457. //System.out.println(tunaObject.toMilitary());
  458. //System.out.println(tunaObject.toString());
  459.  
  460.  
  461. tuna tunaObject = new tuna();
  462. tuna tunaObject2 = new tuna(5);
  463. tuna tunaObject3 = new tuna(5,13);
  464. tuna tunaObject4 = new tuna(5,13,43);
  465.  
  466. System.out.printf("%s\n", tunaObject.toMilitary());
  467. System.out.printf("%s\n", tunaObject2.toMilitary());
  468. System.out.printf("%s\n", tunaObject3.toMilitary());
  469. System.out.printf("%s\n", tunaObject4.toMilitary());
  470.  
  471. }
  472.  
  473. }
  474.  
  475.  
  476.  
  477. public class tuna {
  478.  
  479. private int hour;
  480. private int minute;
  481. private int second;
  482.  
  483. public tuna() {
  484. this(0,0,0);
  485. }
  486.  
  487. public tuna(int h) {
  488. this(h,0,0);
  489. }
  490.  
  491. public tuna(int h, int m) {
  492. this(h,m,0);
  493. }
  494.  
  495. public tuna(int h, int m, int s) {
  496. setTime(h,m,s);
  497. }
  498.  
  499. public void setTime(int h, int m, int s) {
  500. setHour(h);
  501. setMinute(m);
  502. setSecond(s);
  503. }
  504.  
  505. public void setHour (int h) {
  506. hour = ((h >=0 && h < 24) ? h : 0);
  507. }
  508.  
  509. public void setMinute (int m) {
  510. minute = ((m >=0 && m < 60) ? m : 0);
  511. }
  512.  
  513. public void setSecond (int s) {
  514. second = ((s >=0 && s < 60) ? s : 0);
  515. }
  516.  
  517. public int getHour () {
  518. return hour;
  519. }
  520.  
  521. public int getMinute () {
  522. return minute;
  523. }
  524.  
  525. public int getSecond () {
  526. return second;
  527. }
  528.  
  529. public String toMilitary() {
  530. return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
  531. }
  532. }
  533.  
  534.  
  535. Inheritance (Extends)
  536.  
  537. Can be used on multiple levels (hierarchical) with inheritance
  538.  
  539. only public methods can be inherited.
  540.  
  541. tuna extends potpie
  542. potpie extends food
  543.  
  544. class apples {
  545. public static void main(String[] args) {
  546. tuna tunaObject = new tuna();
  547. potpie potObject = new potpie();
  548.  
  549. tunaObject.eat();
  550. potObject.eat();
  551. }
  552. }
  553.  
  554. public class tuna extends food {
  555. public void eat() {
  556. System.out.println("I am the new method of tuna");
  557. }
  558. }
  559.  
  560. public class potpie extends food {
  561.  
  562. }
  563.  
  564. public class food {
  565. public void eat() {
  566. System.out.println("I am the eat methods");
  567. }
  568. }
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575. Constructor
  576.  
  577. method must match the same name (method name) as the class name, inorder for it to be considered a constructor
  578.  
  579.  
  580. Math Operators
  581.  
  582. ++pre-increment variable (-- subtraction)
  583.  
  584. increments the variable before it returns
  585.  
  586.  
  587. post-increment++ variable (-- subtraction)
  588.  
  589. increments the variable after it returns
  590.  
  591.  
  592. increment assign operator
  593.  
  594. variable += 5
  595.  
  596.  
  597. Logical Operator (Multiple Test)
  598.  
  599. && AND
  600. || OR
  601.  
  602. var1 && var2 (both have to be true)
  603. var1 || var2 (either must be true, the other maybe false)
  604.  
  605.  
  606. int whole numbers
  607.  
  608. int + int = sum
  609. int * int = product
  610. int / int = divide
  611. int % int = modulus (remainder)
  612.  
  613. int var = 1;
  614.  
  615. int var_name(, var_name2, var_name3...);
  616.  
  617. var_name = 1;
  618.  
  619.  
  620. double
  621.  
  622. fraction/decimal numbers
  623.  
  624.  
  625. String variable = "value";
  626.  
  627.  
  628.  
  629. "...it can only do one or the other."
  630.  
  631. if (condition) {
  632. //condition is true
  633. testing
  634.  
  635. != does not equal
  636. === absolute equal
  637. >= greater than or equal to
  638. <= less than or equal to
  639. > greater than
  640. < less than
  641.  
  642. block
  643.  
  644. } else {
  645. //condition is false
  646. }
  647.  
  648. While Loop
  649.  
  650. while (counter < 10) {
  651. System.out.println(counter);
  652. counter++;
  653. }
  654.  
  655.  
  656. Do-while loop
  657.  
  658. int counter = 0;
  659.  
  660. do {
  661.  
  662. System.out.println(counter);
  663. counter++;
  664.  
  665. } while (counter <= 10);
  666.  
  667.  
  668. Java Methods (declare after being used?)
  669.  
  670. public static void main(String[] args) {
  671. int bucky[]={3,4,5,6,7};
  672. change(bucky);
  673. for(int y:bucky)
  674. System.out.println(y);
  675. }
  676.  
  677. public static void change(int x[]) {
  678. for (int counter = 0; counter < x.length; counter++ ) {
  679. x[counter] += 5;
  680. }
  681. }
  682.  
  683. Java Local Variables
  684.  
  685. public void setTime(int hour, int minute, int second) {
  686. this.hour = 4;
  687. this.minute = 5;
  688. this.second = 6;
  689. }
  690.  
  691.  
  692. Java Global Variables
  693.  
  694. public int hour = 1;
  695. public int minute = 2;
  696. public int second = 3'
  697.  
  698.  
  699. Java Multi-Array
  700.  
  701. int firstarray[][]={{8,9,10,11}, {12, 13, 14, 15}};
  702. int secondarray[][]={{30,31,32,33},{43},{4,5,6}};
  703.  
  704. firstarray[0][1]; //[col][row]
  705.  
  706.  
  707. Java Multi-Array Display
  708.  
  709. public static void main(String[] args) {
  710.  
  711. int firstarray[][]={{8,9,10,11}, {12, 13, 14, 15}};
  712. int secondarray[][]={{30,31,32,33},{43},{4,5,6}};
  713.  
  714. //firstarray[0][1]; //[col][row]
  715.  
  716. System.out.println("This is the first array");
  717. display(firstarray);
  718.  
  719. System.out.println("This is the second array");
  720. display(secondarray);
  721.  
  722. }
  723.  
  724. public static void display(int x[][]) {
  725.  
  726. for (int row=0; row < x.length; row++) {
  727. for (int column=0; column < x[row].length; column++ ) {
  728. System.out.print(x[row][column]+"\t");
  729. }
  730. System.out.println();
  731. }
  732.  
  733. }
  734.  
  735.  
  736. Variable Length Arguments
  737.  
  738. public static void main(String[] args) {
  739.  
  740. System.out.println(average(43,6,26,23));
  741.  
  742. }
  743.  
  744. public static int average(int...numbers) {
  745.  
  746. int total = 0;
  747.  
  748. for (int x:numbers)
  749. total+=x;
  750.  
  751. return total/numbers.length;
  752.  
  753. }
  754.  
  755.  
  756.  
  757. For loop
  758.  
  759. for(int counter = 1; counter <= 10; counter++) //counter+=3 step by 3, 6, 9...
  760.  
  761. Enhanced For Loop
  762.  
  763. int bucky[] = {3,4,5,6,7};
  764. int total = 0;
  765.  
  766. for (int x: bucky) { total +=x; }
  767.  
  768. System.out.println(total);
  769.  
  770.  
  771.  
  772. Random Dice
  773.  
  774. import java.util.Random;
  775.  
  776. Random dice = new Random();
  777. int number;
  778.  
  779. for(int counter=1; counter <= 10; counter++) {
  780.  
  781. number = 1+dice.nextInt(6);
  782.  
  783. System.out.println(number + " ");
  784.  
  785. }
  786.  
  787.  
  788. Random Face/Dice Generator
  789.  
  790. Random rand = new Random();
  791.  
  792. int freq[] = new int[7];
  793.  
  794. for (int roll = 1; roll < 1000; roll++ ) {
  795.  
  796. ++freq[1+rand.nextInt(6)];
  797.  
  798. }
  799.  
  800. System.out.println("Face\tFrequency");
  801.  
  802. for (int face=1; face<freq.length; face++) {
  803.  
  804. System.out.println(face+"\t"+freq[face]);
  805.  
  806. }
  807.  
  808.  
  809. Time
  810.  
  811. private int hour;
  812. private int minute;
  813. private int second;
  814.  
  815. public void setTime(int h, int m, int s) {
  816. hour = ((h >= 0 && h < 24) ? h : 0);
  817. minute = ((m >= 0 && m < 60) ? m : 0);
  818. second = ((s >= 0 && s < 60) ? s : 0);
  819. }
  820.  
  821. public String toMilitary() {
  822.  
  823. return String.format("%02d:%02d:%02d", hour, minute, second );
  824.  
  825. }
  826.  
  827. public String toString() {
  828.  
  829. return String.format("%d:%02d:%02d %s", ((hour==0 || hour==12) ? 12 : hour%12), minute, second, (hour < 12 ? "AM" : "PM"));
  830.  
  831. }
  832.  
  833.  
  834. public static void main(String[] args) {
  835. tuna tunaObject = new tuna();
  836. System.out.println(tunaObject.toMilitary());
  837. tunaObject.setTime(13, 27, 6);
  838. System.out.println(tunaObject.toMilitary());
  839. }
  840.  
  841.  
  842. Compound Interest
  843.  
  844. A=P(1+R)^n
  845.  
  846. A = Amount
  847. P = Principle
  848. * (1+R = Rate) ^ (power) n = Years
  849.  
  850. double amount;
  851. double principal = 10000;
  852. double rate = .01;
  853.  
  854. for (int day = 1; day <= 20; day++) {
  855.  
  856. amount = principal*Math.pow(1 + rate, day);
  857. System.out.println(day + " " + amount);
  858.  
  859. }
  860.  
  861. System.out.println(Math.abs());
  862.  
  863. abs = absolute value (-26.7 == 26.7)
  864. ceil = ceiling (7.4 == 8.0)
  865. floor = floor (7.8 == 7.0)
  866. max = maximum (8.6, 5.2 == 8.6)
  867. min = minimum (8.6, 5.2 == 5.2)
  868. pow = power (5, 3 == 125)
  869. sqrt = square root (9 == 3)
  870.  
  871.  
  872. GUI
  873.  
  874. import javax.swing.JOptionPane;
  875.  
  876. class apples {
  877. public static void main(String[] args) {
  878.  
  879. String fn = JOptionPane.showInputDialog("Enter first number");
  880. String sn = JOptionPane.showInputDialog("Enter second number");
  881.  
  882. int num1 = Integer.parseInt(fn);
  883. int num2 = Integer.parseInt(sn);
  884. int sum = num1 + num2;
  885.  
  886. JOptionPane.showMessageDialog(null, "The answer is " +sum, "the title", JOptionPane.PLAIN_MESSAGE);
  887. }
  888. }
  889.  
  890.  
  891. GUI with JFrame
  892.  
  893. import javax.swing.JFrame;
  894.  
  895. class apples {
  896. public static void main(String[] args) {
  897.  
  898. tuna bucky = new tuna();
  899. bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  900. bucky.setSize(275,180);
  901. bucky.setVisible(true);
  902.  
  903. }
  904. }
  905.  
  906. import java.awt.FlowLayout;
  907. import javax.swing.JFrame;
  908. import javax.swing.JLabel;
  909.  
  910. public class tuna extends JFrame {
  911. private JLabel item1;
  912. public tuna() {
  913. super("The Title Bar");
  914. setLayout(new FlowLayout());
  915.  
  916. item1 = new JLabel("This is a sentence");
  917. item1.setToolTipText("This is going to show up on hover.");
  918. add(item1);
  919. }
  920. }
  921.  
  922.  
  923.  
  924. Event Handling
  925.  
  926. import javax.swing.JFrame;
  927.  
  928. class apples {
  929. public static void main(String[] args) {
  930. tuna bucky = new tuna();
  931. bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  932. bucky.setSize(350,100);
  933. bucky.setVisible(true);
  934. }
  935. }
  936.  
  937. import java.awt.FlowLayout;
  938. import java.awt.event.ActionListener;
  939. import java.awt.event.ActionEvent;
  940. import javax.swing.JFrame;
  941. import javax.swing.JTextField;
  942. import javax.swing.JPasswordField;
  943. import javax.swing.JOptionPane;
  944.  
  945. public class tuna extends JFrame {
  946.  
  947. private JTextField item1;
  948. private JTextField item2;
  949. private JTextField item3;
  950. private JPasswordField passwordField;
  951.  
  952. public tuna() {
  953.  
  954. super("The Title Bar");
  955. setLayout(new FlowLayout());
  956.  
  957. item1 = new JTextField(10);
  958. add(item1);
  959.  
  960. item2 = new JTextField("Enter text here");
  961. add(item1);
  962.  
  963. item3 = new JTextField("uneditable", 20);
  964. item3.setEditable(false);
  965. add(item3);
  966.  
  967. passwordField = new JPasswordField("mypass");
  968. add(passwordField);
  969.  
  970. thehandler handler = new thehandler();
  971. item1.addActionListener(handler);
  972. item2.addActionListener(handler);
  973. item3.addActionListener(handler);
  974. passwordField.addActionListener(handler);
  975.  
  976. }
  977.  
  978. private class thehandler implements ActionListener {
  979.  
  980. public void actionPerformed(ActionEvent event) {
  981.  
  982. String string = "";
  983.  
  984. if (event.getSource() == item1)
  985. string = String.format("field 1: %s", event.getActionCommand());
  986. else if(event.getSource() == item2)
  987. string = String.format("field 2: %s", event.getActionCommand());
  988. else if(event.getSource() == item3)
  989. string = String.format("field 3: %s", event.getActionCommand());
  990. else if(event.getSource() == passwordField)
  991. string = String.format("password field is : %s", event.getActionCommand());
  992.  
  993. JOptionPane.showMessageDialog(null, string);
  994.  
  995. }
  996.  
  997. }
  998.  
  999. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement