Java17第十四章2-界面設(shè)計.ppt
《Java17第十四章2-界面設(shè)計.ppt》由會員分享,可在線閱讀,更多相關(guān)《Java17第十四章2-界面設(shè)計.ppt(48頁珍藏版)》請在裝配圖網(wǎng)上搜索。
第十四章2 圖形用戶界面設(shè)計 回顧與作業(yè)點評 事件處理機(jī)制布局設(shè)計 本章任務(wù) 掌握Swing容器和組件 知識要點 Swing容器和組件 14 1抽象窗口工具集 AWT 用于設(shè)計與用戶交互的圖形界面14 1 1AWT組件和容器 AWT AbstractWindowingToolkit 中文譯為抽象窗口工具包 是Java提供的用來建立和設(shè)置Java的圖形用戶界面的基本工具 AWT由Java中的java awt包提供 里面包含了許多可用來建立與平臺無關(guān)的圖形用戶界面 GUI 的類 這些類又被稱為組件 components 1 Component和Containercomponent是一個具有圖形表示能力的對象 可在屏幕上顯示 并可與用戶進(jìn)行交互 典型圖形用戶界面中的按鈕 復(fù)選框和滾動條都是組件示例 Container 一般的AWT容器對象 是一個可包含其他AWT組件的組件 14 2Swingj介紹Swing是基于AWT的一組GUI組件的集合 其組件與操作系統(tǒng)無關(guān) 引入了許多新的特性和能力 1 JFrame類2 基本圖形組件類JPanel JButton等14 3Swing組件應(yīng)用1 JOptionPane組件 是標(biāo)準(zhǔn)對話框 Swing是在AWT基礎(chǔ)上開發(fā)出的圖形界面包 14 3 2菜單組件JMenuBar 菜單欄JMenu 菜單JMenuItem 菜單項JChecBoxMenuItem 帶復(fù)選框的菜單項組件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 我的第一個小板凳 設(shè)置不允許調(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 設(shè)置背景色add button add rgb add red 添加到面板上add green add blue publicvoidactionPerformed ActionEvente color JColorChooser showDialog this ChooseColor color setBackground color 從新設(shè)置背景button setText Getagain 重新設(shè)置button顯示字rgb setText RGB color getRGB 設(shè)置得到RGBred setText Red color getRed 設(shè)置得到的紅色green setText Green color getGreen blue setText Blue color getBlue 3 界面基本組件JLabelJTextFieldJPasswordFieldJTextieldJTextAreaJButtonJCheckBoxJRadioButtonJComboxJProgresBarSwingReigester java4 JProgresBar進(jìn)度條 importjava awt BorderLayout importjava awt event ActionEvent importjava awt event ActionListener importjavax swing classBarThreadextendsThread privateintDELAY 100 privateJProgressBarprogressBar 進(jìn)度條類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 事件如果達(dá)到最大值 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 學(xué)歷JLabelediLbl newJLabel 學(xué)歷 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 復(fù)選框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中 文本域默認(rèn)是不帶滾動條的 必須放置在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 標(biāo)題信息參數(shù)4 指定按鈕類型參數(shù)5 指定圖標(biāo)類型返回值 戶所選選項的整數(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)造器設(shè)置窗口標(biāo)題欄DrawPaneldrawPanel newDrawPanel 創(chuàng)建DrawPanel對象用于繪制圖形Containercontent getContentPane 獲得窗口的內(nèi)容窗格content add drawPanel BorderLayout CENTER 把對象drawPanel加入內(nèi)容窗格setSize 400 300 設(shè)置窗口大小setVisible true 設(shè)置窗口可視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坐標(biāo)intposY 2 各圖形位置的y坐標(biāo)intstrY gridH 7 字符串繪制位置的y坐標(biāo)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 坐標(biāo)平移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- 1.請仔細(xì)閱讀文檔,確保文檔完整性,對于不預(yù)覽、不比對內(nèi)容而直接下載帶來的問題本站不予受理。
- 2.下載的文檔,不會出現(xiàn)我們的網(wǎng)址水印。
- 3、該文檔所得收入(下載+內(nèi)容+預(yù)覽)歸上傳者、原創(chuàng)作者;如果您是本文檔原作者,請點此認(rèn)領(lǐng)!既往收益都?xì)w您。
下載文檔到電腦,查找使用更方便
9.9 積分
下載 |
- 配套講稿:
如PPT文件的首頁顯示word圖標(biāo),表示該PPT已包含配套word講稿。雙擊word圖標(biāo)可打開word文檔。
- 特殊限制:
部分文檔作品中含有的國旗、國徽等圖片,僅作為作品整體效果示例展示,禁止商用。設(shè)計者僅對作品中獨創(chuàng)性部分享有著作權(quán)。
- 關(guān) 鍵 詞:
- Java17 第十四 界面設(shè)計
鏈接地址:http://zhongcaozhi.com.cn/p-6358013.html