2015-08-30 29 views
0

我在玻璃窗格下有兩個圖像的代碼。我想要讓每個圖像都在它自己的玻璃窗格下,並且每個玻璃窗格都有自己的mouselistener信號。目前,我已經將它們兩個放在一個玻璃板下面,一個鼠標器用於整個玻璃板。兩幅圖像並排排列,因此將玻璃板分成兩半不應太難。這裏是一個玻璃窗格的代碼,但請注意,我正在嘗試製作兩個玻璃窗格,並且兩個單獨的鼠標監聽器類爲每個圖像。這僅僅是與的**都圖像一個*鼠標監聽代碼:我可以將玻璃窗格分成兩部分,併爲每個窗格添加一個單獨的mouselistener?

package Buttons; 


import java.awt.GridLayout; 
import java.awt.event.*; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Giraffewindow extends JDialog { 
public Giraffewindow() { 
    JDialog giraffewindow = new JDialog(); 

    Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png")); 
    Icon windows = new ImageIcon(getClass().getResource("windows.png")); 

    giraffewindow.setLayout(new GridLayout(1, 2, 0, 0)); 
    giraffewindow.add(new JLabel(windows)); 
    giraffewindow.add(new JLabel(giraffe)); 


    giraffewindow.pack(); 
    giraffewindow.setTitle("GIRAFFE!"); 
    giraffewindow.setVisible(true); 
    giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    JPanel glass = ((JPanel) giraffewindow.getGlassPane()); 
    glass.setVisible(true); 
    status = new JLabel("I can change"); 

    glass.add(status); 
    glass.setLayout(null); 
    giraffemousehandler giraffemouse = new giraffemousehandler(); 
    glass.addMouseListener(giraffemouse); 
    glass.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture 
    // setLayout(null); 
} 


JLabel status = null; 

class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods 

    @Override 
    public void mouseMoved(MouseEvent e) { 
     // TODO Auto-generated method stub 
     status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 

     status.setText("Enter); 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 

     status.setText("Exit"); 
     // status.setBounds(e.getX(), e.getY(), 5, 6); 

    } 

} 
} 

這裏是camickr的請求的代碼,請注意,是兩個不同的鼠標監聽,我很想知道如何做到這一點除此以外。當JLabel遵循鼠標時,1)它離鼠標非常遠,2)它沒有顯示完整的JLabel,3)它在一次退出/輸入後不會改變。我真的很感激的幫助下,這裏是基於camickrs建議代碼:

import java.awt.GridLayout; 
import java.awt.event.*; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

class SSCCE extends JDialog { 

public SSCCE() { 
    JDialog giraffewindow = new JDialog(); 

    Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png")); 
    Icon windows = new ImageIcon(getClass().getResource("windows.png")); 

    giraffewindow.setLayout(new GridLayout(1, 2, 0, 0)); 
    JLabel giraffelabel = new JLabel(); 
    JLabel windowlabel = new JLabel(); 

    windowlabel.setIcon(windows); 
    giraffelabel.setIcon(giraffe); 

    giraffewindow.add(windowlabel); 
    giraffewindow.add(giraffelabel); 

    giraffewindow.setTitle("Title!"); 
    giraffewindow.setSize(1100, 600); 
    giraffewindow.setVisible(true); 
    giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

    JPanel glass = ((JPanel) giraffewindow.getGlassPane()); //Glasspane 
    glass.setVisible(true); 

    status = new JLabel("I can change"); //This is the JLabel which should follow my mouse 

    glass.add(status); 
    glass.setLayout(null); 

    giraffemousehandler giraffemouse = new giraffemousehandler(); 
    windowmousehandler windowmouse = new windowmousehandler(); 

    windowlabel.addMouseListener(windowmouse); 
    giraffelabel.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture 

    // setLayout(null); 
} 

JLabel status = null; 

class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods 

    @Override 
    public void mouseMoved(MouseEvent e) { 
     // TODO Auto-generated method stub 
     status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 

     status.setText("Mouse is on giraffe"); 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 

     status.setText("Mouse has left giraffe"); 
     // status.setBounds(e.getX(), e.getY(), 5, 6); 

    } 

} 

class windowmousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { 
    public void mouseMoved(MouseEvent event) { 
     // TODO Auto-generated method stub 
     status.setBounds(event.getX(), event.getY(), 50, 60); //Makes JLabel follow mouse 

    } 

    public void mouseEntered(MouseEvent event) { 

     status.setText("Mouse is on window"); 

    } 

    @Override 
    public void mouseExited(MouseEvent event) { 

     status.setText("Mouse has left window"); 
     // status.setBounds(e.getX(), e.getY(), 5, 6); 

    } 
} 
} 

public class Icallsscce { 
    public static void main(String [] args) { 
     SSCCE object = new SSCCE(); 
    } 
} 
+0

考慮在看看'JLayer'(用於Java 7,'JXLayer'之前) – MadProgrammer

+0

這是什麼你試圖做的實現?您想要在與玻璃窗格不同的圖像上發生什麼操作? – MadProgrammer

+0

@MadProgrammer我想在每個圖像上有不同的事件。玻璃窗格就在那裏,以便JLabel不會出現在圖片的下方 –

回答

2

幾乎每一個問題可以在10〜50行的代碼來證明,如果你真正瞭解你想要達到的目的。

編號組件是一個組件,您不能將組件分成兩個組件。

你有兩個選擇:

  1. 如果添加的MouseListener到玻璃面板然後你會需要從事件中使用鼠標點,並使用Container.getComponentAt(...)判斷鼠標當前哪個組件過度。請注意,GlassPane覆蓋整個框架,因此鼠標點相對於包含邊框的框架,所以您首先需要使用SwingUtilities.convertPoint(...)方法將鼠標點轉換爲相對於添加了標籤的面板的點。

  2. 如果爲每個標籤添加單獨的MouseListeners,則鼠標點將與標籤相關,因此當您移動彈出標籤時,需要將標籤的「x」值添加到玻璃窗格上的點。我認爲這種方法更容易。用這種方法注意你可以共享同一個監聽器,你只需要使用MouseEvent的getSource()方法來獲取標籤。

編輯:

我怎麼能的getSource()方法添加到我的MouseEvent?

這已經在10天前回答:Rollover on JLabel which consists of image within grid layout?

當你甚至沒有看代碼,因爲它不是一個完整的程序,它會變得煩人。我們不在這裏爲你寫代碼。當有人花時間回答問題時,至少你可以花時間理解這個建議。

1)它是從鼠標非常遠,

我已經回答了這個問題。我說you will need to add the "x" value of the label..。您需要這樣做,因爲鼠標事件是相對於添加偵聽器的組件生成的。

因此,對於長頸鹿標籤,x的值將從0開始並隨着鼠標向右移動而增加。但是,長頸鹿始於玻璃窗格的中心,因此您不能使用0作爲彈出式標籤的位置。您還需要包含長頸鹿的x位置。

所以基本的代碼應該是:

popupLabel.setLocation(e.getX() + giraffe.getLocation().x, e.getY()); 

當然,你應該使用的MouseEvent的「源」對象(不是長頸鹿標籤)作爲答案展示之上。當您使用源對象時,代碼將適用於這兩個標籤。

2),因爲你在你的setBounds()方法使用 「幻數」 它不顯示完整的JLabel

。你爲什麼在標籤的寬度上使用「60」這樣的數字?不要硬編碼數字。相反,當您更改標籤的文本時,您可以這樣做:

label.setText(....); 
label.setSize(label.getPreferredSize()); 

現在標籤將具有適當的寬度/高度。然後,當您移動玻璃板上的標籤時,只需使用setLocation(...)方法定位標籤。

它不會改變一個退出後/輸入

您是實現的MouseListener和的MouseMotionListener,所以你需要這兩個偵聽器添加到每個組件。所以你需要:

giraffe.addMouseListener(...); giraffe.addMouseMotionListener(...); window.addMouseListener(...); window.addMouseMotionListener(...);

同樣,記住你只需要一個類來實現10天前演示的MouseListener和MouseMotionListener。監聽器的相同實例可以添加到這兩個組件中,這將有助於清理代碼。

+0

在我這樣做之前,我的JLabel應該遵循我的鼠標。它還會在玻璃面板上,因爲在我將它添加到玻璃面板之前,但是現在您告訴我將它添加到JLabel中? –

+0

感謝您的編輯,您是否介紹瞭如何將getSource()方法添加到我的mouseevent? –

+0

我試過了你的方法,但是現在JLabel跟着鼠標的時候,1)它離鼠標非常遠,2)它沒有顯示完整的JLabel,3)一個退出/輸入後它不會改變 –

2

我真的認爲你會在glassPane上使用MouseListener,因爲這會消耗鼠標事件,這意味着其他任何組件都不會實際得到通知。

取而代之,我會使用玻璃並監視鼠標在它上方的運動以找到它傳遞的組件,然後相應地更改「懸停標籤」。

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Container; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.Point; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRootPane; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE; 

public class Giraffewindow { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 
       new Giraffewindow(); 
      } 
     }); 
    } 

    public Giraffewindow() { 
     JDialog giraffewindow = new JDialog(); 

     try { 
      Icon giraffe = new ImageIcon(ImageIO.read(new File("..."))); 
      Icon windows = new ImageIcon(ImageIO.read(new File("..."))); 

      JLabel left = new JLabel(windows); 
      JLabel right = new JLabel(giraffe); 

      giraffewindow.setLayout(new GridBagLayout()); 
      giraffewindow.add(left); 
      giraffewindow.add(right); 

      giraffewindow.pack(); 
      giraffewindow.setLocationRelativeTo(null); 
      giraffewindow.setTitle("GIRAFFE!"); 
      giraffewindow.setVisible(true); 
      giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
      JPanel glass = ((JPanel) giraffewindow.getGlassPane()); 
      glass.setVisible(true); 
      status = new JLabel("I can change"); 
      status.setForeground(Color.WHITE); 
      status.setBackground(Color.RED); 
      status.setOpaque(true); 

      glass.add(status); 
      glass.setLayout(null); 
      giraffemousehandler giraffemouse = new giraffemousehandler(); 

      glass.addMouseMotionListener(giraffemouse); 

      // setLayout(null); 
     } catch (IOException exp) { 
      exp.printStackTrace(); 
     } 
    } 

    JLabel status = null; 

    class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods 

     private Component last = null; 

     @Override 
     public void mouseMoved(MouseEvent e) { 
      // TODO Auto-generated method stub 

      Point p = e.getPoint(); 

      JRootPane rootPane = SwingUtilities.getRootPane(e.getComponent()); 
      Container contentPane = rootPane.getContentPane(); 
      Component comp = contentPane.getComponentAt(p); 
      if (comp != last) { 
       if (last != null) { 
        mouseExited(last); 
       } 
       if (comp != null) { 

        if (comp != contentPane) { 
         last = comp; 
         mouseEntered(comp); 
        } else { 
         last = null; 
        } 

       } 
      } 

      status.setSize(status.getPreferredSize()); 
      status.setLocation(p.x, p.y); 

     } 

     protected void mouseExited(Component comp) { 
      System.out.println("Exited"); 
      status.setText("Exited"); 
     } 

     protected void mouseEntered(Component comp) { 
      status.setText("Entered"); 
     } 

    } 
} 

我想,如果提示會更容易些......

+0

雖然你沒有標出最好的答案,但我已經通過贊成你的回答對你表示感謝。非常感謝! –

相關問題