簡體   English   中英

如何垂直滾動到JPanel?

[英]How do i get vertical scrolling to JPanel?

我使用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;
     }

我想在另一個下面添加一個按鈕...(即我想要一個垂直滾動條)

即button1

 button2

   '

   '

但上面的代碼給我一行中的按鈕(即我得到水平滾動條)即button1 button2 ...

另一個問題是按鈕的大小。 使用btn.setSize()根本不影響大小......

有誰能夠幫助我?

您必須為面板使用適當的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);

看看本教程: 在容器中布置組件 (可視指南是一個很好的起點)

編輯
您也可以手動布置組件,即指定容器中每個組件的位置和大小。 為此,您必須將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]);

您需要為JPanel定義一個合適的LayoutManager ,它負責添加Component的方式。 默認的LayoutManagerFlowLayout ,它從左到右FlowLayout Component 要垂直布局Component您應該考慮使用BoxLayoutGridBagLayout

您必須為JPanel設置LayoutManager或使用Box(BoxLayout.Y_AXIS)。

對於按鈕的大小,請使用preferredSize

對於布局問題,您需要將布局管理器更改為執行垂直布局的布局管理器。 為了玩游戲,您可以像這樣使用BoxLayout:

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

如果讓布局管理器完成其工作,這會容易得多。

在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();
}

}

我希望這有幫助。

關於什么?

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

也可以使用SpringLayoutJPanel進行垂直滾動。 如果通過設置約束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替換這個很難或幾乎不可能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM