2

这是我的代码:

//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)
    );*/
}

可以看到,Tab 面板的高度大于灰色面板的高度。所以我想在灰色面板的右侧获得一个滚动条,它可以上下滚动标签面板(位于灰色面板上)。但它只显示标签面板,并没有滚动条!我可以这样做,如果我设置布局边框,而不是 ownlayout(),但我想要一个免费的设计,而不是边框​​布局。请帮我举个例子!

4

1 回答 1

6
JScrollPane thePane = new JScrollPane(yourBigComponent);
container.add(thePane);
于 2011-03-10T21:19:01.157 回答