如何在java中添加滚动条到面板,没有borderlayout?
问题描述:
这里是我的代码:如何在java中添加滚动条到面板,没有borderlayout?
//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)
);*/
}
可以看到,标签面板的高度比灰色面板的高度更大。 所以我想在灰色面板的右侧找到一个滚动条,它可以上下滚动标签面板(位于灰色面板上)。但它只显示标签面板,并没有滚动条! 我可以做到这一点,如果我设置布局边框,而不是ownlayout(),但我想要一个免费的设计,而不是borderlayout。 请帮助我一些例子!
我建议你先阅读Swing教程(http://download.oracle.com/javase/tutorial/uiswing/TOC.html)。有关于使用布局管理器的章节以及特别介绍应该如何帮助的“如何使用滚动窗格”。 – camickr 2011-03-10 21:19:01
另外,通过这种方式混合使用AWT('Panel')和Swing('JScrollBar')组件通常不是一个好主意。 – trashgod 2011-03-11 01:36:32
比我应该添加到面板,如果不是JScrollBar? – victorio 2011-03-11 10:14:09