2010-12-03 48 views
4

我有一個問題,當我將鼠標監聽器添加到用作選項卡的組件時,我無法切換選項卡。Tab組件消耗鼠標,所以選項卡不會改變

這說明問題:

import javax.swing.*; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 

public class JTabBug { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JTabbedPane jTabbedPane = new JTabbedPane(); 
       jTabbedPane.addTab("Red", new JLabel("Roses")); 
       jTabbedPane.addTab("Blue", new JLabel("Skies")); 
       jTabbedPane.addTab("Green", new JLabel("Grass")); 

       for (int i = 0; i < jTabbedPane.getTabCount(); i++) { 
        JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i)); 

        tabComponent.addMouseMotionListener(new MouseMotionAdapter() { 
         @Override 
         public void mouseDragged(MouseEvent e) { 
          System.out.println("dragging"); 
         } 
        }); 
        jTabbedPane.setTabComponentAt(i, tabComponent); 
       } 

       JFrame jFrame = new JFrame("Testing"); 
       jFrame.add(jTabbedPane); 
       jFrame.setSize(400, 500); 
       jFrame.setVisible(true); 
       jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      } 
     }); 
    } 
} 

拖動打印出來作爲預期,但你不能改變標籤。

回答

0

這似乎工作:請注意,我正在添加JLabel,而不是創建一個新的添加。

import javax.swing.*; 

import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 

public class JTabBug { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JTabbedPane jTabbedPane = new JTabbedPane(); 
       jTabbedPane.addTab("Red", new JLabel("Roses")); 
       jTabbedPane.addTab("Blue", new JLabel("Skies")); 
       jTabbedPane.addTab("Green", new JLabel("Grass")); 

       for (int i = 0; i < jTabbedPane.getTabCount(); i++) { 
        JLabel tabComponent = (JLabel)jTabbedPane.getComponent(i); 

        tabComponent.addMouseMotionListener(new MouseMotionAdapter() { 
         @Override 
         public void mouseDragged(MouseEvent e) { 
          System.out.println("dragging"); 
         } 
        }); 
       } 

       JFrame jFrame = new JFrame("Testing"); 
       jFrame.add(jTabbedPane); 
       jFrame.setSize(400, 500); 
       jFrame.setVisible(true); 
       jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      } 
     }); 
    } 
} 
+1

這增加了鼠標移動偵聽器添加的標籤(玫瑰,天空,草地),而不是標籤本身,這是什麼OP是試圖做。 – 2010-12-03 18:05:09

+0

我有一些代碼可以做到這一點...讓我看看我是否能找到它。 – Merky 2010-12-03 18:08:02

+0

我的代碼也有一個錯誤,就像你打電話一樣!我注意到有一個運行代碼的小區域,它切換。點擊選項卡最右側的下方,它會爲我切換。不知道這是什麼交易... – Merky 2010-12-03 18:27:40

0

我不認爲這是一個錯誤,因爲它正在做我期望的。您正在爲選項卡(JLabel)創建一個新組件,將運動監聽器附加到該組件,然後將其設置爲選項卡。你不會添加一個鼠標點擊監聽器到標籤,這會導致標籤改變,所以我不會指望它在那裏。原始選項卡組件處理此鼠標單擊事件,因此如果您可以訪問該組件,請嘗試複製它(如果可以的話)(或只是訪問該組件並添加鼠標動作適配器)。如果這是不可能的,只需自己處理點擊事件。

0

如果新選項卡組件具有另一個偵聽器,它看起來像鼠標事件不會更改選項卡的選擇。不知道爲什麼這是因爲新的標籤選項卡組件沒有鼠標移動偵聽器。如果添加另一個鼠標監聽改變選擇:

   final int index = i; 
       tabComponent.addMouseListener(new MouseAdapter() {      
        @Override 
        public void mouseClicked(MouseEvent e) { 
         jTabbedPane.setSelectedIndex(index); 
        }    

       }); 

你得到想要的結果,但看起來這將是一個奇怪的解決方法。

0

覆蓋您的選項卡組件(在您的情況下爲JLabel)的contains方法返回false。

 public boolean contains(int x, int y) 
     { 
      return false; 
     } 
0

我的解決方案有點複雜,然後jzd的,我不知道它可以做得這麼幹淨。我喜歡你的解決方案,它認爲我有新的東西。謝謝,jzd。



public class JTabBug 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       final JTabbedPane jTabbedPane = new JTabbedPane(); 
       jTabbedPane.addTab("Red", new JLabel("Roses")); 
       jTabbedPane.addTab("Blue", new JLabel("Skies")); 
       jTabbedPane.addTab("Green", new JLabel("Grass")); 

       for(int i = 0; i < jTabbedPane.getTabCount(); i++) 
       { 
        final JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i)); 

        tabComponent.addMouseMotionListener(new MouseMotionAdapter() 
        { 
         @Override 
         public void mouseDragged(MouseEvent e) 
         { 
          System.out.println("tabComponent dragging"); 
         } 
        }); 
        jTabbedPane.setTabPlacement(JTabbedPane.LEFT); 

        tabComponent.addMouseListener(new MouseAdapter() 
        { 
         @Override 
         public void mousePressed(MouseEvent e) 
         { 
          int x = tabComponent.getLocationOnScreen().x - jTabbedPane.getLocationOnScreen().x; 
          int y = tabComponent.getLocationOnScreen().y - jTabbedPane.getLocationOnScreen().y; 
          MouseEvent me = new MouseEvent(
            (JLabel)e.getSource(), 
            e.getID(), e.getWhen(), e.getModifiers(), 
            x, y, 
            e.getLocationOnScreen(). x, e.getLocationOnScreen().y, 
            e.getClickCount(), e.isPopupTrigger(), 
            e.getButton());        
          jTabbedPane.getMouseListeners()[0].mousePressed(me); 
          System.out.println("tabComponent mousePressed e="+e); 
         } 
        }); 
        jTabbedPane.setTabComponentAt(i, tabComponent); 
       } 
       JFrame jFrame = new JFrame("Testing"); 
       jFrame.add(jTabbedPane); 
       jFrame.setSize(400, 500); 
       jFrame.setVisible(true); 
       jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      } 
     }); 
    } 
} 

享受,博羅

相關問題