C 13 2000 7 9
13 Java(8) { Swing(2)(, ) 1 13.1 13 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 13.2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 13.2.1 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 13.2.2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 13.2.3 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 13.2.4 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 13.3 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 13.3.1 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 13.3.2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 10 13.4 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 14 {i{
13 Java(8) { Swing(2)(, ) 13.1 13 12 Swing 13 13.2 Swing 13.2.1 (label) JLabel JLabel 1 public JLabel() public JLabel( String text ) public JLabel( String text, int alignment ) text alignment JLabel.LEFT JLabel.CENTER JLabel.RIGHT // // // 13.2.2 (button) JButton JButton 1 1
2 public JButton() public JButton( String text ) 1 2 text JButton ActionEvent // ButtonAction.java import javax.swing.* import java.awt.* public class ButtonAction extends JFrame { ButtonAction() { super( "Button Action" ) Container mycontainer = getcontentpane() JButton mybutton = new JButton( "Button" ) mycontainer.add( mybutton ) setsize( 150, 100 ) setvisible( true ) // // // // // // mybutton.addactionlistener( new ActListener2( mybutton ) ) // addwindowlistener( new WinAdapter() ) // // main public static void main( String [] args ) { ButtonAction buttonaction = new ButtonAction() // WinAdapter.java // import java.awt.event.* public class WinAdapter extends WindowAdapter { public void windowclosing( WindowEvent we ) { System.exit( 0 ) // 2 2
// ActListener2.java import javax.swing.* import java.awt.event.* public class ActListener2 implements ActionListener { JButton button // boolean fbutton = true // ActListener2( JButton button ) { this.button = button // public void actionperformed( ActionEvent ae ) { if( fbutton ) { button.settext( "Press" ) // fbutton = false // else { button.settext( "Button" ) // fbutton = true // 12 MyActionEvent.java 12 settext JComponent Swing JComponent 13.2.3 (check box) 3 public JCheckBox() public JCheckBox( String text ) public JCheckBox( String text, boolean selected ) text selected true JCheckBox setselected( boolean selected ) JCheckBox ItemEvent ItemListener itemstatechanged 3 JLabel JButton 3
13.2.4 (text eld) 1 public JTextField() public JTextField( String text ) public JTextField( int column ) public JTextField( String text, int column ) text column JTextField Return ( Enter ) ActionEvent 12 MyActionEvent.java ActionListener actionperformed 12 MyChoice.java // TextCheckEvent.java import javax.swing.* import java.awt.* import java.awt.event.* public class TextCheckEvent extends JFrame implements ItemListener, ActionListener { JCheckBox c1, c2 JLabel l4 TextCheckEvent() { super( "TextCheckEvent" ) Container mycontainer = getcontentpane() 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 ) // p2 JPanel p2 = new JPanel( new BorderLayout() ) JLabel l2 = new JLabel( "Make sure your choice." ) c1 = new JCheckBox( "OxfordAdvanced Learner's Dictionary" ) c2 = new JCheckBox( "Longman Language Activator" ) p2.add( l2, BorderLayout.NORTH ) p2.add( c1, BorderLayout.CENTER ) p2.add( c2, BorderLayout.SOUTH ) // p3 ( p4 l4) JPanel p3 = new JPanel( new BorderLayout() ) // p4 JPanel p4 = new JPanel( new FlowLayout() ) ( ) 4
JLabel l3 = new JLabel( "Are you sure?" ) JButton b1 = new JButton( "Yes, I am." ) JButton b2 = new JButton( "No, I'm not." ) p4.add( l3 ) p4.add( b1 ) p4.add( b2 ) // l4 l4 = new JLabel( " " ) p3.add( p4, BorderLayout.NORTH ) p3.add( l4, BorderLayout.SOUTH ) // p4 // l4 mycontainer.add( p1, BorderLayout.NORTH ) mycontainer.add( p2, BorderLayout.CENTER ) mycontainer.add( p3, BorderLayout.SOUTH ) // p1 // p2 // p3 pack() setvisible( true ) // // t.addactionlistener( this ) c1.additemlistener( this ) c2.additemlistener( this ) // addwindowlistener( new WinAdapter() ) // public void actionperformed( ActionEvent ae ) { l4.settext( ae.getactioncommand() ) // public void itemstatechanged( ItemEvent ie ) { if( ie.getitemselectable() == c1 ) { if( c1.isselected() ) l4.settext( "You checked Oxford." ) else l4.settext( "You checked Oxford off." ) else { if( c2.isselected() ) l4.settext( "You checked Longman." ) else l4.settext( "You checked Longman off." ) // main public static void main( String [] args ) { TextCheckEvent textcheckevent = new TextCheckEvent() 12 MyChoice.java ItemEvent ActionEvent TextCheckEvent JFrame ItemListener ActionListener this t c1 c2 5
l4 ActionEvent getactioncommand 4 l4 ItemEvent getitemselectable isselected getactioncommand JTextField gettext 13-1 TextCheckEvent.java \Yes, I am." <name> selects <dic> <name> <dic> \No, I'm not." 13-2 JTextField 13.1 5 13.1: 4 5 4 1 100 1 400 1 6
13.3 13.3.1 Swing JComponent JComponent Swing paintcomponent Graphics void setcolor( Color c ) c void drawstring( String text, int x, int y ) text (x, y) void drawline( int x1, int y1, int x2, int y2 ) (x1, y1) (x2, y2) void drawrect( int x, int y, int w, int h ) (x, y) w h void fillrect( int x, int y, int w, int h ) void clearrect( int x, int y, int w, int h ) void drawoval( int x, int y, int w, int h ) (x, y) w h ( ) void filloval( int x, int y, int w, int h ) ( ) void drawarc( int x, int y, int w, int h, int deg0, int deg1 ) (x, y) w h deg0 deg1 void fillarc( int x, int y, int w, int h, int deg0, int deg1 ) void drawpolygon( int [] x, int [] y, int n ) x y n void fillpolygon( int [] x, int [] y, int n ) void drawpolyline( int [] x, int [] y, int n ) x y n 7
Color Color public Color( int red, int green, int blue ) public Color( int rgb ) public Color( float r, float g, float b ) (red) (green) (blue) 0 255 int 2 rgb 23 16 15 8 7 0 16 3 (r) (g) (b) 0.0f 1.0f float Color 13.1 13.1: Color black yellow white orange red pink gree darkgray blue gray cyan lightgray magenta // MyDraw.java import javax.swing.* import java.awt.* public class MyDraw extends JComponent { MyDraw() { setpreferredsize( new Dimension( 300, 300 ) ) // paintcomponent public void paintcomponent( Graphics g ) { super.paintcomponent( g ) g.setcolor( Color.blue ) g.drawstring( "Graphics objects.", 50, 50 ) g.setcolor( Color.yellow ) g.drawoval( 10, 70, 80, 80 ) g.setcolor( Color.green ) g.fillrect( 100, 100, 150, 50 ) g.setcolor( Color.red ) g.fillarc( 50, 150, 200, 200, 0, 60 ) // // // // // // // 8
// DrawTest.java import javax.swing.* import java.awt.* import java.awt.event.* public class DrawTest extends JFrame { public DrawTest() { // super( "DrawTest" ) Container mycontainer = getcontentpane() MyDraw mydraw = new MyDraw() mycontainer.add( mydraw ) // // addwindowlistener( new WinAdapter() ) pack() setvisible( true ) // main public static void main( String [] args ) { DrawTest DrawTest = new DrawTest() DrawTest 13.2 13.2: DrawTest MyDraw JComponent MyDraw 6 6 (component) 9
setpreferredsize setpreferredsize Dimension Dimension paintcomponent Graphics super.paintcomponent( g ) paintcomponent paintcomponent paintcomponent paintcomponent paintcomponent DrawTest MyDraw 13.3.2 // DrawSine.java import javax.swing.* import java.awt.* public class DrawSine extends JComponent { int startx DrawSine() { startx = 0 setpreferredsize( new Dimension( 360, 200 ) ) // // paintcomponent public void paintcomponent( Graphics g ) { super.paintcomponent( g ) // g.drawline( 0, 100, 360, 100 ) // // int [] xpoints = new int [37] int [] ypoints = new int [37] // for( int i = 0 i < xpoints.length i++ ) { xpoints[i] = i * 10 ypoints[i] = (int)(100-100*math.sin( 2*Math.PI*(startX+i)/36 )) // g.setcolor( Color.red ) g.drawpolyline( xpoints, ypoints, xpoints.length ) ( ) 10
// void move() { Graphics g = getgraphics() // Graphics for( int i = 0 i < 20 i++ ) { // g.clearrect( 0, 0, getwidth(), getheight() ) startx-- update( g ) // // // try { Thread.sleep( 50 ) catch( Exception e ) { e.printstacktrace() // void reset() { startx = 0 // startx repaint() // // DrawAction.java import javax.swing.* import java.awt.* import java.awt.event.* public class DrawAction extends JFrame implements ActionListener { DrawSine drawsine // public DrawAction() { super( "DrawAction" ) Container mycontainer = getcontentpane() // drawsine = new DrawSine() // JPanel panel = new JPanel( new FlowLayout() ) JButton move = new JButton( "MOVE" ) JButton reset = new JButton( "RESET" ) JButton exit = new JButton( "EXIT" ) panel.add( move ) panel.add( reset ) panel.add( exit ) ( ) 11
// mycontainer.add( drawsine, BorderLayout.NORTH ) mycontainer.add( panel, BorderLayout.SOUTH ) // move.addactionlistener( this ) reset.addactionlistener( this ) exit.addactionlistener( this ) addwindowlistener( new WinAdapter() ) pack() setvisible( true ) // public void actionperformed( ActionEvent ae ) { if( ae.getactioncommand().equals( "MOVE" ) ) drawsine.move() else if( ae.getactioncommand().equals( "RESET" ) ) drawsine.reset() else System.exit( 0 ) // main public static void main( String [] args ) { DrawAction drawaction = new DrawAction() MOVE RESET EXIT 3 MOVE RESET EXIT DrawSine startx paintcomponent startx MOVE DrawSine move Graphics getgraphics paintcomponent startx update JComponent update reset startx repaint 12
13-3 JTextField 3 2x Physical = sin 23 2x Sympathy = sin 28 2x Intellectual = sin 33 x 13.3 13.3: 13
13.4 2 1. TextCheckEvent.java \Yes, I am." <name> selects <dic> <name> <dic> \No, I'm not." ( 13-1) 2. JTextField 13.1 ( 13-2) 3. JTextField 3 2x Physical = sin 23 2x Sympathy = sin 28 2x Intellectual = sin 33 x 13.3 ( 13-3) 2 1. Swing 2. /usr/local4/doc/softabc_2001/softc_psfiles/data/hatena.class 3. DrawSine.java DrawSine.java 4. 1) 3 Java,, 14
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,, 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