向jpanel里添加的组件多了后,如何出现滚动条呢

fxbird 2008-04-04 10:45:34
我向jpanel动态添加了N个jtextfield,一行一个,并且在右侧添加了一个jscrollbar,怎么才能让它滚动显示看不到的jtextfield呢?就像netbeans里许多设置窗口一样,出现那种滚动条?
...全文
669 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jetty_xiang 2009-02-25
  • 打赏
  • 举报
回复
我想问个问题,我跟LZ的设计是一样的,不过我的PANEL的布局是FormLayout,我怎么能让滚动条跟着光标走呢
fxbird 2008-04-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 guofei_gf 的回复:]
JScrollPane scroll = new JScrollPane(jpanel);
然后将scroll添加到你的框架中
[/Quote]
我试了,当panel是flow式布局的时候,我添加100个jlabel,会出现横向滚动条,但当我设成null布局的时候,应该出现的纵向滚动条却没有出现,这是为什么?我原以为jscrollpane只能添加jtextarea呢。
下面是源代码,放在有swt-desinger的eclipse可以观察:
package frame;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class check extends JFrame {

private JPanel panel;
private JScrollPane scrollPane;
private JButton button;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
check frame = new check();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the frame
*/
public check() {
super();
getContentPane().setLayout(new BorderLayout());
setBounds(100, 100, 561, 430);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().repaint();
getContentPane().add(getScrollPane());
genRecord(100);
//
}
protected JButton getButton() {
if (button == null) {
button = new JButton();
button.setText("New JButton");
button.setBounds(28, 10, 99, 23);
}
return button;
}

public void genRecord(int len){
int height=20;
int width=100;
for (int i = 1; i <= len; i++) {
JTextField tf=new JTextField(10);
tf.setText(i+","+(i-1)*(height+10));
getPanel().add(tf);

tf.setBounds(100, (i-1)*(height+10), width, height);
System.out.println(tf.getWidth());
}
}
/**
* @return
*/
protected JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane();
scrollPane.setViewportView(getPanel());
}
return scrollPane;

}
/**
* @return
*/
protected JPanel getPanel() {
if (panel == null) {
panel = new JPanel();
panel.setLayout(null);
}
return panel;
}
}
fxbird 2008-04-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Inhibitory 的回复:]
在使用JScrollPane后,对JPanel使用其他的Layout,如GridLayout等,
或者使用Box box = Box.createVerticalBox();向里面加组件就行了
[/Quote]
使用box倒是出来了,不过对jlabel宽度的设置完全不起作用,它已经充满了box的宽度了。我是说,它完全占满了横向的宽度了。对于jpanel,使用非null布局的话,位置的设置也无效了,难道使用null就不行了吗?为什么会不行呢?
fxbird 2008-04-06
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 nj_dobetter 的回复:]
//设panel是你的组件面板

JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(panel);


panel.setSize(maxWidth,maxHeight);//maxWidth,maxHeight超过scrollPane的大小时,滚动条就会出现.
//前提是先的根据panel中控件大小和位置算出maxWidth和maxHeight
[/Quote]

随便设置一个大小不行吗?非要根据实际的设置?我随便设置了一个不起作用
nj_dobetter 2008-04-06
  • 打赏
  • 举报
回复 1
//设panel是你的组件面板

JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(panel);


panel.setSize(maxWidth,maxHeight);//maxWidth,maxHeight超过scrollPane的大小时,滚动条就会出现.
//前提是先的根据panel中控件大小和位置算出maxWidth和maxHeight
Inhibitory 2008-04-06
  • 打赏
  • 举报
回复
在使用JScrollPane后,对JPanel使用其他的Layout,如GridLayout等,
或者使用Box box = Box.createVerticalBox();向里面加组件就行了
yiyi_wx 2008-04-05
  • 打赏
  • 举报
回复
如4楼,用JScrollPane
dracularking 2008-04-05
  • 打赏
  • 举报
回复
如ls
to lz:
你用了JScrollBar 不知道JScrollPane?
guofei_gf 2008-04-05
  • 打赏
  • 举报
回复
JScrollPane scroll = new JScrollPane(jpanel);
然后将scroll添加到你的框架中
fxbird 2008-04-05
  • 打赏
  • 举报
回复
看来用jtable能相对方便点了,不过jtable显示数据又要定义许多东西,唉
fxbird 2008-04-04
  • 打赏
  • 举报
回复
你的意思是自定义一个纵向的滚动条,然后自己去实现定义滚动条滚动的时候,内容如何变化?好像这是唯一的办法了,太麻烦了,哎。什么语言能实现这个比较方便呢?java做swing就是太不方便了。
dracularking 2008-04-04
  • 打赏
  • 举报
回复
一般它要和JViewPort绑定

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧