简体   繁体   English

如何将jbutton添加到jscrollpane

[英]How to add jbuttons to jscrollpane

hy, i am trying to add multiple jbuttons to the pane and want to scroll them. 嗨,我试图将多个jbutton添加到窗格中,并想要滚动它们。 so what i done is first added buttons to the jpanel than that jpanel to the jscrollpane than jscrollpane to the another jpanel and so on. 所以我要做的是先向jpanel添加按钮,然后向该jscrollpane添加该jpanel,然后再向另一个jpanel添加jscrollpane。 but the promblem i am getting is buttons is showing but there is not scroll bar to scroll the pane and see all the buttons. 但是我遇到的问题是按钮正在显示,但是没有滚动条来滚动窗格并查看所有按钮。

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class ICFTWindow extends JFrame

{
JPanel mainPanel,userlistPanel,chatPanel,userlistPanel2;
JButton checkButton,button2,button3,button4,button5,button6;
JScrollPane scrollpane;
JScrollBar scrollbar;

ICFTWindow()
{
    mainPanel = new JPanel();
    mainPanel.setLayout(null);

    userlistPanel = new JPanel(new FlowLayout());
    userlistPanel.setBounds(0,0,160,270);
    userlistPanel2 = new JPanel();
    userlistPanel2.setBounds(0,0,100,270);
    userlistPanel2.setLayout(new BorderLayout());

    scrollpane = new JScrollPane(userlistPanel);

    checkButton = new JButton();
    checkButton.setPreferredSize(new Dimension(90, 50));

    button2 = new JButton();
    button2.setPreferredSize(new Dimension(90, 50));

    button3 = new JButton();
    button3.setPreferredSize(new Dimension(90, 50));

    button4 = new JButton();
    button4.setPreferredSize(new Dimension(90, 50));

    button5 = new JButton();
    button5.setPreferredSize(new Dimension(90, 50));

    button6 = new JButton();
    button6.setPreferredSize(new Dimension(90, 50));

    userlistPanel.setBorder(BorderFactory.createLineBorder(Color.black));

    scrollpane.add(userlistPanel);
    mainPanel.add(userlistPanel);

    userlistPanel.add(button2);
    userlistPanel.validate();
    userlistPanel.add(checkButton);
    userlistPanel.validate();
    userlistPanel.add(button3);
    userlistPanel.validate();
    userlistPanel.add(button4);
    userlistPanel.validate();
    userlistPanel.add(button5);
    userlistPanel.validate();
    userlistPanel.add(button6);
    userlistPanel.validate();

    userlistPanel.validate();
    userlistPanel2.add(scrollpane);
    mainPanel.add(userlistPanel2);
    add(mainPanel);
    setSize(500,300);
    setVisible(true);

}

public static void main(String args[])
{
    new ICFTWindow();
}
}

Delete these two lines: 删除这两行:

scrollpane.add(userlistPanel);
mainPanel.add(userlistPanel);

You don't need to add the JPanel (for which you want a scroll bar) to the JScrollPane . 您无需将JPanel (您想要为其添加滚动条)添加到JScrollPane It is enough to send it to the constructor of the scroll pane. 只需将其发送到滚动窗格的构造函数即可。

After that, you have to add the scroll pane to the top level component and it will also add the panel contained within and put a scroll bar on it. 之后,您必须将滚动窗格添加到顶层组件,并且还将添加其中包含的面板并在其上放置滚动条。 See here for more details on how to use scroll panes. 有关如何使用滚动窗格的更多详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM