C 12 2000 7 2
12 Java(7) { Swing(, ), 1 12.1 12 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 12.2 Swing : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 12.2.1 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 12.2.2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 12.2.3 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 12.3 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 10 12.4 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 14 {i{
12 Java(7) { Swing(, ), 12.1 12 C 12 13 Java 1 GUI GUI 12 Swing 12.2 Swing Java 1 (GUI) Java GUI AWT(Abstract Window Toolkit) AWT GUI Java Swing AWT GUI AWT Swing Java1.1 Java1.2 1 GUI (frame) (parts) GUI ( ) GUI GUI 12.1 Java GUI 1 Swing Java1.1 Java1.2 /usr/local2/java1.2/bin /bin 1
12.1: GUI 12.2.1 GUI GUI Swing JFrame JFrame setsize setvisible C 6 Hello1.java // Hello1.java import java.awt.* import java.awt.event.* import javax.swing.* // // Swing public class Hello1 { public static void main( String [] args ) { JFrame myframe = new JFrame( "SoftC" ) JLabel mylabel = new JLabel( "Hello java world." ) Container mycontainer = myframe.getcontentpane() mycontainer.add( mylabel ) myframe.setsize( 150, 100 ) myframe.setvisible( true ) // Hello1 // main // myframe // mylabel // mycontainer // mycontainer mylabel // myframe // myframe myframe.addwindowlistener( new WindowAdapter() { public void windowclosing( WindowEvent we ) { System.exit(0) ) // 1 \Hello java world." 1 JFrame myframe JLabel \Hello java world." (content pane) AWT 2
Swing Container JFrame getcontentpane Container add setsize setvisible addwindowlistener 2 WindowAdapter windowclosing Hello1.java JFrame JFrame MyFrame.java // MyFrame.java import javax.swing.* import java.awt.* import java.awt.event.* // Swing // ContentPane // public class MyFrame extends JFrame { MyFrame() { super( "SoftC" ) Container mycontainer = getcontentpane() JLabel mylabel = new JLabel( "Hello java world." ) mycontainer.add( mylabel ) setsize( 150, 100 ) setvisible( true ) // // // ContentPane // // // // addwindowlistener( new WindowAdapter() { public void windowclosing( WindowEvent we ) { System.exit( 0 ) ) // // main public static void main( String [] args ) { MyFrame myframe = new MyFrame() 12-1 250 70 JFrame 2 3
12.2.2 Swing (layout manager) 12.1 12.1: (FlowLayout) (GridLayout) (BorderLayout) (CardLayout) (GridBagLayout) (BoxLayout) (OverlayLayout) (ScrollPaneLayout) (ViewportLayout) (JScrollPane) (JViewport) 3 3 public FlowLayout() public FlowLayout( int alignment ) public FlowLayout( int alignment, int horizgap, int vertgap ) alignment FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT // // // horizgap vertgap 4
// MyFlow.java import javax.swing.* import java.awt.* import java.awt.event.* public class MyFlow extends JFrame { MyFlow() { super( "FlowLayout" ) Container mycontainer = getcontentpane() FlowLayout flowlayout = new FlowLayout( FlowLayout.LEFT, 5, 10 ) // FlowLayout mycontainer.setlayout( flowlayout ) // JLabel mylabel1 = new JLabel( "This is 1." ) JLabel mylabel2 = new JLabel( "This is 2." ) JLabel mylabel3 = new JLabel( "This is 3." ) JLabel mylabel4 = new JLabel( "This is 4." ) JLabel mylabel5 = new JLabel( "This is 5." ) mycontainer.add( mylabel1 ) mycontainer.add( mylabel2 ) mycontainer.add( mylabel3 ) mycontainer.add( mylabel4 ) mycontainer.add( mylabel5 ) setsize( 220, 90 ) setvisible( true ) // // // // addwindowlistener( new WindowAdapter() { // public void windowclosing( WindowEvent we ) { System.exit( 0 ) ) // main public static void main( String [] args ) { MyFlow myflow = new MyFlow() 5 12.2: 5
2 3 6 public GridLayout( int row, int column ) public GridLayout( int row, int column, int horizgap, int vertgap ) row column horizgap vertgap MyFlow.java FlowLayout flowlayout = new FlowLayout( FlowLayout.LEFT, 5, 10 ) // FlowLayout mycontainer.setlayout( flowlayout ) // GridLayout gridlayout = new GridLayout( 3, 2, 5, 10 ) // GridLayout mycontainer.setlayout( gridlayout ) // NORTH SOUTH WEST EAST CENTER public BorderLayout() public BorderLayout( int horizgap, int vertgap ) // MyBorder.java import javax.swing.* import java.awt.* import java.awt.event.* public class MyBorder extends JFrame { MyBorder() { super( "BorderLayout" ) Container mycontainer = getcontentpane() BorderLayout borderlayout = new BorderLayout() mycontainer.setlayout( borderlayout ) ( ) // BorderLayout // 6
// JLabel mylabel1 = new JLabel( "This is North.", JLabel.CENTER ) JLabel mylabel2 = new JLabel( "This is South.", JLabel.CENTER ) JLabel mylabel3 = new JLabel( "This is West.", JLabel.CENTER ) JLabel mylabel4 = new JLabel( "This is East.", JLabel.CENTER ) JLabel mylabel5 = new JLabel( "This is Center.", JLabel.CENTER ) // mycontainer.add( mylabel1, BorderLayout.NORTH ) mycontainer.add( mylabel2, BorderLayout.SOUTH ) mycontainer.add( mylabel3, BorderLayout.WEST ) mycontainer.add( mylabel4, BorderLayout.EAST ) mycontainer.add( mylabel5, BorderLayout.CENTER ) setsize( 300, 200 ) setvisible( true ) // // addwindowlistener( new WindowAdapter() { // public void windowclosing( WindowEvent we ) { System.exit( 0 ) ) // main public static void main( String [] args ) { MyBorder myborder = new MyBorder() 12.3: 12-2 7
12.2.3 1 1 Swing (panel) JPanel JPanel 3 MyChoice MyChoice 3 p1 p2 p3 JTextField JCheckBox JButton 12.4 MyChoice p1 JPanel JLabel (l1) JTextField (t) MyChoice p1 p2 p3 l1 t l2 c1 c2 l3 b1 b2 // MyChoice.java import javax.swing.* import java.awt.* import java.awt.event.* 12.4: MyChoice public class MyChoice extends JFrame { public MyChoice() { // super( "My Choice" ) Container mycontainer = getcontentpane() // BorderLayout mycontainer.setlayout( new BorderLayout() ) // p1 JPanel p1 = new JPanel( new FlowLayout() ) JLabel l1 = new JLabel( "Your name please." ) JTextField t = new JTextField( 20 ) p1.add( l1 ) p1.add( t ) ( ) 3 8
// p2 JPanel p2 = new JPanel( new BorderLayout() ) JLabel l2 = new JLabel( "Make sure your choice." ) JCheckBox c1 = new JCheckBox( "OxfordAdvanced Learner's Dictionary" ) JCheckBox c2 = new JCheckBox( "Longman Language Activator" ) p2.add( l2, BorderLayout.NORTH ) p2.add( c1, BorderLayout.CENTER ) p2.add( c2, BorderLayout.SOUTH ) // p3 JPanel p3 = new JPanel( new FlowLayout() ) JLabel l3 = new JLabel( "Are you sure?" ) JButton b1 = new JButton( "Yes, I am." ) JButton b2 = new JButton( "No, I'm not." ) p3.add( l3 ) p3.add( b1 ) p3.add( b2 ) // mycontainer.add( p1, BorderLayout.NORTH ) mycontainer.add( p2, BorderLayout.CENTER ) mycontainer.add( p3, BorderLayout.SOUTH ) pack() setvisible( true ) // // addwindowlistener( new WindowAdapter() { // public void windowclosing( WindowEvent we ) { System.exit( 0 ) ) // main public static void main( String [] args ) { MyChoice p0 = new MyChoice() pack MyChoice 12.5 12.5: MyChoice 9
12-3 3 1 12.3 (event) (event processing) Java 4 java.awt.event MouseEvent WindowEvent (event listener) JButton 5 1 MouseEvent MouseListener mousepressed() MouseListener MouseListener mousepressed ( 12.6 ) 12.2 Button Press! Event MouseEvent MouseListener public void mousepressed() {... 12.6: 4 Java 1.0 Java 1.1 Java 1.1 5 10
12.2: Java 1.1 ActionEvent ActionListener actionperformed() mouseclicked() mouseentered() MouseEvent MouseListener mouseexited() mousepressed() mousereleased() TextEvent TextListener textvaluechanged() windowactivated() windowclosed() windowclosing() WindowEvent WindowListener windowdeactivated() windowiconified() windowdeiconified() windowopened() // MyActionEvent.java import javax.swing.* import java.awt.* public class MyActionEvent extends JFrame { MyActionEvent() { super( "My Action Event" ) Container mycontainer = getcontentpane() JButton mybutton = new JButton( "Button" ) mycontainer.add( mybutton ) setsize( 150, 100 ) setvisible( true ) mybutton.addactionlistener( new ActListener() ) addwindowlistener( new WinListener() ) // // // // // // // // main public static void main( String [] args ) { MyActionEvent myactionevent = new MyActionEvent() 11
// ActListener.java // import java.awt.event.* public class ActListener implements ActionListener { public void actionperformed( ActionEvent ae ) { System.out.println( "Button Pressed!!" ) // WinListener.java // import java.awt.event.* public class WinListener implements WindowListener { public void windowclosing( WindowEvent we ) { System.exit( 0 ) // public void windowopened( WindowEvent we ) { // public void windowclosed( WindowEvent we ) { // public void windowiconified( WindowEvent we ) { // public void windowdeiconified( WindowEvent we ) { // public void windowactivated( WindowEvent we ) { // public void windowdeactivated( WindowEvent we ) { // MyActionEvent JFrame 1 addactionlistener ActListener addwindowlistener WinListener addxxxxlistener XXXX ActListener ActionListener actionperformed \Button Pressed!!" WinListener WindowListener windowclosing System.exit( 0 ) 1 2 WinListener windowclosing Java (adapter) 12
MouseListener MouseAdapter WindowListener WindowAdapter MyActionEvent.java // MyActionEvent.java... mybutton.addactionlistener( new ActListener() ) addwindowlistener( new WinListener() )... // MyActionEvent.java... mybutton.addactionlistener( new ActListener() ) addwindowlistener( new WinAdapter() )... // WinAdapter.java // import java.awt.event.* public class WinAdapter extends WindowAdapter { public void windowclosing( WindowEvent we ) { System.exit( 0 ) // // // // // addwindowlistener (anonymous inner class) 12-4 1 \Mouse Clicked!" 13
12.4 2 1. 250 70 JFrame ( 12-1) 2. ( 12-2) 3. 3 1 ( 12-3) 4. 1 \Mouse Clicked!" ( 12-4) 2 1. Swing AWT MyChoice.java AWT 2. Swing MyChoice.java 3. BoxLayout 12.7 1) 3 Java,, 2) JAVA,, Patrick Niemeyer & Joshua Peck, 3) Java,, Paul Tyma, Gabriel Torok, & Troy Downing, 4) Java,, 5) JAVA,, David Flanagan, 6) JAVA,, Nathan Gurewich & Ori Gurewich, 7) Internet Language Java,, 14
12.7: 8) Java,, Laura Lemay, & Charles L. Perkins,, &, & 9) JAVA2,, 10) Java,, Joseph O'Neil, 11) Java Swing,, Satyaraj Pantham, 12) Swing Java GUI,, 13) Swing Java GUI II,, 14) Swing Java GUI III,, 15) Java Swing Black Book,, Steven Holzner & 16) JBuilder Java,, 15
: xv : Num Lock Num Lock xv $ /usr/local4/bin/xv & xv 12.8: xv xv 12.9: 16
Grab 12.10: Grab xv 12.11 xv 12.11: save 17
12.12: Format: Save File: PostScript LATEX2e \includegraphics LATEX2e A 18