[email protected] CSM6120 Introduction to Intelligent Systems Neural Networks.
CS12420 - Lecture 01 Frames and Components and events Lynda Thomas [email protected].
-
Upload
simon-cook -
Category
Documents
-
view
215 -
download
0
Transcript of CS12420 - Lecture 01 Frames and Components and events Lynda Thomas [email protected].
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!!!!!
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)
JPanels – some methods
• Used as canvas or containers in Frames
• JPanelsetBackground(Color c)
setPreferredSize(Dimension d)
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
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)
So Far in This Lecture
• Frames, panels and layout
• probably skip example in 1-frames
for now - which is on next 2 slides
Driverpublic class SimpleFrameDriver {
public static void main(String[] args) {SimpleFrame sFrame1 = new SimpleFrame(); //etc
Frameimport javax.swing.*;public class SimpleFrame extends JFrame {
SimpleFrame() {this.setSize(200,200);this.setLocation(200,320); //etc
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
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
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); }
}
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");
}
}
We will build this from the ground up in 3-events-components/Counter
handout 3First the model: CounterModel.java
This represents the functionality of the Model
Try and separate the Model and the way it is presented
public class CounterModel {
private int value;
public CounterModel() {
value = 0;
}
public void increment() {
value++;
}
//etc
}
Notice CounterModelTest where we test the model!
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)
CounterDriver runs the CounterFrameCounterFrame has a CounterPanel in it
CounterPanel is guts …..Contains a link to the Model and Buttons
CounterListener listens to the Buttons
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);
//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
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
UML Class Diagram
JFrame JLabelJPanel JButton<<interface>>ActionListener
CounterFrame
CounterPanel+ increment()+ decrement()+ reset()
CounterModel+ increment()+ decrement()+ reset()+ getValue() : int
CounterListener+ actionPerformed(e : ActionEvent)
SimpleFrame+ showIt()
CounterDriver+ main()
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
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
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!
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.