【问题标题】:How do i get vertical scrolling to JPanel?如何垂直滚动到 JPanel?
【发布时间】:2019-06-18 09:03:47
【问题描述】:

我使用swing在java中编写了一个代码,这样我就可以在JPanel中添加一个JscrollPane,然后我会以垂直方式将固定大小的按钮添加到JPanel中

    JPanel panel=new JPanel();
    panel.setBackground(Color.WHITE);

    int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; 
    JScrollPane jsp=new JScrollPane(panel,v,h);
    jsp.setPreferredSize(new Dimension(600,600));
    jsp.setBounds(150,670,850,200);
    frame.add(jsp);

然后我在运行时向它添加按钮。

     for(i=0;i<n;i++) 
    {
         button[i]=new JButton();
         button[i].setBounds(20,y,120,120);
         button[i].setSize(120,120);
         button[i].setToolTipText(file[i].toString());       
         button[i].setIcon(Icon);
         panel.add(button[i]);   
         y=y+140;
     }

我想在另一个下方添加一个按钮...(即我想要一个垂直滚动条)

即按钮1

 button2

   '

   '

但上面的代码给了我一行按钮(即我得到了水平滚动条) 即按钮1按钮2...

另一个问题是按钮的大小。使用 btn.setSize() 根本不会影响大小...

谁能帮帮我?

【问题讨论】:

  • 我觉得要么这个问题的标题是错误的,要么接受的答案是错误的。寻找一种将 JPanel 添加到 JScrollPane 的方法......我没有看到任何地方提到 JScrollPane。只是不同的布局,据我所知没有添加任何滚动条。

标签: java swing


【解决方案1】:

您必须为面板使用适当的 Layoutmanager,例如 GridLayout、Boxlayout 或 GridBagLayout。
这取决于您还想在面板中添加什么。

GridLayout 更易于使用 IMO:

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));  // any number of rows, 1 column
...
    panel.add(button[i]);

BoxLayout 几乎一样简单:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
...
    panel.add(button[i]);

GridBagLayout 更强大,允许多列,跨多个单元格的组件,...需要一个 GridBagConstraints 来添加元素:

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints(
    0, RELATIVE,    // x = 0, y = below previous element
    1, 1,           // cell width = 1, cell height = 1
    0.0, 0.0        // how to distribute space: weightx = 0.0, weighty = 0,0 
    GridBagConstraints.CENTER,  // anchor
    GridBagConstraints.BOTH,    // fill
    new Insets(0, 0, 0, 0),     // cell insets
    0, 0);          // internal padding
...
    panel.add(button[i], constraints);

看看这个教程:Laying Out Components Within a Container(视觉指南是一个很好的起点)

编辑
你也可以手工布局组件,即指定每个组件在容器中的位置和大小。为此,您必须将 LayoutManager 设置为 null,以便删除默认管理器。

JPanel panel = new JPanel();
panel.setLayout(null);
...
    button[i].setLocation(x, y);
    button[i].setSize(width, heigth);
    // OR button[i].setBounds(x, y, width, height);
    panel.add(button[i]);

【讨论】:

    【解决方案2】:

    您需要为您的JPanel 定义一个合适的LayoutManager,它负责添加到其中的Components 的定位方式。默认LayoutManagerFlowLayout,它从左到右布置Component。对于垂直布局Components,您应该考虑使用BoxLayoutGridBagLayout

    【讨论】:

      【解决方案3】:

      您必须为 JPanel 设置 LayoutManager 或使用 Box(BoxLayout.Y_AXIS)。

      【讨论】:

        【解决方案4】:

        对于按钮的大小,请使用preferredSize

        对于您的布局问题,您需要将布局管理器更改为执行垂直布局的管理器。为了玩耍,您可以像这样使用 BoxLayout:

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        

        【讨论】:

          【解决方案5】:

          如果您让layout manager 完成它的工作,这会容易得多。

          在 Swing 中,组件在其他组件(例如面板)上的布局方式是使用布局管理器。

          它用于避免每次容器组件调整大小或添加新组件时都必须计算所有组件的坐标。

          有不同的布局管理器,这里你需要的是BoxLayout

          通过使用此布局,您无需指定按钮位置,也无需指定其大小。布局管理器查询每个组件并使用该信息将它们放置在正确的位置和大小。

          比如下面的框架

          创建了这个(你的修改版本)代码:

          import javax.swing.*;
          import java.awt.*;
          public class ScrollTest {
          
              private JPanel panel;
              private Icon[] icons = new Icon[3];
          
          
              public void main() {
                  panel =new JPanel();
          
                  // Use top to bottom layout in a column
                  panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ));
          
          
                  panel.setBackground(Color.WHITE);
          
                  int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
                  int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; 
                  JScrollPane jsp=new JScrollPane(panel,v,h);
                  jsp.setPreferredSize(new Dimension(600,600));
                  jsp.setBounds(150,670,850,200);
                  JFrame frame = new JFrame();
                  frame.add(jsp); 
          
                  // my addition to load sample icons
                  loadImages();
                  // simulate dynamic buttons 
                  addButtons();
          
                  frame.pack();
                  frame.setVisible( true );
          
              }
              void loadImages() {
                  icons[0] = new ImageIcon( "a.png" );
                  icons[1] = new ImageIcon( "b.png" );
                  icons[2] = new ImageIcon( "c.png" );
              }
          
              void addButtons() {
                  for( int i = 0 ; i < icons.length ; i++ ) {
                     JButton button = new JButton();
                     Icon icon = icons[i];
                     button.setIcon( icon );
                     // Set the button size to be the same as the icon size
                     // The preferred size is used by the layout manager
                     // to know what the component "better" size is.
                     button.setPreferredSize( new Dimension( icon.getIconWidth(),
                                                             icon.getIconHeight() ) );
                     // This is IMPORTANT. The maximum size is used bythe layout manager 
                     // to know "how big" could this component be.
                     button.setMaximumSize( button.getPreferredSize() );
                     panel.add( button );
                  }
          }
          public static void main( String ... args ) { 
              new ScrollTest().main();
          }
          

          }

          我希望这会有所帮助。

          【讨论】:

            【解决方案6】:

            还可以使用SpringLayout 获得JPanel 的垂直滚动。可以通过设置约束SpringLayout.SOUTH 来定义面板的垂直尺寸。可以这样完成:

            JPanel panel = new JPanel();
            SpringLayout panelLayout = new SpringLayout();
            panel.setLayout(panelLayout);
            
            // Adding components to the panel here
            // .....
            
            // That's what defines panel's exact size and makes its scrolling possible
            panelLayout.putConstraint(SpringLayout.SOUTH, panel, 0,
                    SpringLayout.SOUTH, lastComponentOfThePanel);
            
            JScrollPane panelScrollPane = new JScrollPane(panel);
            

            其中lastComponentOfThePanel 是面板底部的一个组件。

            希望这会对某人有所帮助。在我看来,SpringLayout 是一个非常强大的布局管理器,有时用GridBagLayout 替换它非常困难或几乎不可能。

            【讨论】:

              【解决方案7】:

              怎么样?

              JScrollPane scrollPane = new JScrollPane(yourpanel);
              container.add(scrollPane);
              

              【讨论】:

                猜你喜欢
                • 2021-11-13
                • 1970-01-01
                • 2014-08-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-01-05
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多