Professional Documents
Culture Documents
Properties 2. Configure it
Properties: b.text = “press me”; [avoided in java]
JButton Methods: b.setText(“press me”);
Methods 3. Add it
panel.add(b);
Events
JButton
4. Listen to it
Events: Listeners
JLabel
JButton JLabel
1
Build from bottom up Code
Listener
Create: JFrame f = new JFrame(“title”);
JPanel p = new JPanel( );
Frame JLabel JButton JButton b = new JButton(“press me”);
Panel
Components p.add(b); // add button to panel
Listeners f.setContentPane(p); // add panel to frame
Add: (bottom up)
JPanel
listeners into components f.show();
f.show(); press me
}
}
2
Coordinate System Component Hierarchy
Each component has its own subwindow
Upside-down Cartesian Subwindow = rectangular area within parent component
Has own coordinate system
(0,0) (width,0) Clipping:
Can’t paint outside its subwindow
Can’t paint over child components?
(0,0) JPanel
(0,0) JButton
(0,height) (width, height) JButton
(wb, hb)
Ywindow = height - Ycartesian
(wp, hp)
Combinations Combinations
JButton JButton
JButton JButton JFrame
n
JPanel: FlowLayout
JPanel: BorderLayout
JTextArea
c
JTextArea
3
Applets Applet Methods
JApplet is like a JFrame Called by browser:
JApplet
Already has a panel
Access panel with JApplet.getContentPane( )
init( ) - initialization
import javax.swing.*; contentPane start( ) - resume processing (e.g. animations)
stop( ) - pause
class hello extends JApplet {
public void init(){ destroy( ) - cleanup
JButton b = new JButton(“press me”);
JButton paint( ) - redraw stuff (‘expose’ event)
getContentPane().add(b);
}
}
Application + Applet
Command line Browser
Applet Security
import javax.swing.*;
machine
f.show();
}
}
contentPane
class helloApplet extends JApplet {
public void init(){
Communicate only with server
“Java applet window” Warning
// put my mainPanel in the Applet
mainPanel p = new mainPanel();
getContentPane().add(p);
}
}