java programming practice in class buttons icons and clicks
TRANSCRIPT
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
1/21
Copyright © 2012, Oracle. All rights reserved.
Getting Started with Buttons Icons and
Clicks
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
2/21
Copyright © 2012, Oracle. All rights reserved. 2
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Let us create a small project on user interaction in Javausing clipse.
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
3/21
Copyright © 2012, Oracle. All rights reserved. !
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
"e #ill create a ne# "or$space
%ile, itch "or$space, Other,
'ro#se (o )estination %older
nter %older *ame, Clic$ O+
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
4/21
Copyright © 2012, Oracle. All rights reserved.
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
"e #ill create a ne# project named ui-demo
%ile, *e#, Java roject
nter roject *ame as ui-demo
Clic$ %inish
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
5/21
Copyright © 2012, Oracle. All rights reserved. /
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
"e #ill create a ne# pac$age
%ile, *e#, ac$age
&elect &ource %older as ui-demo
nter ac$age name as com.eample.ui-demo
Clic$ %inish
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
6/21
Copyright © 2012, Oracle. All rights reserved.
Getting Started with Green#oot
!cli"se ID!
(he clipse ) loo$s li$e the 3ollo#ing
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
7/21
Copyright © 2012, Oracle. All rights reserved. 4
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Add a main class #ith de3ault main method
&elect created pac$age com.eample.ui-demo
&elect %ile, *e#, Class
nter name as 5ame6ain
&elect pu7lic static void main8&tring9: args
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
8/21Copyright © 2012, Oracle. All rights reserved. ;
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Create a new class to extend JFrame (MainFrame)
Select created package com.example.ui_demo
Select File, New, Class
Enter name as MainFrame
Edit class to ollowing
public class MainFrame extends JFrame{
!e also re"uire to #dd t$e import statement
import javax.swing.JFrame;
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
9/21Copyright © 2012, Oracle. All rights reserved. <
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Add constructor #ith call to super and title as parameter
public 6ain%rame8=>
super 8?5ame anel@=
B
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
10/21Copyright © 2012, Oracle. All rights reserved. 10
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
pdate constructor to set the Layout manager #e #ish to
use.
public MainFrame(){
super("Game panel");
setLayout8new GridLayout(2,2));
DDdisplay 3rame
pac$8= setLocationEelative(o8null); //centre
setFisi7le8true);
B
"e also reGuire to Add another import statement
import jaa!awt!GridLayout;
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
11/21Copyright © 2012, Oracle. All rights reserved. 11
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Add instance o3 6ain%rame to main, and update
constructor to instantiate.
public class 5ame6ain >
public static 6ain%rame gFrame
public static oid main8&tring9: args= >DD create an instance o3 6ain%rame
gFrameH new 6ain%rame8=
B
B
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
12/21Copyright © 2012, Oracle. All rights reserved. 12
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Copy mage 3older to same 3older as ?srcI 3older in project
l d d l i
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
13/21Copyright © 2012, Oracle. All rights reserved. 1!
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Create a ne# class to etend J'utton 8%ruit=, and add event listener
public class %ruit etends J'utton implements ActionListener>
"e also reGuire to Add other import statements and
unimplemented methods to remove errors
import java.a#t.event.Actionvent
import java.a#t.event.ActionListener
import java.s#ing.magecon
import java.s#ing.J'utton
O l A d J F d l I i D
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
14/21Copyright © 2012, Oracle. All rights reserved. 1
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
add class level attri7utes to store images and a 7oolean to
determine i3 a card has 7een turned
priate magecon 3rontmg, 7ac$mg
priate boolean 3acep
O l A d J F d t l I tit t D
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
15/21Copyright © 2012, Oracle. All rights reserved. 1/
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
add a constructor to accept 3ront image and i3 turned. Add
ActionListener method to deal #ith mouse clic$s.
public %ruit8&tring 3ruit*ame, boolean visi7le=>
3rontmg H new magecon83ruit*ame=
7ac$mgH new magecon8@magesDDcard-7ac$.jpg@=
3acepHvisi7le
i# 83acep=>
setcon83rontmg=B
else>
setcon87ac$mg=
B
addActionListener8t$is= DDset to react to mouse clic$s
B
O l A d J F d t l I tit t D
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
16/21Copyright © 2012, Oracle. All rights reserved. 1
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Create an instance o3 a 7utton in the 6ain%rame K 1st
create class level declaration
priate %ruit apple
then instantiate in constructor 7e3ore the code to dra# the 3rame
apple H new %[email protected]@, #alse=
add8apple=
Eun to test, then change 3alse to true and run again. Eeset as 3alse.
O l A d J F d t l I tit t D
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
17/21Copyright © 2012, Oracle. All rights reserved. 14
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Add ! more instances 3or 'ananas, cherries and grapes,
set visi7le parameter in constructor 3or cherries and grapesto ?trueI
priate Fruit apple, c$erries, %rapes, banana;
7anana H new %[email protected]@, #alse=
add87anana=cherries H new %[email protected]@, true=
add8cherries=
grapes H new %[email protected]@, true=
add8grapes=
Eun and test again.
O l A d J F d t l I tit t D
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
18/21Copyright © 2012, Oracle. All rights reserved. 1;
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
*et, #e can add some code to the overridden method
auto created earlier to deal #ith the mouse clic$ in the %ruitClass
public oid actioner3ormed8Actionvent e= >
&ystem.out .println8@mouse clic$ detected@=
i# 83acep=>
setcon83rontmg=
3acepHtrue
B
else>
setcon87ac$mg=
3acepH#alse
B
B
Eun and test again.
Oracle Academy Java Fundamentals Institute Day
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
19/21Copyright © 2012, Oracle. All rights reserved. 1<
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
%inally M create a #ay o3 passing in3ormation 3rom the %ruit
class to the 6ain%rame class to deal #ith situation
Chec$ing n the 6ain%rame class create a class level attri7ute to
$eep trac$ o3 num7er o3 cards 3ace up
priate int cards(urned
in the 3inal line o3 the constructor, set it to 2
cards(urnedH2
Oracle Academy Java Fundamentals Institute: Day
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
20/21Copyright © 2012, Oracle. All rights reserved. 20
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
Create a ne# method in the 6ain%rame class to update
num7er o3 cards 3ace up
public oid update8int change=>
cards(urnedNHchange
&ystem.out .println8@Cards 3ace up @Ncards(urned=
B
n the %ruit class, add a call to the update method #ith the correct
value in the Actioner3ormed method
5ame6ain.gFrame.update81=
Oracle Academy Java Fundamentals Institute: Day
-
8/17/2019 Java Programming Practice in Class Buttons Icons and Clicks
21/2121
Oracle Academy Java Fundamentals Institute: Day
!cli"se Activity
n the %ruit class, add a call to the update method #ith the correct
value in the Actioner3ormed method
i# 83acep=>
setcon83rontmg=
3acepHtrue
5ame6ain.gFrame.update81=
Belse>
setcon87ac$mg=
3acepH#alse
5ame6ain.gFrame.update8K1=
B