在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);
}
});
}
}
解決的問題:
- 的JLabel 11不是點擊後完全可見,它是成功的一半裁剪
- 我無法計算出正確的位置floatingPanel上點擊
- 我不知道如何計算滾動的新位置
不能使用視矩形? – StanislavL
@StanislavL,問題#1,怎麼樣? –
在SwingUtilities.invokeLater()中包圍您的調用,以便正確完成rect計算並確保rect中的標籤邊界。 – StanislavL