Download presentation
Presentation is loading. Please wait.
Published bySimon Cook Modified over 9 years ago
1
CS12420 - Lecture 01 Frames and Components and events Lynda Thomas ltt@aber.ac.uk
2
In This Lecture Frames, panels and layout Basic Components This is the most important of the Swing lectures because here you see the overall way an event driven program works Just a reminder Swing is built on AWT. Some things exist in AWT and in Swing. Example: Frame/JFrame are the AWT and Swing versions. DO NOT MIX!!!!!
3
Frames Generally known as a window JFrame –Title –Buttons to close, maximize, iconize
4
This is where we look at handout 1 if we haven’t already
5
JFrame – some methods setVisible(boolean b) setTitle(String title) setSize(int width,int height) setLocation(int horizontal,int vertical) pack() setDefaultCloseOperation(int operation) –setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
6
JPanels – some methods Used as canvas or containers in Frames JPanel setBackground(Color c) setPreferredSize(Dimension d)
7
But what do you put in your JPanel (or JFrame) ? JComponents like: JLabel- displays stuff JTextField- display and enter JTextArea- display and enter JButton- press and action
8
Layout Manager JFrame : default is BorderLayout –CENTER, NORTH, SOUTH, EAST, WEST JPanel: default is FlowLayout –RIGHT, LEFT, CENTER GridLayout –Rows and columns GridBagLayout –Most powerful (and complicated)
9
So Far in This Lecture Frames, panels and layout probably skip example in 1-frames for now - which is on next 2 slides
10
Driver public class SimpleFrameDriver { public static void main(String[] args) { SimpleFrame sFrame1 = new SimpleFrame(); //etc Frame import javax.swing.*; public class SimpleFrame extends JFrame { SimpleFrame() { this.setSize(200,200); this.setLocation(200,320); //etc
11
public class SimplePanelFrame extends SimpleFrame { //note SimpleFrame gives visibility, size, exiting on close etc. SimplePanelFrame() { ColorPanel CPWest = new ColorPanel(Color.white); add(CPWest,BorderLayout.WEST); //etc public class ColorPanel extends JPanel { ColorPanel(Color c,int w,int h) { this.setPreferredSize (new Dimension(width,height)); this.setBackground(col); } Where do these methods come from? They come from JPanel
12
And this leads us to event driven programming Nothing happens until you click a button (later – move the mouse etc also) Something has to ‘listen’ to button presses These things are called ‘Listeners’ You can have separate Listeners or things like Panels which are also Listeners
13
Edit the simple example (or look at 2-simpleImprovedCode or 2- handout ) import javax.swing.*; /** the panel */ public class MyPanel extends JPanel private JButton button; public MyPanel() { button=new JButton("Hello"); add(button); MyActionListener listener=new MyActionListener(); //added button.addActionListener(listener); }
14
Add a class called MyActionListener import javax.swing.*; import java.awt.event.*; public class MyActionListener implements ActionListener{ public void actionPerformed (ActionEvent e) { System.out.println("pressed button"); }
15
See handout 2 for a nicer version Also show how the Panel could implement ActionListener…
16
Project Specification – handout 3 Counter Up8Down Reset
17
We will build this from the ground up in 3-events-components/Counter handout 3 First the model: CounterModel.java This represents the functionality of the Model Try and separate the Model and the way it is presented
18
public class CounterModel { private int value; public CounterModel() { value = 0; } public void increment() { value++; } //etc } Notice CounterModelTest where we test the model!
19
Components - and some methods JLabel(String text) setText(String text) setForeground(Color c) JButton(String text) Moves us into event-driven programming addActionListener(ActionListener listener)
20
CounterDriver runs the CounterFrame CounterFrame has a CounterPanel in it CounterPanel is guts ….. Contains a link to the Model and Buttons CounterListener listens to the Buttons
21
public class CounterPanel extends JPanel { private CounterModel counter; private JLabel valueLabel; JButton upButton, downButton, resetButton; public CounterPanel() { //setup the layout this.setLayout(new BorderLayout()); //create model and its display: counter = new CounterModel(); valueLabel = new JLabel(""+counter.getValue(),SwingConstants.CENTER); this.add(valueLabel,BorderLayout.CENTER);
22
//this is the constructor from last slide continued … //setup the listener (include a link back to here so that it can update) CounterListener countList = new CounterListener(this); //now do the buttons upButton = new JButton("Up"); //create a button this.add(upButton,BorderLayout.WEST); //position it upButton.addActionListener(countList); //set listener to listen //do same with other buttons //THESE 3 THINGS ALWAYS NEED TO BE DONE FOR ANYTHING //THAT IS RESPONDING TO EVENTS – create, add, listen
23
How does the listener work? import java.awt.event.*; public class CounterListener implements ActionListener { private CounterPanel counterPane; public CounterListener(CounterPanel cp) { counterPane = cp; } public void actionPerformed(ActionEvent evt) { String actionCommand = evt.getActionCommand(); if(actionCommand.equals("Up")) { counterPane.increment(); }//etc
24
UML Class Diagram JFrameJLabelJPanelJButton > ActionListener CounterFrame CounterPanel + increment() + decrement() + reset() CounterModel + increment() + decrement() + reset() + getValue() : int CounterListener + actionPerformed(e : ActionEvent) SimpleFrame + showIt() CounterDriver + main()
25
Lynda – this is handout 3 we’ll act this out (maybe)
26
If this seems complicated …. It is because things are getting all linked up with things pointing to things that point to them One possibility is to make the Panel itself its own listener – see CounterAlternative
27
public class CounterPanelSelfListener extends JPanel implements ActionListener { private CounterModel counter; private JLabel valueLabel; JButton upButton, downButton, resetButton; public CounterPanelSelfListener() { ……………… //now do the buttons upButton = new JButton("Up"); //create a button this.add(upButton,BorderLayout.WEST); //position it upButton.addActionListener(this); ………………. } public void actionPerformed(ActionEvent evt) { //etc
28
If this is too complex look at zzBruteForce – handout 4 BruteForceAndIgnorance.java Everything done in one class. if (this helps) great – look at it but consider the advantages of the other way else ignore it!
29
This Lecture Basic Frames and the things that go in them TODO: put a JTextField in the North that allows the user to enter a starting value for the Counter –There is an example of a JTextField in Bank –You’ll get the most out of it if you try it today. It shouldn’t take very long at all.
30
In The Next Lecture Other kinds of selection and Menus
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.