日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Swing 邊框(一)

系統(tǒng) 2065 0

Swing組件提供了對組件周圍的邊框區(qū)域進(jìn)行定制的功能。為了簡單,我們可以使用預(yù)定義的八個邊框,或者是我們可以創(chuàng)建自己的邊框。在本章中,我們將會了解如何最好的使用已存在邊框以及如何創(chuàng)建我們自己的邊框。

7.1 Some Basics on Woring with Borders

邊框是帶有標(biāo)準(zhǔn)的setBorder()與getBorder()屬性方法的JComponent屬性。所以,所有的JComponent子類的Swing組件都具有邊框。默認(rèn)情況下,一個組件并沒有與其相關(guān)聯(lián)的自定義邊框。(JComponent的getBorder()方法返回null。)相反,組件顯示的默認(rèn)邊框是依據(jù)當(dāng)前的觀感對于其狀態(tài)最為合適的邊框。例如,對于JButton,對于每一個觀感特定不同的邊框,邊框可以表現(xiàn)為按下,未按下或是禁止。

盡管對于所有的組件初始的邊框?qū)傩栽O(shè)置為null,我們可以通過調(diào)用JComponent的setBorder(Border newValue)方法來修改組件的邊框。一旦設(shè)置,修改的值就會覆蓋當(dāng)前觀感的邊框,并且在組件的區(qū)域內(nèi)繪制新邊框。如果在稍后的時候,我們希望將邊框重新設(shè)置為對于狀態(tài)與觀感合適的邊框,我們可以將邊框?qū)傩孕薷臑閚ull,使用setBorder(null)并且調(diào)用組件的updateUI()方法。updateUI()方法會通知觀感重新設(shè)置邊框。如果我們沒有調(diào)用updateUI()方法,則組件將沒有邊框。

圖7-1顯示了一個JLabel周圍的各種邊框設(shè)置,通過文本標(biāo)簽來標(biāo)明邊框類型。如何創(chuàng)建不同的邊框?qū)诒菊碌纳院蟛糠诌M(jìn)行討論。

swing_7_1

7.1.1 Exploring the Border Inteface

我們可以在javax.swing.border包中找到Border接口。這個接口構(gòu)成了所有邊框類的基礎(chǔ)。這個接口直接由AbstractBorder類實現(xiàn),這個類是所有預(yù)定義的Swing邊框類的父類:BevelBorder,CompoundBorder,EmptyBorder,EtchedBorder,LineBorder,MatteBorder,SoftBevelBorder以及TitledBorder。另外一個有趣的類就是BorderFactory類,我們可以在javax.swing包中找到這個類。這個類使用工廠設(shè)計模式來創(chuàng)建邊框,隱藏了實現(xiàn)細(xì)節(jié),并且可以緩存各種選項來優(yōu)化共同使用。

在這里顯示的Border接口由三個方法構(gòu)成:paintBorder(),getBordernsets()以及isBorderOpaque()。這些方法會在后面的章節(jié)中進(jìn)行描述。

paintBorder()

paintBorder()方法是這個接口的關(guān)鍵方法。其定義如下:

      public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    

邊框?qū)崿F(xiàn)的繪制是由這個方法完成的。通常,Border實現(xiàn)首先會詢問Insets維度,然后在外層區(qū)域的四個邊進(jìn)行繪制,如圖7-2所示。如果邊框是不透明的,paintBorder()實現(xiàn)必須填充整個內(nèi)部區(qū)域。如果一個邊框是不透明的,并沒有填充區(qū)域,那么這是一個bug并且需要修正。

列表7-1顯示了一個簡單的paintBorder()實現(xiàn),這個實現(xiàn)使用比上部與下部略淺的顏色填充左邊與右邊。

        
          public
        
        
          void
        
         paintBorder(Component c, Graphics g, 
        
          int
        
         x, 
        
          int
        
         y, 
        
          int
        
         width, 
        
          int
        
         height) { Insets insets = getBorderInsets(c); Color color = c.getForeground(); Color brighterColor = color.brighter(); 
        
          // Translate coordinate space
        
         g.translate(x, y); 
        
          // Top
        
         g.setColor(color); g.fillRect(0, 0, width, insets.top); 
        
          // Left
        
         g.setColor(brighterColor); g.fillRect(0, insets.top, insets.left, height-insets.top-insets.bottom); 
        
          // Bottom
        
         g.setColor(color); g.fillRect(0, height-insets.bottom, width, insets.bottom); 
        
          // Right
        
         g.setColor(brighterColor); g.fillRect(width-insets.right, insets.top, insets.right, height-insets.top-insets.bottom); 
        
          // Translate coordinate space back
        
         g.translate(-x, -y); }
      

?

當(dāng)創(chuàng)建我們自己的邊框時,我們將會經(jīng)常發(fā)現(xiàn)我們自己在填充相同的非重疊矩形區(qū)域。Graphics的translate()方法簡化了繪制坐標(biāo)的指定。無需轉(zhuǎn)換坐標(biāo),我們需要通過原始的(x,y)來偏移繪制。

注意:我們不能通過插入g.fillRect(x,y,width,height)來簡化,因為這會填充整個組件區(qū)域,而不是邊框區(qū)域。

getBorderInsets()

getBorderInsets()方法會返回在指定的組件c作為Insets對象的周圍繪制邊框所必須的空間。其定義如下:

      public Insets getBorderInsets(Component c)
    

如圖7-2所示,這些內(nèi)部區(qū)域定義了可以繪制邊框的合法區(qū)域。Component參數(shù)可以使得我們使用他的一些屬性來決定內(nèi)部區(qū)域的尺寸。

isBorderOpaque()

邊框可以是不透明的或是透明的。isBorderOpaque()方法可以返回true或是false來表明邊框是哪種形式。其定義如下:

      public boolean isBorderOpaque()
    

當(dāng)這個方法返回true時,邊框需要是非透明的,填充其整個內(nèi)部區(qū)域。當(dāng)其返回false時,沒有繪制的區(qū)域?qū)3诌吙蛩诘慕M件的背景顏色。

7.1.2 Introducing BorderFactory

現(xiàn)在我們已經(jīng)基本了解了Border接口是如何工作的,現(xiàn)在我們來了解一下作為簡單創(chuàng)建邊框方法的BorderFactory類。我們可以在javax.swing包中找到這個類,BorderFactory類提供了一系列的static方法來創(chuàng)建預(yù)定義的邊框。無需調(diào)用不同的邊框類的特定構(gòu)造函數(shù),通過這個工廠類我們幾乎可以創(chuàng)建所有的邊框。這個工廠類同時可以緩存一些邊框的創(chuàng)建從而避免多次重新創(chuàng)建經(jīng)常使用的邊框。這個類的定義如下:

        
          public
        
        
          class
        
         BorderFactory { 
        
          public
        
        
          static
        
         Border createBevelBorder(
        
          int
        
         type); 
        
          public
        
        
          static
        
         Border createBevelBorder(
        
          int
        
         type, Color highlight, Color shadow); 
        
          public
        
        
          static
        
         Border createBevelBorder(
        
          int
        
         type, Color highlightOuter, Color highlightInner, Color shadowOuter, Color shadowInner); 
        
          public
        
        
          static
        
         CompoundBorder createCompoundBorder(); 
        
          public
        
        
          static
        
        
          public
        
        
          static
        
         Border createEmptyBorder(); 
        
          public
        
        
          static
        
         Border createEmptyBorder(
        
          int
        
         top, 
        
          int
        
         left, 
        
          int
        
         bottom, 
        
          int
        
         right); 
        
          public
        
        
          static
        
         Border createEtchedBorder(); 
        
          public
        
        
          static
        
         Border createEtchedBorder(Color highlight, Color shadow); 
        
          public
        
        
          static
        
         Border createEtchedBorder(
        
          int
        
         type); 
        
          public
        
        
          static
        
         Border createEtchedBorder(
        
          int
        
         type, Color highlight, Color shadow); 
        
          public
        
        
          static
        
         Border createLineBorder(Color color); 
        
          public
        
        
          static
        
         Border createLineBorder(Color color, 
        
          int
        
         thickness); 
        
          public
        
        
          static
        
         Border createLoweredBevelBorder(); 
        
          public
        
        
          static
        
         MatteBorder createMatteBorder(
        
          int
        
         top, 
        
          int
        
         left, 
        
          int
        
         bottom, 
        
          int
        
         right, Color color); 
        
          public
        
        
          static
        
         MatteBorder createMatteBorder(
        
          int
        
         top, 
        
          int
        
         left, 
        
          int
        
         bottom, 
        
          int
        
         right, Icon icon); 
        
          public
        
        
          static
        
         Border createRaisedBevelBorder(); 
        
          public
        
        
          static
        
         TitledBorder createTitledBorder(Border border); 
        
          public
        
        
          static
        
         TitledBorder createTitledBorder(Border border, String title); 
        
          public
        
        
          static
        
         TitledBorder createTitledBorder(Border border, String title, 
        
          int
        
         justification, 
        
          int
        
         position); 
        
          public
        
        
          static
        
         TitledBorder createTitledBorder(Border border, String title, 
        
          int
        
         justification, 
        
          int
        
         position, Font font); 
        
          public
        
        
          static
        
         TitledBorder createTitledBorder(Border border, String title, 
        
          int
        
         justification, 
        
          int
        
         position, Font font, Color color); 
        
          public
        
        
          static
        
         TitledBorder createTitledBorder(String title); }
      

?

我們將會在描述特定的邊框類型的過程中描述這個類的不同方法。例如,要創(chuàng)建一個具有紅線的邊框,我們可以使用下面的語句,然后將這個邊框關(guān)聯(lián)到一個組件。

      Border lineBorder = BorderFactory.createLineBorder(Color.RED);
    

7.1.3 Starting with AbstractBorder

在我們了解javax.swing.border包中單個的邊框之前,一個系統(tǒng)邊框需要獲得特別的關(guān)注:AbstractBorder。正如前面所提到的,AbstractBorder類是其他的預(yù)定義邊框的父類。

創(chuàng)建AbstractBorder

AbstractBorder有一個構(gòu)造函數(shù):

      public AbstractBorder()
    

因為AbstractBorder是其他標(biāo)準(zhǔn)邊框的父類,這個構(gòu)造函數(shù)實際是為其他邊框類自動調(diào)用的。

檢測AbstractBorder方法

AbstractBorder類提供了Border接口的三個方法實現(xiàn)。

      public Insets getBorderInsets(Component c)
    

AbstractBorder的內(nèi)部區(qū)域是零。每一個預(yù)定義的子類要重寫getBorderInsets()方法。

      public boolean isBorderOpaque()
    

AbstractBorder的默認(rèn)非透明屬性設(shè)置為false。這就意味著如果我們需要繪制類似點劃線的邊框,組件的背景將會是透明的。許多預(yù)定義的子類重寫了isBorderOpaque()方法。

      public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    

AbstractBorder的繪制邊框是空的。所有的子類應(yīng)該重寫這個方法來實際繪制一個邊框,也許除了EmptyBorder。

除了提供了Border方法的默認(rèn)實現(xiàn)以外,AbstractBorder提供了我們可以利用的其他兩個功能,或者僅是允許系統(tǒng)使用。首先,還有另外一個需要兩個參數(shù)Component與Insets的getBorderInsets()方法:

      public Insets getBorderInsets(Component c, Insets insets)
    

在這個方法版本中,并沒有創(chuàng)建并返回一個新的Insets對象,所傳遞的Insets對象首先被修改然后返回。使用這個方法可以避免每次需要查詢邊框內(nèi)部區(qū)域時創(chuàng)建然后銷毀額外的Insets對象。

第二個可用的新方法是getInteriorRectangle(),這個方法有靜態(tài)與非靜態(tài)版本。指定了Component,Border,以及四個整數(shù)參數(shù),這個方法將會返回一個內(nèi)部的Rectangle,從而組件可以在邊框內(nèi)部區(qū)域內(nèi)繪制其自身。

Swing 邊框(一)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 漳平市| 云浮市| 贵州省| 宝兴县| 辽中县| 缙云县| 自贡市| 彝良县| 兴安县| 汝州市| 洞头县| 犍为县| 高安市| 鹤庆县| 资兴市| 读书| 淳安县| 昭苏县| 滨海县| 奉节县| 泸水县| 格尔木市| 耿马| 汤阴县| 大悟县| 阳信县| 邵阳县| 疏附县| 嘉峪关市| 海林市| 鸡泽县| 廊坊市| 喀喇沁旗| 历史| 华宁县| 汤阴县| 临城县| 灵山县| 兴城市| 博乐市| 兖州市|