九九热最新网址,777奇米四色米奇影院在线播放,国产精品18久久久久久久久久,中文有码视频,亚洲一区在线免费观看,国产91精品在线,婷婷丁香六月天

歡迎來到裝配圖網(wǎng)! | 幫助中心 裝配圖網(wǎng)zhuangpeitu.com!
裝配圖網(wǎng)
ImageVerifierCode 換一換
首頁 裝配圖網(wǎng) > 資源分類 > PPT文檔下載  

Java圖形用戶界面第7章.ppt

  • 資源ID:6359914       資源大?。?span id="24d9guoke414" class="font-tahoma">436.55KB        全文頁數(shù):88頁
  • 資源格式: PPT        下載積分:14.9積分
快捷下載 游客一鍵下載
會員登錄下載
微信登錄下載
三方登錄下載: 微信開放平臺登錄 支付寶登錄   QQ登錄   微博登錄  
二維碼
微信掃一掃登錄
下載資源需要14.9積分
郵箱/手機(jī):
溫馨提示:
用戶名和密碼都是您填寫的郵箱或者手機(jī)號,方便查詢和重復(fù)下載(系統(tǒng)自動生成)
支付方式: 支付寶    微信支付   
驗(yàn)證碼:   換一換

 
賬號:
密碼:
驗(yàn)證碼:   換一換
  忘記密碼?
    
友情提示
2、PDF文件下載后,可能會被瀏覽器默認(rèn)打開,此種情況可以點(diǎn)擊瀏覽器菜單,保存網(wǎng)頁到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無水印,預(yù)覽文檔經(jīng)過壓縮,下載后原文更清晰。
5、試題試卷類文檔,如果標(biāo)題沒有明確說明有答案則都視為沒有答案,請知曉。

Java圖形用戶界面第7章.ppt

第7章 圖形用戶界面程序設(shè)計(jì) 本章主要內(nèi)容 圖形用戶界面 GUI Java的基本繪圖Swing組件設(shè)計(jì)布局管理器 一 圖形用戶界面 GUI GUI是用圖形的方式 借助于菜單 按鈕等標(biāo)準(zhǔn)界面元素和鼠標(biāo)操作 幫助用戶方便地向計(jì)算機(jī)系統(tǒng)發(fā)出指令 啟動操作 并將系統(tǒng)運(yùn)行的結(jié)果也以同樣的方式顯示給用戶的技術(shù) AWT AWT AbstractWindowToolkit 譯為抽象窗口工具包 它是Java語言為建立圖形用戶界面提供的API 其主要功能包括 用戶界面組件事件處理模型圖形和圖像工具布局管理器 Swing Java最初作為網(wǎng)絡(luò)編程工具 提供的AWT功能比較弱 只能用來支持必要的用戶界面組件 隨著Java被越來越多的業(yè)內(nèi)人士認(rèn)可 應(yīng)用范圍的迅猛擴(kuò)展 Sun公司意識到需要對AWT的設(shè)計(jì)進(jìn)行修改 以適應(yīng)于更加廣泛的開發(fā)目的 于是就誕生了Swing Swing是繼AWT之后 更加豐富 功能更加強(qiáng)大的GUI工具包 它構(gòu)成了JFC JavaFoundationClass 的圖形用戶界面功能的核心部分 Swing與AWT的主要區(qū)別 AWT是基于同位體 Peer 的體系結(jié)構(gòu) 這種設(shè)計(jì)策略嚴(yán)重限制了用戶界面中可以使用的組件種類及功能 成為一個致命的缺憾 而Swing不需要本地提供同位體 這樣可以給設(shè)計(jì)者帶來更大的靈活性 有利于增強(qiáng)組件的功能 在AWT中 有一部分代碼是用C編寫的 而Swing是100 的純Java 增強(qiáng)了應(yīng)用程序的與環(huán)境無關(guān)性 Swing與AWT的主要區(qū)別 Swing具有控制外觀 Pluggablelookandfeel 的能力 即允許用戶自行定制桌面的顯示風(fēng)格 比如 更換配色方案 讓窗口系統(tǒng)更加適應(yīng)用戶的習(xí)慣和需要 而AWT組件完全依賴于本地平臺 增加了裁剪板 鼠標(biāo)提示 拖放和打印等功能 與圖形用戶界面有關(guān)的包 java awt與繪圖相關(guān)的類java event與事件處理相關(guān)的類javax swing與Swing組件相關(guān)的類 二 Java的基本繪圖 繪圖類Graphics顏色類Color字體類Font 1 繪圖類Graphics Java的圖形處理主要由AWT中的Graphics類實(shí)現(xiàn) 該類支持兩種類型的繪圖功能 利用繪圖成員方法繪圖和顯示圖象 Graphics是一個抽象類 在這個類中封裝了所有的繪圖操作 Graphics類中的繪圖成員方法 voiddrawLine intstartX intstartY intendX intendY voiddrawRect intx inty intwidth intheight voidfillRect intx inty intwidth intheight voiddrawRoundRect intx inty intwidth intheight intarcWidth intarcHeight voidfillRoundRect intx inty intwidth intheight intarcWidth intarcHeight voiddraw3DRect intx inty intwidth intheight booleanraised voidfill3DRect intx inty intwidth intheight booleanraised voiddrawOval intx inty intwidth intheight voidfillOval intx inty intwidth intheight Graphics類中的繪圖成員方法 voiddrawArc intx inty intwidth intheight intstartAngle intendAngle voidfillArc intx inty intwidth intheight intstartAngle intendAngle voiddrawString Stringstr intx inty voiddrawPloygon int xPoints int yPoints intnumPoints voidfillPloygon int xPoints int yPoints intnumPoints 利用Graphics類提供的成員方法繪圖的基本步驟 創(chuàng)建繪圖類對象調(diào)用Graphics提供的成員方法繪圖 創(chuàng)建繪圖類對象的方法 在Swing組件中 通過paintComponent 成員方法的參數(shù)獲得繪圖類對象利用組件的getGraphics 成員方法顯式地創(chuàng)建繪圖對象 并用dispose 釋放 importjava awt importjavax swing publicclassGraphicsDemoextendsJPanel publicstaticvoidmain String agrs JFramefrm newJFrame 繪圖應(yīng)用范例 frm getContentPane add newGraphicsDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 380 200 frm setVisible true publicvoidpaintComponent Graphicsg 刷新面板時 系統(tǒng)自動調(diào)用這個成員方法super paintComponent g g drawString 繪圖對象 5 40 g drawLine 5 50 150 50 g drawRect 5 55 50 50 g fillRect 5 110 50 50 g drawRoundRect 60 55 50 50 30 30 g fillRoundRect 60 110 50 50 30 30 g drawOval 115 55 50 50 g fillOval 115 110 50 50 g drawArc 100 5 100 100 0 45 g fillArc 155 5 100 100 0 45 intx vals 170 170 210 250 210 inty vals 60 100 100 80 60 g drawPolygon x vals y vals x vals length for inti 0 i 5 i y vals i 55 g fillPolygon x vals y vals x vals length g fill3DRect 260 5 100 70 true g fill3DRect 260 90 100 70 false 2 2D繪圖方式 利用Graphics類中提供的方法實(shí)現(xiàn)繪圖 其功能十分有限 從JDK1 2版本以后 Java提供了一種2D庫 它體現(xiàn)了面向?qū)ο笤O(shè)計(jì)方法的思想 其中 包含了強(qiáng)大的繪圖功能 Graphics與Graphics2D的主要區(qū)別 Graphics類以成員方法的形式封裝了繪制各種圖形的工具 而Graphics2D將繪制的所有圖形用相應(yīng)的類對象標(biāo)識 這是它們兩個的主要區(qū)別 與Graphics相比 Graphics2D提供的幾何圖形控制 坐標(biāo)轉(zhuǎn)換 顏色管理和文本布局的功能更加強(qiáng)大 使用起來更加靈活 Graphics2D類 利用Java2D繪制圖形時所需要的繪圖工具都包含在Graphics2D類對象中 通常將此稱為圖形設(shè)備文本 graphicscontext 無論希望在組件表面繪制什么圖形 都要擁有一個圖形設(shè)備文本對象 通過它實(shí)現(xiàn)繪制直線 曲線 各種幾何圖形 填充幾何圖形等一系列繪圖操作 獲取Grphics2D對象的方式 Graphics2D是Graphics的子類 它們都是抽象類 因此不能夠直接創(chuàng)建這兩個類的對象 它們的創(chuàng)建過程完全由相應(yīng)的組件控制 在Swing組件中 通過paintComponent 成員方法的參數(shù)獲得繪圖類對象 Graphics2D類中提供的成員方法 draw Shapeshape 使用圖形設(shè)備文本的當(dāng)前屬性繪制由shape帶入的圖形 fill Shapeshape 使用圖形設(shè)備文本的當(dāng)前屬性填充由shape帶入的圖形 drawString Stringtext 使用圖形設(shè)備文本的當(dāng)前屬性繪制文本 drawImage 使用圖形設(shè)備文本的當(dāng)前屬性顯示圖象 在java awt geom包中定義了一系列實(shí)現(xiàn)Shape接口的幾何圖形類 Line2D這是一個抽象類 定義了由兩個端點(diǎn)確定的直線 在這個類中包含了兩個內(nèi)部類 Line2D Float和Line2D Double 它們分別實(shí)現(xiàn)用float和double類型的用戶坐標(biāo)定義直線端點(diǎn)的功能 Ellipse2D這是一個定義橢圓的抽象類 其中包含了兩個內(nèi)部類 Ellipse2D Float和Ellipse2D Double 它們分別實(shí)現(xiàn)用float和double類型的數(shù)值描述用戶坐標(biāo)點(diǎn)的功能 橢圓由左上角位置和封閉它的矩形寬度和高度確定 Graphics2D應(yīng)用舉例 importjava awt importjava awt geom importjavax swing publicclassDrawTest publicstaticvoidmain String args DrawFrameframe newDrawFrame frame setDefaultCloseOperation JFrame EXIT ON CLOSE frame show importjava awt importjava awt geom importjavax swing publicclassDrawFrameextendsJFrame publicDrawFrame setTitle DrawTest setSize DEFAULT WIDTH DEFAULT HEIGHT DrawPanelpanel newDrawPanel ContainercontentPane getContentPane contentPane add panel publicstaticfinalintDEFAULT WIDTH 400 publicstaticfinalintDEFAULT HEIGHT 400 importjava awt importjava awt geom importjavax swing publicclassDrawPanelextendsJPanel publicvoidpaintComponent Graphicsg Graphics2Dg2 Graphics2D g Rectangle2Drect newRectangle2D Double 100 100 200 150 g2 draw rect Ellipse2Dellipse newEllipse2D Double ellipse setFrame rect g2 draw ellipse g2 draw newLine2D Double 100 100 300 250 doublecenterX rect getCenterX doublecenterY rect getCenterY doubleradius 150 Ellipse2Dcircle newEllipse2D Double circle setFrameFromCenter centerX centerY centerX radius centerY radius g2 draw circle 3 顏色類 Java主要使用RGB模式描述顏色特性 R G B各用一個字節(jié)表示 取值范圍為0 255 類庫提供Color類描述顏色 Color類提供的靜態(tài)常量Color red Color類提供的成員方法 Color red green blue publicColorbrighter publicColordarker publicintgetRed publicintgetGreen publicintgetBlue 設(shè)置顏色的成員方法 在Component類中 提供了設(shè)置組件前景顏色和背景顏色的成員方法 setForeground Colorcolor setBackground Colorcolor 在組件中繪圖時 設(shè)置 獲取繪圖顏色的成員方法 setColor Colorcolor getColor Colorcolor importjavax swing importjava awt geom publicclassLineArtTest publicstaticvoidmain String args JFramefrm newJFrame LineArtDemo frm getContentPane add newLineArtDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 300 200 frm setVisible true 設(shè)置繪圖顏色 publicclassLineArtDemoextendsJPanel privateintmNumberOfLines privateColormColor publicLineArtDemo this publicLineArtDemo Stringstr super mNumberOfLines 25 mColor newColor 3 mColor 0 newColor 255 0 0 mColor 1 newColor 0 255 0 mColor 2 newColor 0 0 255 publicvoidpaintComponent Graphicsg Dimensiond getSize super paintComponent g Graphics2Dg2 Graphics2D g for inti 0 i mNumberOfLines i doubleratio double i double mNumberOfLines g2 setColor mColor i mColor length g2 draw newLine2D Double 0 int ratio d height int ratio d width d height importjava awt importjava awt event importjavax swing importjava awt geom publicclassColorDemoextendsJPanel JPanelpanel JButtonbutton1 JButtonbutton2 Colorcolor publicColorDemo color Color orange setLayout newBorderLayout setPreferredSize newDimension 300 200 button1 newJButton 增量 button1 addActionListener newActionListener publicvoidactionPerformed ActionEvente color color brighter repaint button2 setEnabled true button1 setEnabled false 改變顏色明暗 button1 setEnabled false button2 newJButton 減暗 button2 addActionListener newActionListener publicvoidactionPerformed ActionEvente color color darker repaint button1 setEnabled true button2 setEnabled false panel newJPanel panel setBackground Color darkGray panel add button1 panel add button2 add panel BorderLayout SOUTH publicvoidpaintComponent Graphicsg super paintComponent g g setColor color g fillRect 10 10 this getWidth 20 this getHeight panel getHeight 20 publicstaticvoidmain Stringagrs JFramefrm newJFrame 顏色應(yīng)用范例 frm getContentPane add newColorDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 300 200 frm setVisible true 4 字體類 在AWT中有兩個與字體相關(guān)的類 Font 字體類 FontMetrics 字體信息類 決定字體外觀的三個屬性 字體 字體風(fēng)格 字體大小 設(shè)置字體的基本步驟 創(chuàng)建Font對象調(diào)用組件中的setFont 成員方法設(shè)置字體 Font構(gòu)造方法的格式 Font 字體名 字體風(fēng)格 字體大小 publicstaticfinalintPLAIN 0 粗體publicstaticfinalintBOLD 1 斜體publicstaticfinalintITALIC 2 普通 importjava awt importjavax swing publicclasssetFontDemoextendsJPanel publicvoidpaintComponent Graphicsg super paintComponent g Graphics2Dg2 Graphics2D g Fontf1 newFont TimesNewRoman Font BOLD 16 Fontf2 newFont 黑體 Font PLAIN 20 Fontf3 newFont 楷體 GB2312 Font ITALIC 24 setSize 200 160 g2 setFont f1 g2 drawString TimesRomanBOLD24 10 40 g2 setFont f2 g2 drawString 黑體PLAIN36 10 90 g2 setFont f3 g2 drawString 楷體 GB2312ITALIC48 10 130 publicstaticvoidmain String args JFramefrm newJFrame 字體設(shè)置范例 frm getContentPane add newsetFontDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 380 200 frm setVisible true 三 Swing組件 Swing組件包含容器和普通組件 容器 用來放置其他組件的組件組件 放置在容器中的用戶界面元素在Java語言中 所有的容器和組件都用相應(yīng)的類描述 因此 在設(shè)計(jì)圖形用戶界面之前一定要充分地了解相關(guān)的類定義 1 容器 常用的Swing容器主要有兩類 頂層容器 包含其他組件和容器的容器 每個應(yīng)用程序都應(yīng)該有一個頂層容器 主要有 JApplet JFrame和JDialog 通用容器 指中間層容器 主要有JPanel JScrollPane和JToolBar 頂層容器的類結(jié)構(gòu) 使用頂層容器需要注意的地方 每個GUI組件必須位于一個且僅一個容器中 每個頂層容器包含一個內(nèi)容窗格 Contentpane 所有的組件必須放置在內(nèi)容窗格中 可以在頂層容器中添加菜單欄 它將位于頂層窗格的約定位置 頂層容器 內(nèi)容窗格和菜單的位置關(guān)系 JFrame容器 構(gòu)造方法 JFrame JFrame Stringtitle 設(shè)置點(diǎn)擊關(guān)閉窗口按鈕的操作效果voidsetDefaultCloseOperation int DO NOTHING ON CLOSEHIDE ON CLOSEDISPOSE ON CLOSEEXIT ON CLOSE 設(shè)置窗口大小voidsetSize intwidth intheight voidsetSize Dimensionsize 獲取內(nèi)容窗格ContainergetContentPane 將窗口設(shè)置為最佳大小pack importjava awt importjava awt event importjavax swing publicclassTryJFrame publicstaticvoidmain Stringagrs JFrameframe newJFrame JFrame應(yīng)用舉例 frame setDefaultCloseOperation JFrame EXIT ON CLOSE JLabellabel newJLabel JFrame應(yīng)用舉例 label setPreferredSize newDimension 175 100 frame getContentPane add label BorderLayout CENTER frame pack frame setVisible true JPanel類 通用容器 JPanel容器又稱為面板容器 其主要特點(diǎn)有 不能作為頂層容器 可以嵌套放置 組件直接放置在容器中 importjavax swing publicclassJPanelDemo publicJPanelDemo JButtonbutton newJButton Hello JPanelpanel newJPanel panel setBackground Color orange panel setPreferredSize newDimension 300 200 panel add button JFramefrm newJFrame JPanel應(yīng)用范例 frm getContentPane add panel frm pack frm setVisible true publicstaticvoidmain String args JPanelDemopanelDemo newJPanelDemo 2 Swing組件 JComponent是所有Swing組件的父類 它增加了以下功能 支持組件的可插入觀感支持工具提示當(dāng)組件被拖拽時 支持列表或樹的自動滾動支持?jǐn)U展組件類 以創(chuàng)建自己需要的組件 1 標(biāo)簽組件 JLabel 標(biāo)簽組件的主要用途是顯示提示信息 JLabel即可以顯示文字 也可以顯示圖標(biāo) publicJLabel Stringtext Iconicon inthorizontalAlignment publicJLabel Stringtext inthorizontalAlignment publicJLabel Stringtext publicJLabel Iconimage inthorizontalAlignment publicJLabel Iconimage publicJLabel publicStringgetText publicvoidsetText Stringtext publicIcongetIcon publicvoidsetIcon Iconicon publicintgetVerticalAlignment publicvoidsetVerticalAlignment intalignment publicintgetHorizontalAlignment publicvoidsetHorizontalAlignment intalignment publicintgetVerticalTextPosition publicvoidsetVerticalTextPosition inttextPosition publicintgetHorizontalTextPosition importjava awt importjavax swing publicclassJLabelDemoextendsJFrame publicJLabelDemo super 標(biāo)簽組件應(yīng)用范例 Iconicon newImageIcon map gif JLabeljLabel1 newJLabel 標(biāo)簽組件1 JLabeljLabel2 newJLabel 標(biāo)簽組件2 icon SwingConstants LEFT JLabeljLabel3 newJLabel jLabel3 setText 標(biāo)簽組件3 jLabel3 setIcon icon jLabel3 setHorizontalTextPosition SwingConstants CENTER jLabel3 setVerticalTextPosition SwingConstants BOTTOM jLabel3 setToolTipText Label3 ContainercontentPane getContentPane contentPane setLayout newFlowLayout contentPane add jLabel1 contentPane add jLabel2 contentPane add jLabel3 setSize 200 100 show publicstaticvoidmain String args JLabelDemofrm newJLabelDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 2 按鈕組件 復(fù)選框 單選框 按鈕 使用按鈕組件的基本步驟 創(chuàng)建按鈕對象將按鈕對象添加到容器中設(shè)置響應(yīng)單擊按鈕事件的操作 主要的成員方法 booleanisSelected voidsetSelected booleanb StringgetText voidsetText Stringtext IcongetIcon voidsetIcon Iconicon IcongetDisableIcon voidsetDisabledIcon Iconicon JButton JButton類定義了最常見的按鈕 用來響應(yīng)用戶的某項(xiàng)操作請求 JButton JButton Iconicon JButton Stringtext isDefaultButton setDefaultButton Buttondefault importjava awt importjava awt event importjavax swing AbstractButton importjavax swing publicclassTryButtonextendsJPanelimplementsActionListener protectedJButtonb1 b2 b3 publicTryButton b1 newJButton Disablemiddlebutton b1 setVerticalTextPosition AbstractButton CENTER b1 setHorizontalTextPosition AbstractButton CENTER b1 setMnemonic KeyEvent VK D b1 setActionCommand disable b2 newJButton Middlebutton b2 setVerticalTextPosition AbstractButton BOTTOM b2 setHorizontalTextPosition AbstractButton CENTER b2 setMnemonic KeyEvent VK M b3 newJButton Enablemiddlebutton b3 setMnemonic KeyEvent VK E b3 setActionCommand enable b3 setEnabled false b1 addActionListener this b3 addActionListener this b1 setToolTipText Todisablethemiddlebutton b2 setToolTipText Doesnothingwhenyouclickit b3 setToolTipText Enablethemiddlebutton add b1 add b2 add b3 publicvoidactionPerformed ActionEvente if disable equals e getActionCommand b2 setEnabled false b1 setEnabled false b3 setEnabled true else b2 setEnabled true b1 setEnabled true b3 setEnabled false publicstaticvoidmain String args JFrameframe newJFrame TryButton frame setDefaultCloseOperation JFrame EXIT ON CLOSE TryButtonnewContentPane newTryButton frame setContentPane newContentPane frame pack frame setVisible true JCheckBox 在Swing中 復(fù)選按鈕用JCheckBox類實(shí)現(xiàn) 與JButton相同 可以為之設(shè)置文本串和圖像 通常 將多個復(fù)選按鈕組合成一組 對于處于一組中的復(fù)選按鈕 每一時刻既可以選擇一項(xiàng) 又可以選擇多項(xiàng) JRadioButton組件 JRadioButton JRadioButton Icon JRadioButton String JRadioButton String Icon JRadioButton Icon boolean JRadioButton String boolean JRadioButton String Icon boolean 通常與ButtonGroup結(jié)合使用 3 文本框 文本框是接收用戶輸入的一種組件 主要的成員方法 StringgetText voidsetText Stringtext booleanisEditable voidsetEditable booleaneditable intgetColumns voidsetColumns intcol importjava awt importjava awt event importjavax swing publicclassJTextFieldDemoextendsJFrame JTextFieldjText JPasswordFieldjPassword publicJTextFieldDemo JLabeljLabel1 newJLabel 用戶 JLabeljLabel2 newJLabel 密碼 jText newJTextField 20 jPassword newJPasswordField 20 JButtonjButton newJButton 確定 jButton addActionListener newActionListener publicvoidactionPerformed ActionEvente jButton actionPerformed e JPanelcontentPane JPanel getContentPane contentPane setLayout newFlowLayout contentPane add jLabel1 contentPane add jText contentPane add jLabel2 contentPane add jPassword contentPane add jButton setSize 300 150 setTitle 文本輸入框范例 show privatevoidjButton actionPerformed ActionEvente Stringuser jText getText Stringpword String copyValueOf jPassword getPassword JOptionPane showMessageDialog getParent 用戶 user n 密碼 pword publicstaticvoidmain String args JTextFieldDemofrm newJTextFieldDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 四 布局管理器 對于不同的操作平臺 顯示文字 圖形的方式可能有所不同 如果顯式地指定一個組件的絕對位置和大小 有可能結(jié)果不可預(yù)測 為此 Java語言提供了布局管理器來管理放置在容器內(nèi)部的各組件大小及排列布局 利用setLayout 設(shè)置布局管理器每種容器都有一種默認(rèn)的布局管理器若不使用布局管理器 setLayout NULL 常用的布局管理器 FlowLayout 流程式 布局管理器BorderLayout 邊框式 布局管理器CardLayout 卡片式 布局管理器GridLayout 網(wǎng)格式 布局管理器 1 FlowLayout布局管理器 FlowLayout自左向右 自上向下地布置容器中所包含的GUI組件 JPanel默認(rèn)的布局管理器就是FlowLayout構(gòu)造方法FlowLayout FlowLayout intalign 默認(rèn)中對齊FlowLayout intalign inthgap intvgap importjava awt importjava awt event importjavax swing publicclassFlowLayoutDemoextendsJFrame publicFlowLayoutDemo super FlowLayout布局管理器 ContainercontentPane getContentPane contentPane setLayout newFlowLayout contentPane add newJButton 1 contentPane add newJButton 2 contentPane add newJButton 3 contentPane add newJButton 4 contentPane add newJButton 5 contentPane add newJButton 6 contentPane add newJButton 7 contentPane add newJButton 8 setSize 320 240 setVisible true setResizable false publicstaticvoidmain String args FlowLayoutDemofrm newFlowLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 2 BorderLayout布局管理器 BorderLayout將整個容器劃分為5個區(qū)域 North 上部 South 下部 East 右側(cè) West 左側(cè) 和Center 中部 JFrame的內(nèi)容窗格的默認(rèn)布局管理器是BorderLayout 構(gòu)造方法 BorderLayout BorderLayout inthgap intvgap 如果使用BorderLayout布局容器中的組件 需要明確地指出組件放置的位置add newButton Button BorderLayout SOUTH 否則默認(rèn)為中間區(qū)域 importjava awt importjava awt event importjavax swing publicclassBorderLayoutDemoextendsJFrame publicBorderLayoutDemo super BorderLayout布局管理器 ContainercontentPane getContentPane contentPane add newJButton 南 BorderLayout SOUTH contentPane add newJButton 北 BorderLayout NORTH contentPane add newJButton 中間 BorderLayout CENTER contentPane add newJButton 西 BorderLayout WEST contentPane add newJButton 東 BorderLayout EAST setSize 320 240 setVisible true publicstaticvoidmain String args BorderLayoutDemofrm newBorderLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE 3 CardLayout布局管理器 CardLayout布局管理器提供了一種基于卡片式的布局管理器 它并沒有可視化地安排組件 它所產(chǎn)生的效果就像一副撲克牌一樣疊在一起 每次顯示時只顯示最上面的一張牌 需要顯示下一張時 只需把最上面的一張牌放到最底下即可 構(gòu)造方法 CardLayout CardLayout inthgap intvgap 常用的幾個方法voidfirst Container voidlast Container voidnext Container voidprevious Container importjava awt importjava awt event importjavax swing publicclassCardLayoutDemoextendsJFrame JPanelpnlCommandArea newJPanel JPanelpnlDisplayArea newJPanel CardLayoutcardlayout1 newCardLayout JButtonbtnFirst newJButton 第一個 JButtonbtnPrevious newJButton 前一個 JButtonbtnNext newJButton 后一個 JButtonbtnLast newJButton 最后一個 ContainercontentPane publicCardLayoutDemo super CardLayout布局管理器范例 contentPane getContentPane contentPane add pnlCommandArea BorderLayout NORTH contentPane add pnlDisplayArea BorderLayout CENTER pnlDisplayArea setLayout cardlayout1 JPanelpnlFirst newJPanel pnlFirst setBackground Color blue pnlDisplayArea add first pnlFirst pnlFirst add newJLabel ThisisthefirstPanel JPanelpnlSecond newJPanel pnlSecond setBackground Color red pnlDisplayArea add second pnlSecond pnlSecond add newJLabel ThisisthesecondPanel JPanelpnlThird newJPanel pnlThird setBackground Color yellow pnlDisplayArea add third pnlThird pnlThird add newJLabel ThisisthethirdPanel JPanelpnlFourth newJPanel pnlFourth setBackground Color green pnlDisplayArea add fourth pnlFourth pnlFourth add newJLabel ThisisthefourthPanel btnFirst addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e btnPrevious addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e btnNext addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e btnLast addActionListener newActionListener publicvoidactionPerformed ActionEvente processAction e pnlCommandArea add btnFirst pnlCommandArea add btnPrevious pnlCommandArea add btnNext pnlCommandArea add btnLast setSize 400 300 setVisible true publicstaticvoidmain String args CardLayoutDemofrm newCardLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE privatevoidprocessAction ActionEvente JButtonbtnEvent JButton e getSource if btnEvent equals btnFirst cardlayout1 first pnlDisplayArea elseif btnEvent equals btnLast cardlayout1 last pnlDisplayArea elseif btnEvent equals btnPrevious cardlayout1 previous pnlDisplayArea elseif btnEvent equals btnNext cardlayout1 next pnlDisplayArea 4 GridLayout布局管理器 GridLayout提供了一種基于網(wǎng)格的布局管理方式 網(wǎng)格的行數(shù)和列數(shù)可以在創(chuàng)建GridLayout對象時指定 另外 容器中每個組件將占據(jù)大小完全相同的一個網(wǎng)格 向網(wǎng)格中布局GUI組件有兩種方法 使用默認(rèn)的順序采用add Componentcomp intindex 構(gòu)造方法 GridLayout GridLayout introws intcols GridLayout introws intcols inthgap intvgap importjava awt importjava awt event importjavax swing publicclassGridLayoutDemoextendsJFrame publicGridLayoutDemo super GridLayout布局管理器應(yīng)用范例 ContainercontentPane getContentPane contentPane setLayout newGridLayout 2 4 contentPane add newJButton 1 contentPane add newJButton 2 contentPane add newJButton 3 contentPane add newJButton 4 contentPane add newJButton 5 contentPane add newJButton 6 contentPane add newJButton 7 contentPane add newJButton 8 publicstaticvoidmain String args GridLayoutDemofrm newGridLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 320 240 frm setVisible true 不使用布局管理器 importjava awt importjava awt event importjavax swing publicclassNullLayoutDemoextendsJFrame JLabellblUserName newJLabel 用戶名稱 JTextFieldtxtUserName newJTextField JLabellblUserPasswd newJLabel 用戶密碼 JPasswordFieldtxtUserPasswd newJPasswordField JButtonbtnLogin newJButton 登錄 JButtonbtnReset newJButton 重設(shè) publicNullLayoutDemo super 不使用布局管理器的應(yīng)用范例 ContainercontentPane getContentPane contentPane setLayout null lblUserName setBounds newRectangle 45 38 67 34 txtUserName setBounds newRectangle 115 39 210 33 lblUserPasswd setBounds newRectangle 43 86 66 26 txtUserPasswd setBounds newRectangle 115 84 210 33 btnLogin setBounds newRectangle 78 150 86 30 btnReset setBounds newRectangle 193 150 86 30 contentPane add lblUserName contentPane add txtUserName contentPane add lblUserPasswd contentPane add txtUserPasswd contentPane add btnLogin contentPane add btnReset publicstaticvoidmain String args NullLayoutDemofrm newNullLayoutDemo frm setDefaultCloseOperation JFrame EXIT ON CLOSE frm setSize 400 240 frm setVisible true 運(yùn)行結(jié)果 上機(jī)作業(yè) 設(shè)計(jì) 計(jì)算器 界面設(shè)計(jì) 萬年日歷 界面

注意事項(xiàng)

本文(Java圖形用戶界面第7章.ppt)為本站會員(max****ui)主動上傳,裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對上載內(nèi)容本身不做任何修改或編輯。 若此文所含內(nèi)容侵犯了您的版權(quán)或隱私,請立即通知裝配圖網(wǎng)(點(diǎn)擊聯(lián)系客服),我們立即給予刪除!

溫馨提示:如果因?yàn)榫W(wǎng)速或其他原因下載失敗請重新下載,重復(fù)下載不扣分。




關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號:ICP2024067431-1 川公網(wǎng)安備51140202000466號


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請立即通知裝配圖網(wǎng),我們立即給予刪除!