我的 JScrollPane 出现意外行为: 我的 ScrollPanel 充满了不同的面板 (需要透明度,因为最终我将在背景中显示图像而不仅仅是颜色)
我举了一个简单的例子来说明我的问题:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.border.Border;
public class ScrollPaneTest extends JPanel{
ScrollPaneTest(){
JPanel Content = new JPanel();
BoxLayout ColumnLayout = new BoxLayout(Content,BoxLayout.Y_AXIS);
Content.setLayout(ColumnLayout);
for(int i = 0 ; i < 10 ; i++){
JPanel pane = new JPanel();
JLabel elem = new JLabel("element "+i);
pane.setBackground(new Color(0,0,0,125));
pane.add(elem);
Content.add(pane);
}
Content.setBackground(Color.ORANGE);
JScrollPane scrollPane = new JScrollPane(Content);
scrollPane.setPreferredSize(new Dimension(100,100));
scrollPane.setBackground(new Color(0,0,0,250));
add(scrollPane);
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
JPanel bck = new JPanel();
bck.setBackground(Color.RED);
bck.add(new ScrollPaneTest());
f.add(bck);
f.setSize(200, 200);
f.setVisible(true);
}
}
这样你就可以看到当我滚动时,图形都乱了:/ 感谢您的帮助:p
最佳答案
我对您的问题有以下解决方案
当您滚动元素时,元素不会重新绘制,因此您需要在滚动滚动条时重新绘制它们。在该行后添加以下代码 JScrollPane scrollPane = new JScrollPane(Content);
它将起作用!
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
repaint();
}
});
关于java - 滚动 Pane 图形错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26073529/