这可能是非常简单的事情,但我完全糊涂了。
当我在 Pane 中滚动时,我在 JLayeredPane
中有一个 JScrollPane
JLayeredPane
上面的内容根本不会重新绘制.
这里我有一个小例子,注意蓝色方 block 是如何完全没有被重绘的。
我是否完全误解了分层 Pane 的工作原理?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class LayeredTest extends JPanel{
public LayeredTest(){
JPanel content = new JPanel();
content.setBackground(Color.red);
content.setPreferredSize(new Dimension(2048, 2048));
content.setBounds(0, 0, 2048, 2048);
JPanel control = new JPanel();
control.setBackground(Color.blue);
control.setPreferredSize(new Dimension(200, 50));
control.setBounds(0, 0, 100, 50);
JScrollPane scroll = new JScrollPane(content);
scroll.setBounds(0, 0, 400, 400);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(400, 400));
layeredPane.add(control, 0);
layeredPane.add(scroll, 1);
this.add(layeredPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("Test - Very lulz");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.setSize(400, 400);
//Create and set up the content pane.
frame.setContentPane(new LayeredTest());
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
有什么想法吗?
最佳答案
您正在做的是将组件 control
和 scroll
添加到“默认”层,因此实际上它们仍然在同一层上。要将它们放在不同的图层上,您还需要指定图层编号。最好将组件放置在默认层(索引为 0 的最底部)和下一层(索引为 100 的“调色板”层)之间的某个位置。
因此,例如,要将组件放在第 50 层和第 51 层,请将组件添加到 layeredPane
的位置更改为:
layeredPane.add(scroll, 50, 0);
layeredPane.add(control, 51, 0);
这会将 scroll
放在第 50 层的位置 0,并将 control
放在第 51 层的位置 0。
关于java - JLayeredPane 中的 JScrollPane 没有重绘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7223320/