Download presentation
Presentation is loading. Please wait.
Published byVirginia Fletcher Modified over 6 years ago
2
PC02 Consolidation %WITTY_QUOTE%
3
This is the consolidation for this term
Introduction This is the consolidation for this term Has lots of exercises on all the topics covered And some small things that are interesting, but you haven’t seen yet Start at any point you want Put all these in a single project called “Term01Consolidation” And then packages for each topic you do
4
Choose a Topic to Recap Constructors JFrames JPanels GUI Components
Topic Index Choose a Topic to Recap Constructors JFrames JPanels GUI Components
5
When making classes, we can add constructors
Functions that are run when an object of the class is instantiated Setting memory aside on the computer for the object Often used to ‘set up’ the object Like giving instance variables values
6
When making classes, we can add constructors
Constructors (EX) When making classes, we can add constructors Functions that are run when an object of the class is instantiated Setting memory aside on the computer for the object Often used to ‘set up’ the object Like giving instance variables values
7
Constructors (EX 1) Make a package called constructor_practice
In it, a class called Axolotl Create two instance variables for the name and age Create a constructor without any parameters Sets name to “Unknown” Sets age to 0 Outputs “Making an Axolotl with no known values” to the console Create a class called ConstructorProgram (and give it the main) Instantiate an Axolotl object (using the constructor you made)
8
Constructors (EX 2) Create a constructor in Axolotl with one parameter
The Axolotl’s name This constructor should Assign the parameter to name Set the age to 0 Output “Making name, the Axolotl, with no known age” Instantiate an Axolotl object in the ConstructorProgram main Use this new constructor by giving it a name
9
Constructors (EX 3) Create a constructor in Axolotl with one parameter
The Axolotl’s age This constructor should Assign the parameter to age Set the name to “Unknown” Output “Making a age year old Axolotl with no name” Instantiate an Axolotl object in the ConstructorProgram main Use this new constructor by giving it an age
10
Constructors (EX 4) Create a constructor in Axolotl with two parameters The Axolotl’s name and age This constructor should Assign the parameters to the correct variables Output “Making name, the age year old Axolotl” Instantiate an Axolotl object in the ConstructorProgram main Use this new constructor by giving it a name and an age
11
JFrames act as windows for a GUI
We add a JPanel to it to add other components Can do three main things Make JFrame with title Set the close operation Pack and show
12
JFrames (EX) Make a package called jframe_practice
In it, a class called JFrameProgram Make a JFrame object: Set it to quit the program when closed Set its size Make it visible Try using pack() before making it visible What’s the difference?
13
Allows multiple components to be shown
JPanels Allows multiple components to be shown Rather than just one via JFrames Handles layout (e.g. BorderLayout, GridLayout) Can also change colour It’s background
14
JPanels (EX 1) Make a package called jpanel_practice
In it, a class called JPanelProgram Make a JFrame window (and make it close the program on exit) Make a JPanel object Change its colour Give it a preferred size Add it to the window Pack and show the window What happens if you don’t pack()?
15
JPanels (EX 2) Set the layout of this JPanel to BorderLayout
Create two more JPanels Give them both different colours Give them both the same preferred size Add one JPanel to the north of the main JPanel Add the other JPanel to the south of the main JPanel
16
We have looked at multiple JComponents (things we add to a JPanel)
JLabel JTextField JButton Each have different uses
17
Make a package called jcomponent_practice
JComponents (EX 1) Make a package called jcomponent_practice In it, a class called JComponentProgram Make a JFrame window (and make it close the program on exit) Make a JPanel object (using the BorderLayout) Give the JPanel a colour and preferred size Pack and show the window
18
JComponents (EX 2) Before packing the window, create a JLabel object
Set the font of the JLabel Set the colour of the JLabel Set the text to centrally align horizontally Add the JLabel to the north of the JPanel What does this look like now?
19
JComponents (EX 3) Before packing the window, create a JTextField object Set the font of the JTextField Add the JTextField to the centre of the JPanel What does this look like now? Try adding the JTextField to a blank JPanel (with no layout) And give JTextField a preferred size Then add this new JPanel to centre of main JPanel Does it look different?
20
JComponents (EX 4) Before packing the window, create a JButton object
Give the JButton an anonymous ActionListener Make it get the text from the JTextField And output the text to the console Add the JButton to the south of the main JPanel
21
You will make a timer that counts down from the chosen number
Final Project This will act as a final project (if you don’t need a recap on anything else) You will make a timer that counts down from the chosen number
22
Make a new package called final_project Create a class called Program
Create a new JFrame
23
Set the following for the JFrame:
Final Project (2) Set the following for the JFrame: DefaultCloseOperation to WindowConstants.EXIT_ON_CLOSE Resizable to false Visible to true Pack it
24
Make another class and call it TimerPanel Make it extend JPanel
Final Project (3) Make another class and call it TimerPanel Make it extend JPanel Give it a constructor Also give it three attributes: private int maximum = 30 private int current = maximum private boolean isRunning = false
25
We will need a label as well:
Final Project (4) Change your size and layout. Because our class extends Jpanel, we can set it’s properties like this: setPreferredSize(new Dimension(250, 100)); setLayout(new BorderLayout()); We will need a label as well: JLabel label = new JLabel("" + current, SwingConstants.CENTER); add(label, BorderLayout.CENTER); label.setFont(new Font("Serif", Font.PLAIN, 72));
26
Next, make a new panel and 3 buttons:
Final Project (5) Next, make a new panel and 3 buttons: JPanel panel = new JPanel(); JButton reset = new JButton("Reset"); JButton pause = new JButton("Pause"); JButton play = new JButton("Play"); Add your buttons to the panel: panel.add(play); panel.add(pause); panel.add(reset);
27
Next, make a new panel and 3 buttons:
Final Project (6) Next, make a new panel and 3 buttons: JPanel panel = new JPanel(); JButton reset = new JButton("Reset"); JButton pause = new JButton("Pause"); JButton play = new JButton("Play"); Add your buttons to the panel: panel.add(play); panel.add(pause); panel.add(reset);
28
Now we need to add some action listeners All of them look like this:
Final Project (7) Now we need to add some action listeners All of them look like this: play.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CODE } });
29
They should do the following things:
Final Project (8) They should do the following things: Play: isPlaying = true; Pause: isPlaying = false; Reset: current = maximum; label.setText("" + current); isPlaying = false;
30
Final Project (9) Finally, let’s add the main thread. Make a new Runnable, and in run() add an infinite while loop. It should do the following: if (isPlaying) { if(current <=0){ isPlaying = false; } label.setText("" + current); current--; Thread.sleep(1000); } else { Thread.sleep(100); }
31
You will need a try catch block around the whole thing
Final Project (10) You will need a try catch block around the whole thing Also, make sure to call start().
32
THE END Have a nice holiday!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.