我正在做一個項目,我需要一些自定義的swing組件。到目前爲止,我已經制作了一個帶有一系列圖像的新按鈕(Java Metal外觀根本不符合我的UI)。我在這個新組件上實現了MouseListener
,這是我的問題出現的地方。我的小工具在懸停時更改圖片,點擊等,除了我的MouseListener
拿起鼠標進入整個GridLayout
容器,而不是進入圖像。所以我有一個大約200 * 100的圖像,周圍的容器大約是400 * 200,並且mouseEntered
方法在它進入GridLayout
部分(甚至它的空白部分)而不是在圖像上時被觸發。我怎樣才能讓它在我懸停在圖像上時才被解僱?我試着設置大小和邊界以及其他屬性無濟於事。Java swing JComponent「size」
編輯:這是我的問題的演示。正如您所看到的(顏色非常相似),只需輸入GridlLayout
的部分即可突出顯示右下角的按鈕。我只想讓它突出顯示實際圖像,而不是GridLayout
部分。
我將不添加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);
}
編輯:添加代碼段
請發佈一個自包含的代碼示例...使您更容易看到您正在嘗試做什麼...並更容易找到問題... – 2010-11-13 00:16:08
'customWidget'的超類是什麼?另外,你的'GridLayout容器'在哪裏設置? – 2010-11-13 00:36:25
customWidget擴展了JComponent。 GridLayout正在測試程序im中設置,用於測試小部件。將提出一個圖像來演示。 – Alex 2010-11-13 00:43:55