CS 202 Java Object Oriented Programming GUI Programming
CS 202 Java Object Oriented Programming GUI Programming – Introduction Chengyu Sun California State University, Los Angeles
GUI
GUI Components Widgets n Windows, menus, toolbars, buttons, label, text components, lists, … Widgets libraries (APIs) n n n Windows – MFC XWindow – XT, GTK, QT Java AWT and Swing w Cross platform w Slower
Swing GUI Components A Visual Index to the Swing Component n http: //java. sun. com/docs/books/tutorial/uis wing/components. html
GUI Programming Create a container Create components Add components to the container Handle events
What You Should Know General knowledge about what GUI components Swing provide Look up documentations for details about specific components n SUN’s Java Tutorial w Using Swing Components n n Java API documentation Textbook Assemble and layout components together Event handling
Display An Empty Window import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Empty. Frame extends JFrame { public Empty. Frame() { super( "An Empty Window" ); } } set. Size( 400, 400 ); set. Location. Relative. To( null ); set. Default. Close. Operation( EXIT_ON_CLOSE ); public static void main( String args[] ) { Empty. Frame f = new Empty. Frame(); f. set. Visible( true ); }
Panel A general-purposed container Common usage n n A place holder for layout purposes Drawing area for customized paints Important methods n n JPanel() add( Component c ) remove( Component c ), remove. All() paint( Graphics g )
Buttons and Labels JButton n n JButton( String s ) String get. Text() void set. Text( String s) void set. Enabled( boolean b ) JLabel n n n JLabel( String s ) String get. Text() void set. Text( String s)
Text Field and Text Area JText. Field is for single-line text input JText. Area is for multi-line text input Both inherits from JText. Component n n get. Text(), set. Text( String s ), get. Selected. Text() set. Editable( boolean b ), is. Editable() JText. Field(), JText. Field( int cols ), JText. Field( String s ) JText. Area(), JText. Area( String s )
Adding Component All components must be added to the content pane, except the menu bar get. Content. Pane() Container cp = get. Content. Pane(); cp. add( some_component ); Title Menu Content Pane
Layouts
More Layouts http: //java. sun. com/docs/ books/tutorial/uiswing/lay out/index. html
Border Layout Default layout for applet, content pane, and dialog box WEST CENTER JButton b 1 = new JButton(“Button 1”); JButton b 2 = new JButton(“Button 2”); cp. add( b 1, Border. Layout. NORTH ); cp. add( b 2, Border. Layout. CENTER ); SOUTH EAST cp = get. Content. Pane(); cp. set. Layout( new Border. Layout() ); NORTH
Flow Layout Adds components from left to right Starts a new row if necessary cp = get. Content. Pane(); cp. set. Layout( new Flow. Layout() ); JButton b 1 = new JButton(“Button 1”); JButton b 2 = new JButton(“Button 2”); cp. add( b 1 ); cp. add( b 2 );
Grid Layout columns cp = get. Content. Pane(); cp. set. Layout( new Grid. Layout(2, 3) ); JButton b 1 = new JButton(“Button 1”); JButton b 2 = new JButton(“Button 2”); cp. add( b 1 ); cp. add( b 2 ); rows Either rows or cols can be zero All components are of equal size 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Box Layout Lay out components in one row or one column Can do vertical layout (vs. Flow. Layout) Do not force components to be equal size (vs. Grid. Layout) Constructor n Box. Layout( Container c, int axis ) cp = get. Content. Pane(); cp. set. Layout( new Box. Layout(cp, Box. Layout. X_AXIS) ); JButton b 1 = new JButton(“Button 1”); cp. add( b 1 );
Gridbag Layout The most flexible layout, and The most difficult to use Often can be “simulated” with a combination of simpler layouts
Handling Action Events import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Frame. Test implements Action. Listener { JButton b; public Frame. Test() { b = new JButton( “Button 1” ); b. add. Action. Listener( this ); } } public void action. Performed( Action. Event e ) { // code that handling action events }
Action. Event Object get. Source() String get. Action. Command()
- Slides: 20