这是我的代码:
//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(),但我想要一个免费的设计,而不是borderlayout。 请帮我举个例子!
答案 0 :(得分:6)
JScrollPane thePane = new JScrollPane(yourBigComponent);
container.add(thePane);