您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > java_SUN_JAVA 培训专用教材c11-15
JavaJDK1.2JavaSwingSwingAWT(100%Java)JFCSwingAWTJavaJFCGUIJFCSwingSwingAWT104Javacom.sun.java.swingSwingSwingGUISwingJframeJappletTheJavaTutorialSunMicrosystems“J”GUIGUISwingGUISwingSwingpanepaneJAppletJButtonJComboBoxJOptionPane107JListSwingJLabelJScrollPaneJTableJScrollBarJSliderJTooltipJTreeSwingHelloSwing11.5.1HelloSwing1081.importjava.awt.*;2.importjava.awt.event.*;3.importcom.sun.java.swing.*;4.importjava.awt.accessibility.*;5.6.publicclassHelloSwingimplementsActionListener{7.privateJFramejFrame;8.privateJLabeljLabel;9.privateJPaneljPanel;10.privateJButtonjButton;11.privateAccessibleContextaccContext;12.13.privateStringlabelPrefix=14.Numberofbuttonclicks:;15.privateintnumClicks=0;16.17.publicvoidgo(){18.19.//Hereishowyoucansetupaparticular20.//lookAndFeel.Notnecessaryfordefault.21.//22.//try{23.//UIManager.setLookAndFeel(24.//UIManager.getLookAndFeel());25.//}catch(UnsupportedLookAndFeelExceptione){26.//System.err.println(Couldn'tusethe+27.//defaultlookandfeel+e);28.//}29.30.jFrame=newJFrame(HelloSwing);31.jLabel=newJLabel(labelPrefix+0);32.33.jButton=newJButton(IamaSwingbutton!);34.35.//Createashortcut:makeALT-Abeequivalent36.//topressingmouseoverbutton.37.jButton.setMnemonic('i');38.39.jButton.addActionListener(this);40.HelloSwing1.//Addsupportforaccessibility.2.accContext=jButton.getAccessibleContext();3.accContext.setAccessibleDescription(4.Pressingthisbuttonincrements+5.thenumberofbuttonclicks);1096.7.//Setuppane.8.//Giveitaborderaroundtheedges.9.jPanel=newJPanel();10.jPanel.setBorder(11.BorderFactory.createEmptyBorder(12.30,30,10,30));13.14.//Arrangeforcomptstobeinasinglecolumn.15.jPanel.setLayout(newGridLayout(0,1));16.17.//Putcomptsinpane,notinJFramedirectly.18.jPanel.add(jButton);19.jPanel.add(jLabel);20.jFrame.setContentPane(jPanel);21.22.//SetupaWindowListenerinnerclasstohandle23.//window'squitbutton.24.WindowListenerwl=newWindowAdapter(){25.publicvoidwindowClosing(WindowEvente){26.System.exit(0);27.}28.};29.jFrame.addWindowListener(wl);30.31.jFrame.pack();32.jFrame.setVisible(true);33.}34.HelloSwing1.//Buttonhandling.2.publicvoidactionPerformed(ActionEvente){3.numClicks++;4.jLabel.setText(labelPrefix+numClicks);5.}6.7.publicstaticvoidmain(String[]args){8.9.HelloSwinghelloSwing=newHelloSwing();10.helloSwing.go();11.}12.}11.4.2Swing110SwinggetLookAndFeel()JFrameFrameJFramecontentpaneFrameimportcom.sun.java.swing.*SwingSwingHelloSwing2228getLookAndFeel()WindowsSolarisCDE/Motif11.4.3SwingJFrameJFrameAWTFrameSwingHelloSwingJFramepublicHelloSwing(){JFramejFrame;JPaneljPanel;……jFrame=newJFrame(HelloSwing);jPanel=newJPanel();……jFrame.setContentPane(jPanel);FrameJFrameJFramecontentpanecontentpanecontentpaneJFramecontentpanegetContentPane()contentpanesetContentPane()11.4.4SwingSwingSwingHelloSwing.java4Swing4JFrameJButtonJLabelJPanelHelloSwing.javaaccContext=jButton.getAccessibleContext();accContext.setAccessibleDescription(Pressingthisbuttonincrements+thenumberofbuttonclicks.);111HelloSwing4JFrameJButtonJLabelJPanelHelloSwing3345JButton3337ACTI3941454959JPanelJPanelpanelPanelHelloSwingPanel11.4.5HelloSwing.javaaccContext=jButton.getAccessibleContext();accContext.setAccessibleDescription(Pressingthisbuttonincrements+thenumberofbuttonclicks.);jButton=newJButton(I'maSwingbutton!);jLabel=newJLabel(labelPrefix+0);jLabel.setText(labelPrefix+numClicks);JFrameJButtonJLabelAccessibilitySwingGUISwingGUIJFrameJAppletJDialogJWindowJButtonJPanelJMenuSwingcontentpaneSwingJFrameJAppletJWindowJDialogJ…JButtonJPanelJMenuSwingAWTSwingSwingAWTSwingSwingSwingAppletJAppletjava.applet.AppletSwingJFrameSwingSwingSwingSwingcontentpanecontentpaneSwingJPanelSwingGUI2Jframe(atop-levelSwingcontainer)112……?contentpane?`+++??JButtonJButtonJPanelJTextField++??JListGUIGUIApplet…?…?JApplet(atop-levelSwingcontainer)?contentpane?`+++???JButtonJButtonJPanel?++??JTextFieldJListGUI1.importcom.sun.java.swing.*;2.importjava.awt.*;3.4.publicclassSwingGUI{5.6.JFrametopLevel;7.JPaneljPanel;8.JTextFieldjTextField;9.JListjList;10.11.JButtonb1;12.JButtonb2;13.ContainercontentPane;14.15.ObjectlistData[]={16.newString(Firstselection),17.newString(Secondselection),18.newString(Thirdselection)19.};20.21.publicstaticvoidmain(Stringargs[]){22.SwingGUIswingGUI=newSwingGUI();11323.swingGUI.go();24.}25.26.publicvoidgo(){27.topLevel=newJFrame(SwingGUI);28.29.//SetuptheJPanel,whichcontainsthetextfield30.//andlist.31.jPanel=newJPanel();32.jTextField=newJTextField(20);33.jList=newJList(listData);34.35.contentPane=topLevel.getContentPane();36.contentPane.setL
本文标题:java_SUN_JAVA 培训专用教材c11-15
链接地址:https://www.777doc.com/doc-958888 .html