《java_SUN_JAVA 培训专用教材c11-15.pdf》由会员分享,可在线阅读,更多相关《java_SUN_JAVA 培训专用教材c11-15.pdf(63页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 Java?JDK1.2?Java?Swing?Swing?AWT?(?100%?Java?)?JFC?Swing?AWT?Java?JFC?GUI?JFC?Swing?Swing?AWT?104?Java?com.sun.java.swing?Swing?Swing GUI?Swing?Jframe?Japplet?The Java Tutorial?Sun Microsystems?http:/ AWT Java2D Accessibility Drag&Drop Swing Java?GUI?Java?JFC?JDK1.2?5?API?AWT?Java2D?Accessibility?D
2、rag&Drop?Swing?AWT?Java?GUI?Java2D?API?Java?2D?Java2D API?java.awt?java.awt.image?API?Accessibility API?Drag&Drop?Java?Java?Java?JFC?Swing?Swing?GUI?11.3.1 Swing?105 Swing?Swing?Swing?AWT?API?AWT?Swing?GUI?Java?Windows?Unix?Unix?Swing?Applet?GUI?Swing?AWT?Swing?Swing API?AWT?API?AWT?AWT?Swing?Swing?
3、JFC?Java2D?Accessibility?Drag&Drop?Accessibility API?AWT?JFC?Swing?Swing?Swing?JComponent?JComponent?AWT?Swing?Swing?106 Swing GUI?GUI?GUI?GUI?JComponent?“J”?GUI?GUI?Swing?GUI?Swing?Swing?pane?pane?JApplet JButton JComboBox JOptionPane 107 JList Swing?JLabel JScrollPane JTable JScrollBar JSlider JTo
4、oltip JTree?Swing?HelloSwing?11.5.1 HelloSwing 108 1.import java.awt.*;2.import java.awt.event.*;3.import com.sun.java.swing.*;4.import java.awt.accessibility.*;5.6.public class HelloSwing implements ActionListener 7.private JFrame jFrame;8.private JLabel jLabel;9.private JPanel jPanel;10.private JB
5、utton jButton;11.private AccessibleContext accContext;12.13.private String labelPrefix=14.Number of button clicks:;15.private int numClicks=0;16.17.public void go()18.19./Here is how you can set up a particular 20./lookAndFeel.Not necessary for default.21./22./try 23./UIManager.setLookAndFeel(24./UI
6、Manager.getLookAndFeel();25./catch(UnsupportedLookAndFeelException e)26./System.err.println(Couldnt use the +27./default look and feel +e);28./29.30.jFrame=new JFrame(HelloSwing);31.jLabel=new JLabel(labelPrefix+0);32.33.jButton=new JButton(I am a Swing button!);34.35./Create a shortcut:make ALT-A b
7、e equivalent 36./to pressing mouse over button.37.jButton.setMnemonic(i);38.39.jButton.addActionListener(this);40.HelloSwing?1./Add support for accessibility.2.accContext=jButton.getAccessibleContext();3.accContext.setAccessibleDescription(4.Pressing this button increments +5.the number of button cl
8、icks);109 6.7./Set up pane.8./Give it a border around the edges.9.jPanel=new JPanel();10.jPanel.setBorder(11.BorderFactory.createEmptyBorder(12.30,30,10,30);13.14./Arrange for compts to be in a single column.15.jPanel.setLayout(new GridLayout(0,1);16.17./Put compts in pane,not in JFrame directly.18.
9、jPanel.add(jButton);19.jPanel.add(jLabel);20.jFrame.setContentPane(jPanel);21.22./Set up a WindowListener inner class to handle 23./windows quit button.24.WindowListener wl=new WindowAdapter()25.public void windowClosing(WindowEvent e)26.System.exit(0);27.28.;29.jFrame.addWindowListener(wl);30.31.jF
10、rame.pack();32.jFrame.setVisible(true);33.34.HelloSwing?1./Button handling.2.public void actionPerformed(ActionEvent e)3.numClicks+;4.jLabel.setText(labelPrefix+numClicks);5.6.7.public static void main(String args)8.9.HelloSwing helloSwing=new HelloSwing();10.helloSwing.go();11.12.11.4.2?Swing?110?S
11、wing?getLookAndFeel()?JFrame?Frame?JFrame?content pane?Frame?import com.sun.java.swing.*?Swing?Swing?Hello Swing?22?28?getLookAndFeel()?Windows?Solaris?CDE?/Motif?11.4.3?Swing?JFrame?JFrame?AWT Frame?Swing?Hello Swing?JFrame?public HelloSwing()JFrame jFrame;JPanel jPanel;jFrame=new JFrame(HelloSwing
12、);jPanel=new JPanel();jFrame.setContentPane(jPanel);?Frame?JFrame?JFrame?content pane?content pane?content pane?JFrame?content pane?getContentPane()?content pane?set ContentPane()?11.4.4?Swing?Swing?Swing?Hello Swing.java?4?Swing?4?JFrame?JButton?JLabel?JPanel?Hello Swing.java?accContext=jButton.get
13、AccessibleContext();accContext.setAccessibleDescription(Pressing this button increments +the number of button clicks.);111 Hello Swing?4?JFrame?JButton?JLabel?JPanel?Hello Swing?33?45?JButton?33?37?ACT?I?39?41?45?49?59?JPanel?JPanel?panel?Panel?Hello Swing?Panel?11.4.5?Hello Swing.java?accContext=jB
14、utton.getAccessibleContext();accContext.setAccessibleDescription(Pressing this button increments +the number of button clicks.);?jButton=new JButton(Im a Swing button!);jLabel=new JLabel(labelPrefix+0);jLabel.setText(labelPrefix+numClicks);?JFrame?JButton?JLabel?Accessibility?Swing GUI?Swing GUI?JFr
15、ame?JApplet?JDialog?JWindow?JButton?JPanel?JMenu?Swing?content pane?Swing?JFrame?JApplet?JWindow?JDialog?J?JButton?JPanel?JMenu?Swing?AWT?Swing?Swing?AWT?Swing?Swing?Swing?Applet?JApplet?java.applet.Applet?Swing?JFrame?Swing?Swing?Swing?Swing?content pane?content pane?Swing?JPanel?Swing?GUI?2?Jframe
16、(a top-level Swing container)112?content pane?+?JButton JButton JPanel JTextField +?JList?GUI?GUI?Applet?JApplet(a top-level Swing container)?content pane?+?JButton JButton JPanel?+?JTextField JList?GUI?1.import com.sun.java.swing.*;2.import java.awt.*;3.4.public class SwingGUI 5.6.JFrame topLevel;7
17、.JPanel jPanel;8.JTextField jTextField;9.JList jList;10.11.JButton b1;12.JButton b2;13.Container contentPane;14.15.Object listData=16.newString(First selection),17.new String(Second selection),18.new String(Third selection)19.;20.21.public static void main(String args)22.SwingGUI swingGUI=new SwingG
18、UI();113 23.swingGUI.go();24.25.26.public void go()27.topLevel=new JFrame(Swing GUI);28.29./Set up the JPanel,which contains the text field 30./and list.31.jPanel=new JPanel();32.jTextField=new JTextField(20);33.jList=new JList(listData);34.35.contentPane=topLevel.getContentPane();36.contentPane.set
19、Layout(new BorderLayout();37.38.b1=new JButton(1);39.b2=new JButton(2);40.contentPane.add(b1,BorderLayout.NORTH);41.contentPane.add(b2,BorderLayout.SOUTH);42.43.jPanel.setLayout(new FlowLayout();44.jPanel.add(jTextField);45.jPanel.add(jList);46.contentPane.add(jPanel,BorderLayout.CENTER);47.48.topLe
20、vel.pack();49.topLevel.setVisible(true);50.51.?JComponent?JComponent?Swing?JComponent?Swing?JComponent?JComponent?Container?Swing?JComponent?setBorder()?EmptyBorder?114?Swing?Swing?setToolTipText()?registerKeyboardAction()?GUI?KeyStroke?Java?GUIManager?Swing?UIManager.setLookAndFeel()?Swing?JCompone
21、nt?ComponentGUI?JComponent?Swing?GUI?Swing?Swing?AWT?1?Swing?1.?HelloSwing?2.?ImageIcon?3.?“JFC Button”?2?Swing?1.?JFrame?JToolBar?TextArea?JLabel?2.?JMenuBar?JFrame?3.?JMenuBar?JMenu?JMenuItems?New?Open?Save?Close?4.?5.?JMenuItem?ActionListener?6.?About JMenuItem?HelpJMenu?JMenuBar?H?A?7.?About JMe
22、nuItem?8.?4?JButton?New?Open?Save?About?9.?ActionListener?10.?115?Java?com.sun.java.swing?Swing?Swing GUI?Swing?JFrame?JApplet?GUI?Web?GUI?116?11?Jvav?JDK?Applet?Applet?Applet?117?Applet?HTML?Java Applet?Applet?AWT?HelloWorld.Java Applet?Applet?AWT?Applet?URL?Applet Applet?Applet?Applet?HTML?Web?Jav
23、a?URL?HTML?Applet?Applet Applet?HTML?Web?Java?Java?(container)?main()?Applet?Applet?12.3.1?Applet?Applet?Web?HTML?118 1.2.3.4.?URL?HTML?Applet?Applet 12.3.2 Applet?Applet?/?Applet?Socket?Applet?Internet?Netscape Nevigator?/?Applet?Socket?Applet?Applet?JVM?Socket?JDK1.2?“?”?Applet?URL?Applet?Applet?A
24、pplet?119?Applet?Applet?import java.applet.*;public class HelloWorld extends Applet Applet?public?HelloWorld.java?java.applet.Applet?Applet?Java.applet.Applet?java.awt.Panel?Applet?AWT?Applet?AWT?Applet?Panel?(flow)?Component?Container?Panel?Applet?12.4.1?Applet?Applet?init()start()stop()destroy()pa
25、int()?main()?Applet?init()?Applet?init()?start()?start()?start()?Applet?init()?start()?Applet?“?”?Applet?main()?Applet?Applet?stop()?destroy()?paint()?12.4.2 Applet?Applet?Applet?paint()?120 Applet?System.out.println()?paint()?Applet?panel?Applet?paint()?paint()?12.4.3 paint()?paint()?java.awt.Graph
26、ics?Applet?panel?Applet?paint()?1.import java.awt.*;2.import java.applet.*;3.4.public class HelloWorld extends Applet 5.6.public void paint(Graphics g)7.g.drawString(Hello World!,25,25);8.9.?drawString?x?y?0?0?y?0?y?Applet?Applet?Applet?Applet?init()?Applet?start()?Applet?stop()?Applet?Applet?init()
27、?start()?stop()?121 12.5.1 init()?Applet?Java?appletviewer?Applet?Applet?start()?12.5.2 start()init()?start()?Applet?“?”?Applet?URL?Applet?1.public void start()2.musicClip.play();3.12.5.3 stop()stop()?Applet?URL?Applet?1.public void stop()2.musicClip.stop();3.start()?stop()?start()?Applet?stop()?AWT
28、?AWT?paint(Graphics g)repaint()update(Graphics g)?Applet?AWT?AWT?AWT?exposure?12.6.1 Paint(Graphics g)?paint()?Graphics?paint()?12.6.2 repaint()?repaint()?paint()?122 12.6.3 update(Graphics g)?repaint()?update()?AWT?update?paint()?update()?paint()?12.6.4?paint()?update()?repaint()?12.6.5 Applet?Appl
29、et?paint()?repaint()?Applet?paint()?paint()?paint()?repaint()?update()?paint()?AWT?AWT?paint()?update()?AWT?14?appletviewer?appletviewer?123?appletviewer?Web?Applet?Java?HTML?appletViewer HelloWorld.html?HTML?Applet?Web?HotJava TM?Netscape Navigator?Java?JDK?Applet?HTML?appletviewer?appletviewer?Web
30、?Applet?Java?“?”?appletviewer?URL?HTML?Applet?appletviewer?HTML?HTML?Applet?12.7.1?appletviewer?Applet appletviewer?Applet?Applet?appletviewer?URL?Applet?HTML?Applet?appletviewer?HTML?HTML?Applet?12.7.2?appletviewer?appletviewer?HTML?URL?URL?appletviewer-debug URLs appletviewer?debug?Applet?Java?jdb
31、?g?Java?appletviewer?appletviewer?c:jdk1.2source appletviewer HelloWorld.html 124?Applet?12.8.1?Applet?alternateHTML?archive=archiveList?“?”?archives?codebase?AppletClassLoader?ArchiveList?archives?code=appletFile.class?Applet?package.appletFile.class?HTML?URL?Applet?URL?width=pixels height=pixels?A
32、pplet?Applet?12.8.2?codebase=codebaseURL?Applet?URL?Applet?125?URL?alt=alternateText?Applet?Java Applet?name=appletInstanceName?Applet?Applet?align=alignment?Applet?HTML?IMG?left?right?top?texttop?middle?absmiddle?baseline?bottom?absbottom?vspace=pixels hspace=pixels?Applet?vspace?hspace?IMG?vspace?
33、hspace?“?”?Applet?Java?Applet?getParameter()?Java?HTML?Java?HTML?Applet?Applet?getDocumentBase()?URL?getCodeBase()?Applet?URL?getImage(URL base,String target)?getAudioClip(URL base,String target)?URL?Applet?Java?15?Applet?.URL?URL?Applet?URL?getDocumentBase()?Applet?HTML?URL?getCodeBase()?Applet?URL
34、?HTML?Applet?URL?Applet?getImage(URL base,String target)?target?base?126?Image?getAudioClip(URL base,String target)?target?base?Audio Clip?getImage(URL,String)?getAudioClip(URL,String)?String target?URL?Applet?getDocumentBase?graphics/joe.gif?appletviewer?1./HelloWorld extended to draw an image 2./A
35、ssumes existence of 3./graphics/SurferDuke.gif 4./5.import java.awt.*;6.import java.applet.Applet;7.8.public class HwImage extends Applet 9.Image duke;10.11.public void init()12.duke=getImage(getDocumentBase(),13.graphics/SurferDuke.gif);14.15.16.public void paint(Graphics g)17.g.drawImage(duke,25,2
36、5,this);18.19.drawImage()?Image?x?y?getImage()?paint()?paint()?Applet?drawImage()?127?Audio Clips Java?Audio Clips?java.applet.AudioClip?Audio Clips?12.11.1?Clip?audio clip?Applet?play?play(URL soundDirectory,String soundFile);?play(URL soundURL);?play(getDocumentBase(),bark.au);?HTML?bark.au?12.11.
37、2?Audio?Applet?appletviewer?“Audio Test”?audio?sounds/cuckoo.au?1./2./HelloWorld extended to play an Audio sound 3./Assumes existence of sounds/cuckoo.au file 4./5.6.import java.awt.Graphics;7.import java.applet.Applet;8.9.public class HwAudio extends Applet 10.11.public void paint(Graphics g)12.g.d
38、rawString(Audio Test,25,25);13.play(getDocumentBase(),sounds/cuckoo.au);14.15.12.11.3?Audio Clip?Audio Clip?Audio Clip?Audio Clip?Audio Clip?audio clip?Audio Clip?Audio Clip?java.applet.Applet?getAudioClip?AudioClip sound;sound=getAudioClip(getDocumentBase(),bark.au);?clip?play?loop?stop?128?Audio C
39、lip?java.applet.AudioClip?play?audio clip?sound.play();?clip?java.applet.AudioClip?loop?sound.loop();?Audio Clip?clip?java.applet.AudioClip?stop?sound.stop();12.11.4?Audio?audio clip?1./2./HelloWorld extended to loop an audio track 3./Assumes existence of sounds/cuckoo.au 4./5.6.import java.awt.Grap
40、hics;7.import java.applet.*;8.9.public class HwLoop extends Applet 10.AudioClip sound;11.12.public void init()13.sound=getAudioClip(getDocumentBase(),14.sounds/cuckoo.au);15.16.17.public void paint(Graphics g)18.g.drawString(Audio Test,25,25);19.20.21.public void start()22.sound.loop();23.24.25.publ
41、ic void stop()26.sound.stop();27.?JDK1.2?MIDI?.wav?aiff?.au?newAudioClip(URL url)?URL?audio clip?URL?audio clip?13?getAudioClip?NewAudioClip?String?URL?129?Java?Java Applet?9?JDK1.2?MouseListener?mouseClicked?mouseEntered?mouseExited?mousePressed?mouseReleased?12.12.1?Mouse?Applet?1./2./HelloWorld e
42、xtended to watch for mouse input 3./Hello World!is reprinted at the location of 4./the mouse click.5./6.7.import java.awt.*;8.import java.awt.event.*;9.import java.applet.Applet;10.11.public class HwMouse extends Applet 12.implements MouseListener 13.14.int mouseX=25;15.int mouseY=25;16.17./Register
43、 this applet instance to catch/MouseListener events 18.public void init()19.addMouseListener(this);20.21.22.public void paint(Graphics g)23.g.drawString(Hello World!,mouseX,mouseY);24.25.26./Process the mousePressed MouseListener event 27.public void mousePressed(MouseEvent evt)28.mouseX=evt.getX();
44、130 29.mouseY=evt.getY();30.repaint();31.32.33./We are not using the other mouse events 34.public void mouseClicked(MouseEvent e)35.public void mouseEntered(MouseEvent e)36.public void mouseExited(MouseEvent e)37.public void mouseReleased(MouseEvent e)38.39.?HTML?Applet?Applet?getParameter()?1.impor
45、t java.awt.*;2.import java.applet.*;3.4.public class DrawAny extends Applet 5.Image im;6.7.public void init()8.URL url=getDocumentBase();9.String imageName=getParameter(image );10.im=getImage(url,imageName);11.12.13.public void paint(Graphics g)14.g.drawImage(im,0,0,this);15.16.?getParameter()?getPa
46、rameter()?null?String?int?int speed=Integer.parseInt(getParameter(speed );?HTML?131?Java Applet?Java?Java?Applet/?1./Applet/Application which shows an image of Duke in 2./surfing mode 3.import java.applet.Applet;4.import java.awt.*;5.import java.awt.event.*;6.import java.util.*;7.8.public class Appl
47、etApp extends Applet 9.10.Date date;11.12./An application will require a main()13.public static void main(String args)14.15./Create a Frame to house the applet 16.Frame frame=new Frame(Application);17.18./Create an instance of the class(applet)19.AppletApp app=new AppletApp();20.21./Add it to the ce
48、nter of the frame 22.frame.add(app,BorderLayout.CENTER);23.frame.setSize(250,150);24.25./Register the AppletApp class as the 26./listener for a Window Destroy event 27.frame.addWindowListener(new WindowAdapter()28.public void windowClosing(WindowEvent e)29.System.exit(0);30.31.);32.33./Call the appl
49、et methods 1.app.init();2.app.start();3.frame.setVisible(true);/Invokes paint()4.132 5.6.public void init()7.date=new Date();8.9.10.public void paint(Graphics g)11.g.drawString(This Java program started at,25,25);12.g.drawString(date.toString(),25,60);13.14.?getImage()?getAudioClip()?Applet?Applet?paint()?Applet?1?Applet 1.?Command Tool?2.?HwMouse.java?course_example?3.?Applet?4.?HwMouse.java.java?c:student HwMouse.java 5.?HwMouse.html?HwMouse.class?6.?appletviewer?Applet?c:student HwMouse.html?2?1.?Applet?Squares.java?2.?java.awt.Color?setColor?Java applet?import java.awt.Color;public void p
限制150内