下面是我的测试代码,为什么这个代码显示不了滚动条?
当然,我的写的那个JScrollPane里面并不是装 JTextArea ,而是很多JButton和JLabel····
但我用这个测试类,即使JScrollPane里面放的是JTextArea ,也没有效果啊!!import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;public class JScrollPaneLabel extends JFrame { private static final long serialVersionUID = 1L;
private JScrollPane pan = null;
private JLabel lab = new JLabel("I am the label"); public JScrollPaneLabel() {
pan = new JScrollPane();
pan.setLayout(null); lab.setBounds(0, 0, 100, 20);
pan.add(lab);
pan.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
pan.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 300);
this.setVisible(true);
} public static void main(String[] args) {
new JScrollPaneLabel();
} }
当然,我的写的那个JScrollPane里面并不是装 JTextArea ,而是很多JButton和JLabel····
但我用这个测试类,即使JScrollPane里面放的是JTextArea ,也没有效果啊!!import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;public class JScrollPaneLabel extends JFrame { private static final long serialVersionUID = 1L;
private JScrollPane pan = null;
private JLabel lab = new JLabel("I am the label"); public JScrollPaneLabel() {
pan = new JScrollPane();
pan.setLayout(null); lab.setBounds(0, 0, 100, 20);
pan.add(lab);
pan.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
pan.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 300);
this.setVisible(true);
} public static void main(String[] args) {
new JScrollPaneLabel();
} }
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class JScrollPaneLabel extends JFrame
{
private static final long serialVersionUID = 1L;
private JScrollPane pan = null;
private JLabel lab = new JLabel("I am the label\nI am the label");
public JScrollPaneLabel()
{
pan = new JScrollPane();
//pan.setLayout(null); lab.setBounds(0, 0, 100, 20);
//pan.add(lab);
pan.setViewportView(lab); //不能用add,要用setViewportView
pan.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
pan.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 300);
this.setVisible(true);
} public static void main(String[] args)
{
new JScrollPaneLabel();
}
}
是这样的,它根据从数据库里面遍历表得到List,然后循环List,每次循环都在JScrollPanel上面加上几个JLabel和 JButton ,模拟的是显示信息,JButton在后面 ( 修改 和 删除)~~~ 我试着把那些加在JPanel里面,然后再把JPanel setViewportView到JScrollPanel上面,可是还是不行!
请求帮忙写一个代码,JScrollPanel上面是加很多组建的~~直到超出他的显示范围,并且能显示出滚动条~~· 谢谢各位大侠了!小弟调试了很多办法都不行···