我正在尝试向 JScrollpane 添加一些面板,然后再添加到框架,但我无法滚动。 这是代码。
first_panel = new JPanel();
first_panel.setBackground(Color.red);
second_panel = new JPanel();
second_panel.setBackground(Color.blue);
third_panel = new JPanel();
third_panel.setBackground(Color.green);
fourth_panel = new JPanel();
fourth_panel.setBackground(Color.red);
fifith_panel = new JPanel();
fifith_panel.setBackground(Color.blue);
six_panel = new JPanel();
six_panel.setBackground(Color.green);
final_panel = new JPanel();
final_panel.setLayout(null);
final_panel.setBackground(Color.gray);
final_panel.add(first_panel);
final_panel.add(second_panel);
final_panel.add(third_panel);
final_panel.add(fourth_panel);
final_panel.add(fifith_panel);
final_panel.add(six_panel);
first_panel.setBounds(10,10,200,100);
second_panel.setBounds(10,10,200,200);
third_panel.setBounds(10,10,200,300);
fourth_panel.setBounds(10,10,200,400);
fifith_panel.setBounds(10,10,200,500);
six_panel.setBounds(10,10,200,600);
panel_scroll = new JScrollPane(final_panel);
panel_scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
final_panel.setBounds(0,0,400,400);
// scroll_panel = new JPanel();
// scroll_panel.setLayout(null);
// scroll_panel.add(panel_scroll);
// panel_scroll.setBounds(0,0,400,400);
frame.getContentPane().add(panel_scroll);
有人可以帮我解决这个问题吗?提前致谢。
最佳答案
使用标准
LayoutManagers
而不是AbsoluteLayout
,那么 Swing GUI 将连续或按比例调整大小JFrame
使用
GridLayout()
对于final_panel
在所有JPanels
的情况下可以在屏幕上获得相同的尺寸覆盖
getPreferredSize
对于JPanel
,初始且正确PreferredSize
在屏幕上(final_panel
的尺寸大于JScrollPane
的尺寸)
需要 来覆盖
ScrollPanel.setPreferredScrollableViewportSize(new Dimension(int, int));
,然后调用JFrame.pack()
初始且正确PreferredSize
在屏幕上
需要 来覆盖
JScrollPane.getVerticalScrollBar().setUnitIncrement(int);
用于自然滚动
关于java - JFrame 与 JscrollPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17524114/