JFrame with JscrollPane



我试图在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 连续或按比例调整大小

  • 在屏幕上所有JPanels都可以获得相同大小的情况下,将GridLayout()用于final_panel

  • JPanel覆盖getPreferredSize,为初始并在屏幕上更正PreferredSizefinal_panel的尺寸大于JScrollPane的尺寸)

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

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

相关内容

  • 没有找到相关文章

最新更新