Java17第十四章2-界面設計swing.ppt
-
資源ID:6358020
資源大?。?span id="fy8rik0" class="font-tahoma">7.46MB
全文頁數(shù):49頁
- 資源格式: PPT
下載積分:9.9積分
快捷下載
會員登錄下載
微信登錄下載
微信掃一掃登錄
友情提示
2、PDF文件下載后,可能會被瀏覽器默認打開,此種情況可以點擊瀏覽器菜單,保存網(wǎng)頁到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無水印,預覽文檔經(jīng)過壓縮,下載后原文更清晰。
5、試題試卷類文檔,如果標題沒有明確說明有答案則都視為沒有答案,請知曉。
|
Java17第十四章2-界面設計swing.ppt
第十四章2 圖形用戶界面設計 回顧與作業(yè)點評 事件處理機制布局設計 本章任務 掌握Swing容器和組件 知識要點 Swing容器和組件 14 1抽象窗口工具集 AWT 用于設計與用戶交互的圖形界面14 1 1AWT組件和容器 AWT AbstractWindowingToolkit 中文譯為抽象窗口工具包 是Java提供的用來建立和設置Java的圖形用戶界面的基本工具 AWT由Java中的java awt包提供 里面包含了許多可用來建立與平臺無關(guān)的圖形用戶界面 GUI 的類 這些類又被稱為組件 components 1 Component和Containercomponent是一個具有圖形表示能力的對象 可在屏幕上顯示 并可與用戶進行交互 典型圖形用戶界面中的按鈕 復選框和滾動條都是組件示例 Container 一般的AWT容器對象 是一個可包含其他AWT組件的組件 14 2Swingj介紹Swing是基于AWT的一組GUI組件的集合 其組件與操作系統(tǒng)無關(guān) 引入了許多新的特性和能力 1 JFrame類2 基本圖形組件類JPanel JButton等14 3Swing組件應用1 JOptionPane組件 是標準對話框 Swing是在AWT基礎上開發(fā)出的圖形界面包 14 3 2菜單組件JMenuBar 菜單欄JMenu 菜單JMenuItem 菜單項JChecBoxMenuItem 帶復選框的菜單項組件MyNotedpad javaJPopupMenu 彈出式菜單組件 importjava awt Color importjavax swing importjava awt event MouseAdapter importjava awt event MouseEvent publicclassPopupMenuTest staticJPopupMenupopup null publicstaticvoidmain String args JFrameframe newJFrame frame setBounds 200 300 200 300 frame setTitle 我的第一個小板凳 設置不允許調(diào)整窗口的大小frame setResizable false frame setBackground Color ORANGE popup newJPopupMenu popup add 徐明華 popup add 張燕君 popup add 徐詩佳 popup add 王子昊 popup setName 右擊菜單 frame add popup 在窗體上加右擊菜單frame addMouseListener newMouseAdapter publicvoidmouseReleased MouseEvente if e getButton MouseEvent BUTTON3 popup show e getComponent e getX e getY 添加事件監(jiān)聽器frame setVisible true 顯示窗體 2 JColorChooser 顏色選擇器 用于允許用戶操作和選擇顏色的控制器窗格 importjava awt importjava awt event importjavax swing publicclassJColorChooserDemoimplementsActionListener publicstaticvoidmain String args JFrameframe newJFrame JColorChooserDemo frame setDefaultCloseOperation JFrame EXIT ON CLOSE MyPanelpanel newMyPanel Panel面板對象frame getContentPane add panel frame pack 自動調(diào)整大小frame show 窗體顯示 publicvoidactionPerformed ActionEvente classMyPanelextendsJPanelimplementsActionListener privateJButtonbutton rgb red green blue ButtonprivateColorcolor newColor 0 0 0 顏色對象publicMyPanel button newJButton GetColor rgb newJButton RGB red newJButton Red green newJButton Green blue newJButton Blue 面板上有五個按鈕button addActionListener this 添加事件setPreferredSize newDimension 550 250 setLayout newFlowLayout FlowLayout CENTER 5 5 setBackground color 設置背景色add button add rgb add red 添加到面板上add green add blue publicvoidactionPerformed ActionEvente color JColorChooser showDialog this ChooseColor color setBackground color 從新設置背景button setText Getagain 重新設置button顯示字rgb setText RGB color getRGB 設置得到RGBred setText Red color getRed 設置得到的紅色green setText Green color getGreen blue setText Blue color getBlue 3 界面基本組件JLabelJTextFieldJPasswordFieldJTextieldJTextAreaJButtonJCheckBoxJRadioButtonJComboxJProgresBarSwingReigester java4 JProgresBar進度條 importjava awt BorderLayout importjava awt event ActionEvent importjava awt event ActionListener importjavax swing classBarThreadextendsThread privateintDELAY 100 privateJProgressBarprogressBar 進度條類privateJButtonbutton privatebooleanflag true publicBarThread JProgressBarbar JButtonbutton progressBar bar this button button publicvoidrun 線程體button setEnabled false intmaximum progressBar getMaximum 最大值while flag try Thread sleep DELAY 休眠100 catch InterruptedExceptionignoredException progressBar setValue progressBar getValue 1 if progressBar getValue maximum 如果拿到值 最大值flag false button setEnabled true publicclassJProgressBarTest publicstaticvoidmain Stringargs finalJProgressBaraJProgressBar newJProgressBar 0 100 aJProgressBar setStringPainted true finalJButtonaJButton newJButton Start 按鈕aJButton addActionListener newActionListener publicvoidactionPerformed ActionEvente if aJProgressBar getValue aJProgressBar getMaximum aJProgressBar setValue 0 事件如果達到最大值 Threadstepper newBarThread aJProgressBar aJButton stepper start JFrameframe newJFrame ProgressBars frame setDefaultCloseOperation JFrame EXIT ON CLOSE frame add aJProgressBar BorderLayout NORTH frame add aJButton BorderLayout SOUTH frame setSize 300 100 frame setVisible true 5JToolBar工具條 importjava awt BorderLayout URL importjavax swing publicclassJToolBarTest publicstaticvoidmain String args JToolBarbar newJToolBar JButtonbutton1 button2 button3 URLurl Thread currentThread getContextClassLoader getResource left jpg URLurl2 Thread currentThread getContextClassLoader getResource center jpg URLurl3 Thread currentThread getContextClassLoader getResource right jpg button1 newJButton newImageIcon url button1 setToolTipText 向前 button2 newJButton newImageIcon url2 button2 setToolTipText 向上 button3 newJButton newImageIcon url3 button3 setToolTipText 向后 bar add button1 bar add button2 bar add button3 bar setFloatable true JFrameframe newJFrame frame setTitle JToolBar工具條 frame add bar BorderLayout NORTH frame add newJTextArea BorderLayout CENTER frame setSize 400 400 frame setDefaultCloseOperation JFrame EXIT ON CLOSE frame setVisible true publicclassSwingReigsterextendsJFrame publicSwingReigster this init publicvoidinit this setTitle 用戶注冊 this setBounds 100 100 340 500 this createUI 改變感觀 try UIManager setLookAndFeel newWindowsClassicLookAndFeel catch UnsupportedLookAndFeelExceptione TODOAuto generatedcatchblock e printStackTrace this setVisible true this setDefaultCloseOperation EXIT ON CLOSE publicvoidcreateUI JPanelpanel newJPanel 添加邊框Borderborder BorderFactory createEtchedBorder EtchedBorder LOWERED TitledBordertBorder BorderFactory createTitledBorder border 注冊面板 TitledBorder CENTER TitledBorder TOP panel setBorder tBorder panel setLayout null this add panel 名JLabelnameLbl newJLabel 輸入姓名 nameLbl setBounds 10 50 60 25 panel add nameLbl JTextFieldnameTxt newJTextField nameTxt setBounds 80 50 120 22 panel add nameTxt 密碼JLabelpwdLbl newJLabel 輸入密碼 pwdLbl setBounds 10 80 60 25 panel add pwdLbl JPasswordFieldpwd newJPasswordField pwd setBounds 80 80 120 22 panel add pwd 性別JLabelgenderLbl newJLabel 性別 genderLbl setBounds 10 110 60 25 panel add genderLbl ButtonGroupgroup newButtonGroup JRadioButtonfRdo newJRadioButton 男 true fRdo setBounds 80 110 50 25 group add fRdo JRadioButtonmRdo newJRadioButton 女 false mRdo setBounds 140 110 50 25 group add mRdo panel add fRdo panel add mRdo 學歷JLabelediLbl newJLabel 學歷 ediLbl setBounds 10 140 60 25 panel add ediLbl 下拉列表 JComboBoxedicbo newJComboBox edicbo addItem 高中 edicbo addItem 大專 edicbo addItem 本科 edicbo addItem 其它 edicbo setBounds 80 140 80 22 panel add edicbo 愛好JLabelhobbyLbl newJLabel 愛好 hobbyLbl setBounds 10 170 60 25 panel add hobbyLbl 復選框JCheckBoxhobbyChk newJCheckBox 睡覺 hobbyChk setBounds 80 170 60 25 panel add hobbyChk JCheckBoxhobbyChk2 newJCheckBox 運動 true hobbyChk2 setBounds 150 170 60 25 panel add hobbyChk2 JCheckBoxhobbyChk3 newJCheckBox 爬山 hobbyChk3 setBounds 220 170 60 25 panel add hobbyChk3 自我介紹 JLabelintrLbl newJLabel 自我介紹 intrLbl setBounds 10 200 60 25 panel add intrLbl Swing中 文本域默認是不帶滾動條的 必須放置在JScrollPane中 才會滾動條JTextAreaintrArea newJTextArea JScrollPanescrollPane newJScrollPane intrArea JScrollPane VERTICAL SCROLLBAR AS NEEDED JScrollPane HORIZONTAL SCROLLBAR AS NEEDED scrollPane setBounds 20 230 240 110 panel add scrollPane 放置圖片JLabelimgLbl newJLabel imgLbl setBounds 5 350 324 47 panel add imgLbl URLurl Thread currentThread getContextClassLoader getResource logo2 png URLurl this getClass getResource 517 jpg Iconicon newImageIcon url imgLbl setIcon icon 按鈕JButtonbtn newJButton 提交 btn setBounds 50 420 70 22 btn addActionListener newActionListener publicvoidactionPerformed ActionEventarg0 參數(shù)1 父組件參數(shù)2 提示信息參數(shù)3 標題信息參數(shù)4 指定按鈕類型參數(shù)5 指定圖標類型返回值 戶所選選項的整數(shù) intoption JOptionPane showConfirmDialog null 你確定提交嗎 提交提示 JOptionPane OK CANCEL OPTION JOptionPane WARNING MESSAGE if option JOptionPane OK OPTION System out println ok submit else System out println no cancel panel add btn JButtonbtn2 newJButton 重置 btn2 setBounds 150 420 70 22 panel add btn2 btn2 addActionListener newActionListener publicvoidactionPerformed ActionEventarg0 JFileChooserchooser newJFileChooser FileNameExtensionFilterfilter newFileNameExtensionFilter JPG 2D圖形的繪制 importjava awt BorderLayout importjava awt Color importjava awt Container importjava awt Graphics importjava awt Graphics2D importjava awt Shape importjava awt geom AffineTransform importjava awt geom Arc2D importjava awt geom CubicCurve2D importjava awt geom Ellipse2D importjava awt geom Line2D importjava awt geom QuadCurve2D importjava awt geom Rectangle2D importjava awt geom RoundRectangle2D importjavax swing JFrame importjavax swing JPanel publicclassGraphicsShape2DDemoextendsJFrame 主窗口類GraphicsShape2DDemo super 常用圖形的繪制與填充 調(diào)用父類構(gòu)造器設置窗口標題欄DrawPaneldrawPanel newDrawPanel 創(chuàng)建DrawPanel對象用于繪制圖形Containercontent getContentPane 獲得窗口的內(nèi)容窗格content add drawPanel BorderLayout CENTER 把對象drawPanel加入內(nèi)容窗格setSize 400 300 設置窗口大小setVisible true 設置窗口可視setDefaultCloseOperation JFrame EXIT ON CLOSE 關(guān)閉窗口時退出程序 publicstaticvoidmain String args newGraphicsShape2DDemo 創(chuàng)建GraphicsShapeDemo對象 顯示圖形的面板classDrawPanelextendsJPanel 重載paintComponent 方法publicvoidpaintComponent Graphicsg super paintComponent g 調(diào)用父類的繪制組件方法Graphics2Dg2D Graphics2D g setBackground Color white setForeground Color black intcharH 16 最大字符高度intgridW getWidth 5 intgridH getHeight 4 繪圖網(wǎng)格高度intposX 2 各圖形繪制位置的x坐標intposY 2 各圖形位置的y坐標intstrY gridH 7 字符串繪制位置的y坐標intw gridW 2 posX 圖形的寬度inth strY charH posY 圖形的高度intcirlceD Math min w h 圓的直徑Shape shape newShape 2 5 shape 0 0 newLine2D Float 0 0 w h 直線shape 0 1 newRectangle2D Double 0 0 w h 矩形shape 0 2 newRoundRectangle2D Float 0 0 w h 20 20 圓角矩形shape 0 3 newEllipse2D Float 0 0 cirlceD cirlceD 圓shape 0 4 newEllipse2D Float 0 0 w h 橢圓shape 1 0 newArc2D Float 0 0 w h 45 225 Arc2D OPEN 開弧shape 1 1 newArc2D Float 0 0 w h 45 225 Arc2D CHORD 弓形shape 1 2 newArc2D Float 0 0 w h 45 225 Arc2D PIE 餅形shape 1 3 newQuadCurve2D Double 0 0 w h 6 w h 二次曲線shape 1 4 newCubicCurve2D Double 0 0 w 2 h w h 2 w h 三次曲線 繪制幾何圖形的名稱String shapeName 直線 矩形 圓角矩形 圓 橢圓 開弧 弓形 餅形 二次曲線 三次曲線 AffineTransformdefaultAT g2D getTransform for inti 0 i shapeName length i for intj 0 j shape i length j g2D setColor Color black g2D translate posX posY 坐標平移g2D draw shape i j g2D setColor Color blue g2D drawString shapeName i j 2 strY 繪制說明文字g2D setTransform defaultAT posY gridH g2D translate posX posY g2D setColor Color pink g2D fill shape i j g2D setColor Color blue g2D drawString 填充 shapeName i j 2 strY 文字g2D setTransform defaultAT posX gridW posY gridH posX 2 posY 2 gridH