简体   繁体   English

如何在Java中将滚动条添加到面板中,而没有borderlayout?

[英]How to add scrollbar to panel in java, without borderlayout?

Here is my code: 这是我的代码:

//this is a JPanel, the gray panel behind the A4 paper
public Panel(int w, int h) {  //w=624, h=600
    this.w = w;
    this.h = h;
    ownlayout();
    setLocation(0, 0);
    setSize(w,h);
    setBackground(Color.gray);

    JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL);
    vbar.setLocation(w-30,0);
    Tab tab = new Tab(w-30,842);
    //Tab is a JPanel too, this is the A4 paper
    add(tab);
    add(vbar);
}
private void ownlayout() {
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    /*layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, w, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, h, Short.MAX_VALUE)
    );*/
}

You can see, the Tab panel's height is bigger than gray Panel's height. 您会看到,“选项卡”面板的高度大于灰色“面板”的高度。 So I want, to get a scrollbar in the right side of the gray Panel, which can scroll the tab panel (which is on the gray Panel) up and down. 因此,我想在灰色面板的右侧获得一个滚动条,该滚动条可以上下滚动选项卡面板(在灰色面板上)。 But it only shows the tab panel, and there is no scrollbar! 但是它仅显示选项卡面板,并且没有滚动条! I can do this, if I set the layout border, and not the ownlayout(), but I want a free design, not borderlayout. 如果我设置布局边框,而不是ownlayout(),则可以这样做,但是我想要一个免费的设计,而不是borderlayout。 Please help me with some example! 请帮我举个例子!

JScrollPane thePane = new JScrollPane(yourBigComponent);
container.add(thePane);

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

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