0

我正在尝试将几个面板添加到 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);

有人可以帮我弄这个吗。提前致谢。

4

1 回答 1

3
  • 使用 standardLayoutManagers而不是AbsoluteLayout,则 Swing GUI 将连续或按比例调整大小JFrame

  • 用于GridLayout()在屏幕上final_panelJPanels可以有相同大小的情况下

  • 覆盖getPreferredSizefor JPanel, for inital 并且PreferredSize在屏幕上更正(final_panel比更大的维度JScrollPane

  • 需要覆盖,然后在屏幕上ScrollPanel.setPreferredScrollableViewportSize(new Dimension(int, int));调用JFrame.pack()初始和更正PreferredSize

  • 需要覆盖JScrollPane.getVerticalScrollBar().setUnitIncrement(int);自然滚动

于 2013-07-08T10:24:21.873 回答