2012-11-29 61 views
2

JScrollPane內部有20 JLabel s。還有浮動標籤加上JLayeredPane以及上面提到的JscrollPane如何使JLabel在JScrollPane內部的相同JComponent上浮動

要求是,當我點擊裏面JScrollPane它應該自動滾動以使JLabel與索引11 完全可見。當用戶滾動JScrollPane時,floatingLabel也應該開始浮動,高於JLabel 11,請參閱AdjustmentListener

問題[編輯]: 1.如何檢查JLabel 11變得完全或部分隱形?

SSCCE:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Point; 
import java.awt.event.AdjustmentEvent; 
import java.awt.event.AdjustmentListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.ArrayList; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JLayeredPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 

public class JScrollPaneTest { 
    protected ArrayList<JLabel> labels = new ArrayList<JLabel>(); 
    protected JLabel floatingLabel = new JLabel("floating"); 
    protected JFrame frame; 
    protected JScrollPane sPane; 
    protected JLayeredPane lPane; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new JScrollPaneTest(); 
      } 
     }); 
    } 

    public JScrollPaneTest() { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 

       lPane = new JLayeredPane() { 
        private static final long serialVersionUID = 1L; 

        @Override 
        public Dimension getPreferredSize() { 
         return new Dimension(800, 600); 
        } 
       }; 

       JPanel panel = new JPanel(); 

       panel.setLayout(new GridBagLayout()); 
       GridBagConstraints gbc = new GridBagConstraints(); 

       for (int i = 0; i < 20; i++) { 
        JLabel label = new JLabel(" | Label" + i + " | "); 
        panel.add(label, gbc); 
        labels.add(label); 
       } 

       panel.addMouseListener(new MouseAdapter(){ 
        public void mousePressed (MouseEvent me) { 
         JLabel label = labels.get(11); 
         label.scrollRectToVisible(label.getBounds()); 

         Point loc = label.getLocation(); 
         Point wrPos = SwingUtilities.convertPoint(label, loc, lPane); 
         floatingLabel.setLocation(wrPos); 
         floatingLabel.setVisible(true); 
        } 
       }); 

       sPane = new JScrollPane(panel) { 
        private static final long serialVersionUID = 1L; 

        @Override 
        public Dimension getPreferredSize() { 
         return new Dimension(600, 400); 
        } 
       }; 

       sPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() { 
        @Override 
        public void adjustmentValueChanged(AdjustmentEvent ae) { 
         System.out.println("adjustmentValueChanged: " + ae.getValue()); 

         //if label11 is not visible 
         //floatingLabel.setVisible(false); 
         //else floatingLabel.setLocation(); 
        } 
       }); 

       lPane.add(sPane, JLayeredPane.PALETTE_LAYER); 
       sPane.setBounds(100, 100, (int)sPane.getPreferredSize().getWidth(), (int)sPane.getPreferredSize().getHeight()); 

       lPane.add(floatingLabel, JLayeredPane.POPUP_LAYER); 
       floatingLabel.setBounds(100, 100, 50, 30); 
       floatingLabel.setBorder(BorderFactory.createLineBorder(Color.red)); 

       frame.getContentPane().add(lPane); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

解決的問題:

  1. 的JLabel 11不是點擊後完全可見,它是成功的一半裁剪
  2. 我無法計算出正確的位置floatingPanel上點擊
  3. 我不知道如何計算滾動的新位置
+0

不能使用視矩形? – StanislavL

+0

@StanislavL,問題#1,怎麼樣? –

+0

在SwingUtilities.invokeLater()中包圍您的調用,以便正確完成rect計算並確保rect中的標籤邊界。 – StanislavL

回答

3
label.scrollRectToVisible(new Rectangle(0,0,label.getWidth(), label.getHeight())); 

使用此相反在點擊

public void mousePressed (MouseEvent me) { 
    JLabel label = labels.get(11); 
    label.scrollRectToVisible(new Rectangle(0,0,label.getWidth(), label.getHeight())); 

    //Point loc = label.getLocation(); 
    Point loc = new Point(0,0); 
    Point wrPos = SwingUtilities.convertPoint(label, loc, lPane); 
    floatingLabel.setLocation(wrPos); 
    floatingLabel.setVisible(true); 
} 

public void adjustmentValueChanged(AdjustmentEvent ae) { 
    System.out.println("adjustmentValueChanged: " + ae.getValue()); 

    JLabel label = labels.get(11); 

    Point loc = new Point(0,0); 
    Point wrPos = SwingUtilities.convertPoint(label, loc, lPane); 
    floatingLabel.setLocation(wrPos); 
    floatingLabel.setVisible(true); 
} 
+0

(咳嗽)如何...代碼調整到領先的邊界? – kleopatra

+1

我太懶了......從IDE複製/粘貼:-) – StanislavL

+0

你太棒了!有用。 @kleopatra,你們知道如何檢查JLabel11變得完全/部分不可見以隱藏floatingLabel嗎? –