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

时间:2022-09-11 11:36:43
我向jpanel动态添加了N个jtextfield,一行一个,并且在右侧添加了一个jscrollbar,怎么才能让它滚动显示看不到的jtextfield呢?就像netbeans里许多设置窗口一样,出现那种滚动条?

13 个解决方案

#1


一般它要和JViewPort绑定

#2


你的意思是自定义一个纵向的滚动条,然后自己去实现定义滚动条滚动的时候,内容如何变化?好像这是唯一的办法了,太麻烦了,哎。什么语言能实现这个比较方便呢?java做swing就是太不方便了。

#3


看来用jtable能相对方便点了,不过jtable显示数据又要定义许多东西,唉

#4


JScrollPane scroll = new JScrollPane(jpanel);
然后将scroll添加到你的框架中

#5


如ls 
to lz:
你用了JScrollBar 不知道JScrollPane?

#6


如4楼,用JScrollPane

#7


引用 4 楼 guofei_gf 的回复:
JScrollPane scroll = new JScrollPane(jpanel); 
然后将scroll添加到你的框架中

我试了,当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;
}
}

#8


在使用JScrollPane后,对JPanel使用其他的Layout,如GridLayout等,
或者使用Box box = Box.createVerticalBox();向里面加组件就行了

#9


//设panel是你的组件面板

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


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

#10


引用 9 楼 nj_dobetter 的回复:
//设panel是你的组件面板 

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


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


随便设置一个大小不行吗?非要根据实际的设置?我随便设置了一个不起作用

#11


引用 8 楼 Inhibitory 的回复:
在使用JScrollPane后,对JPanel使用其他的Layout,如GridLayout等, 
或者使用Box box = Box.createVerticalBox();向里面加组件就行了 

使用box倒是出来了,不过对jlabel宽度的设置完全不起作用,它已经充满了box的宽度了。我是说,它完全占满了横向的宽度了。对于jpanel,使用非null布局的话,位置的设置也无效了,难道使用null就不行了吗?为什么会不行呢?

#12


我想问个问题,我跟LZ的设计是一样的,不过我的PANEL的布局是FormLayout,我怎么能让滚动条跟着光标走呢

#13


该回复于2015-05-26 10:27:05被管理员删除

#1


一般它要和JViewPort绑定

#2


你的意思是自定义一个纵向的滚动条,然后自己去实现定义滚动条滚动的时候,内容如何变化?好像这是唯一的办法了,太麻烦了,哎。什么语言能实现这个比较方便呢?java做swing就是太不方便了。

#3


看来用jtable能相对方便点了,不过jtable显示数据又要定义许多东西,唉

#4


JScrollPane scroll = new JScrollPane(jpanel);
然后将scroll添加到你的框架中

#5


如ls 
to lz:
你用了JScrollBar 不知道JScrollPane?

#6


如4楼,用JScrollPane

#7


引用 4 楼 guofei_gf 的回复:
JScrollPane scroll = new JScrollPane(jpanel); 
然后将scroll添加到你的框架中

我试了,当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;
}
}

#8


在使用JScrollPane后,对JPanel使用其他的Layout,如GridLayout等,
或者使用Box box = Box.createVerticalBox();向里面加组件就行了

#9


//设panel是你的组件面板

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


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

#10


引用 9 楼 nj_dobetter 的回复:
//设panel是你的组件面板 

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


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


随便设置一个大小不行吗?非要根据实际的设置?我随便设置了一个不起作用

#11


引用 8 楼 Inhibitory 的回复:
在使用JScrollPane后,对JPanel使用其他的Layout,如GridLayout等, 
或者使用Box box = Box.createVerticalBox();向里面加组件就行了 

使用box倒是出来了,不过对jlabel宽度的设置完全不起作用,它已经充满了box的宽度了。我是说,它完全占满了横向的宽度了。对于jpanel,使用非null布局的话,位置的设置也无效了,难道使用null就不行了吗?为什么会不行呢?

#12


我想问个问题,我跟LZ的设计是一样的,不过我的PANEL的布局是FormLayout,我怎么能让滚动条跟着光标走呢

#13


该回复于2015-05-26 10:27:05被管理员删除