2010-11-13 72 views
3

我正在做一個項目,我需要一些自定義的swing組件。到目前爲止,我已經制作了一個帶有一系列圖像的新按鈕(Java Metal外觀根本不符合我的UI)。我在這個新組件上實現了MouseListener,這是我的問題出現的地方。我的小工具在懸停時更改圖片,點擊等,除了我的MouseListener拿起鼠標進入整個GridLayout容器,而不是進入圖像。所以我有一個大約200 * 100的圖像,周圍的容器大約是400 * 200,並且mouseEntered方法在它進入GridLayout部分(甚至它的空白部分)而不是在圖像上時被觸發。我怎樣才能讓它在我懸停在圖像上時才被解僱?我試着設置大小和邊界以及其他屬性無濟於事。Java swing JComponent「size」

編輯:這是我的問題的演示。正如您所看到的(顏色非常相似),只需輸入GridlLayout的部分即可突出顯示右下角的按鈕。我只想讓它突出顯示實際圖像,而不是GridLayout部分。

alt text

我將不添加MouseListener方法,因爲它們只是涉及切換顯示的圖像。

public customWidget() 
{ 
    this.setLayout(new FlowLayout()); 
    try { 
     imageDef=ImageIO.read(new File("/home/x101/Desktop/buttonDef.png")); 
     imageClick=ImageIO.read(new File("/home/x101/Desktop/buttonClick.png")); 
     imageHover=ImageIO.read(new File("/home/x101/Desktop/buttonHover.png")); 
     current=imageDef; 
    } catch (IOException e) 
    { 

     e.printStackTrace(); 
    } 
    this.addMouseListener(this); 
} 

protected void paintComponent(Graphics g) 
{ 
    super.paintComponents(g); 
    g.drawImage(current, 0, 0, current.getWidth(), current.getHeight(), null); 

} 

編輯:添加代碼段

+0

請發佈一個自包含的代碼示例...使您更容易看到您正在嘗試做什麼...並更容易找到問題... – 2010-11-13 00:16:08

+0

'customWidget'的超類是什麼?另外,你的'GridLayout容器'在哪裏設置? – 2010-11-13 00:36:25

+0

customWidget擴展了JComponent。 GridLayout正在測試程序im中設置,用於測試小部件。將提出一個圖像來演示。 – Alex 2010-11-13 00:43:55

回答

3

我假設你的映像只包含4 'customWidget'(在一個2x2網格)的對象。

您的代碼正如我所料。你的MouseListener方法響應MouseEvent的'customWidget'(不是'customWidget'中繪製的圖像),它的大小佔用了圖像的1/4,所以它們會在進入放大區域時作出響應。該錯誤實際上是在您的測試程序中,因爲您允許自定義按鈕小部件大於圖像。

如果你想要一個提供類似於你的圖像的測試程序,你應該創建一個更大的網格(比如說4x4),然後只將你的按鈕放在其他網格節點中。將空白組件放入空隙中。

+0

你正在我的思路上。有沒有一個佈局管理器,可以很容易地在屏幕上設置一個部分,比如一個高度爲110的鋼筋,而不是gridlayout和flowlayout所做的所有比例材料? – Alex 2010-11-13 01:09:28

+0

'BoxLayout'包含這些不可見的填充組件:http://download.oracle.com/javase/tutorial/uiswing/layout/box.html#filler – trashgod 2010-11-13 01:21:32

+0

此外,如果您可以使用垃圾回收的方法,我會高度推薦使用標準方法。 :) – 2010-11-13 01:24:37

0

雖然我不會回答你的特定問題,我希望這有助於:

如果組件只看錯也許你應該重用Swing組件和公正寫一個自定義Look &感覺或主題。

這肯定有助於確保應用程序的外觀是一致的,至少您正在使用正確的工具完成您想要完成的任務。

作爲旁註,請注意Java帶有多個Look &感覺,包括Look &感覺是爲了模仿本機操作系統主題。

參見:http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

+0

即時消息意識到外觀和感覺,但它是一個團隊項目,我正在與別人的設計工作,他們用玻璃外觀按鈕等使它變得光彩照人。所以這是決定採取的道路。無論如何,歡呼! – Alex 2010-11-13 00:35:41

8

作爲替代方案,考慮The Button API,包括方法setRolloverIcon()「使當光標經過它的按鈕顯示指定的圖標。」

附錄:對於example

import java.net.MalformedURLException; 
import java.net.URL; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class ButtonIconTest { 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 

    private static void createAndShowGui() { 
     String base = "http://download.oracle.com/" 
      + "javase/tutorial/uiswing/examples/components/" 
      + "RadioButtonDemoProject/src/components/images/"; 
     ImageIcon dog = null; 
     ImageIcon pig = null; 
     try { 
      dog = new ImageIcon(new URL(base + "Dog.gif")); 
      pig = new ImageIcon(new URL(base + "Pig.gif")); 
     } catch (MalformedURLException ex) { 
      ex.printStackTrace(System.err); 
      return; 
     } 
     JFrame frame = new JFrame("Rollover Test"); 
     JPanel panel = new JPanel(); 
     panel.add(new JLabel(dog)); 
     panel.add(new JLabel(pig)); 

     JButton button = new JButton(dog); 
     button.setRolloverIcon(pig); 
     panel.add(button); 

     frame.add(panel); 

     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
}