301 Lec 07
301 Lec 07
Containers:
GUI practicalities
1
Comparison:
Example
AWT classes
2
Border layout Event handling
Container contentPane = getContentPane();
• How to connect graphics to action
//Use the content pane's default BorderLayout.
//contentPane.setLayout(new BorderLayout()); //unnecessary • Handled though interfaces “ActionListeners”
3
Event handling patterns: Event handling patterns 2
Reactor pattern Monitor pattern
• Single object: • Combined listener/handler monitors (and acts on)
1. Draws graphics what happens in source
2. Handles events.
4
Design idea 1 Structure
• Use several nested panes for structurally different • Abstract into two classes:
components: 1. Extend Jframe to set up the window and menu bars
2. Extend Jpanel to set up and arrange the calculator
Jframe buttons and displays
JPanel
JFrame
Calculator panel
Class JavaCalcFrame extends Jframe class CalculatorPanel extends JPanel
//constructor for menu etc implements ActionListener, KeyListener
:
// misc events handling, e.g.:
• Implements reactive pattern.
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Example of anonymous
: class implementing listener
//Instantiate main panel
Container contentpane = getContentPane();
CalculatorPanel panel = new CalculatorPanel();
contentPane.add(panel);
5
Hierarchy of panels Hierarchy of panels 2
class CalculatorPanel extends Jpanel //scroll capability
: JScrollPane scrollPane = new JScrollPane(numList);
setLayout(new BorderLayout()); scrollPane.setFont(f);
//Component and panel setup add(scrollPane, "West");
JPanel eastPanel = new JPanel(); scrollPane.addKeyListener(this); Add history
eastPanel.setLayout(new BorderLayout());
//display //button panels
window, alfa and
display = new JTextField(12); JPanel moreButs = new JPanel(); numeric buttons to
display.setHorizontalAlignment(JTextField.RIGHT); moreButs.setLayout(new GridLayout(4, 2));
display.setEditable(false); : intermediate pane
Color col = new Color(255,255,255); eastPanel.add(moreButs, "West");
display.setBackground(col); :
display.addKeyListener(this); centerpanel = new JPanel();
Font f = new Font("MonoSpaced", Font.BOLD, 10); centerpanel.setLayout(new GridLayout(4, 4));
display.setText("0"); eastPanel.add(centerpanel, "Center");
eastPanel.add(display, "North"); :
add(eastPanel, ("Center"));
Add intermediate pane
c.add(b); "%"};
b.addKeyListener(this);
Take care of event handling for (i = 0; i < 8; i++) {
addButton(moreButs, moreButtons[i]);
b.addActionListener(this); in same class }
eastPanel.add(moreButs, "West");
but = b;
}
(Reactive pattern)
centerpanel = new JPanel();
centerpanel.setLayout(new GridLayout(4, 4));
6
Eventhandlers Eventhandlers
Mouse events Key events
//mouse events //Event handlers
public void keyAction(KeyEvent e)
public void actionPerformed(ActionEvent evt)
{
{
char c = e.getKeyChar();
Required in interface //handy substitutions
st = evt.getActionCommand(); if (c == '*') c = 'x';
doAction(st); else if (c == 'p') c = '+';
//enable the Enter keys
}
else if (c == '\n' || c==141)
c = '=';
Call calculation procedure with else if (c == 'c') c = '%';
string of events st = c + "";
if (c == 'q') st = SQRT;
else st = c + "";
doAction(st);
}