日期:2014-05-20  浏览次数:20709 次

关于Jscrollpane里面添加panel的问题,菜鸟求助
class AAA extends Frame implements ActionListener
{ int chuangKouGao,chuangKouKuan;
AAA(){
ScrollPane scrollPane1;
Panel panel2 = new Panel(new GridLayout(5,1));
Panel panel3 = new Panel(new GridLayout(5,1));
scrollPane1= new ScrollPane();
//scrollPane1.setVerticalScrollBarPolicy(ScrollPane.SCROLLBARS_ALWAYS);
windowsSize windowssize= new windowsSize();
panel2.setPreferredSize(new Dimension((int) (windowssize.getWidthSize()*0.7*0.3),(int) (windowssize.getHeightSize()*0.7)));
panel3.setPreferredSize(new Dimension((int) (windowssize.getWidthSize()*0.7*0.3),(int) (windowssize.getHeightSize()*0.7)));
scrollPane1.setPreferredSize(new Dimension((int) (windowssize.getWidthSize()*0.7*0.4),(int) (windowssize.getHeightSize()*0.7)));
panel2.setBackground(Color.green);
panel3.setBackground(Color.red);
scrollPane1.setBackground(Color.BLACK);
add(scrollPane1);
setBounds(100, 100, 100, 100);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){}
}
上面的代码用的是AWT组件 scrollPane1显示为黑色。我用了JAVA SWING 就不行了,为什么?JAVA SWING如下代码:
class AAA extends JFrame implements ActionListener
{ int chuangKouGao,chuangKouKuan;
AAA(){
JScrollPane scrollPane1;
JPanel panel2 = new JPanel(new GridLayout(5,1));
JPanel panel3 = new JPanel(new GridLayout(5,1));
scrollPane1= new JScrollPane();
//scrollPane1.setVerticalScrollBarPolicy(ScrollPane.SCROLLBARS_ALWAYS);
windowsSize windowssize= new windowsSize();
panel2.setPreferredSize(new Dimension((int) (windowssize.getWidthSize()*0.7*0.3),(int) (windowssize.getHeightSize()*0.7)));
panel3.setPreferredSize(new Dimension((int) (windowssize.getWidthSize()*0.7*0.3),(int) (windowssize.getHeightSize()*0.7)));
scrollPane1.setPreferredSize(new Dimension((int) (windowssize.getWidthSize()*0.7*0.4),(int) (windowssize.getHeightSize()*0.7)));
panel2.setBackground(Color.green);
panel3.setBackground(Color.red);
scrollPane1.setBackground(Color.BLACK);
        scrollPane1.add(panel3);
add(scrollPane1);

setBounds(100, 100, 100, 100);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){}
}
向scrollpane里加入panel3,为什么不显示(背景色即不是黑也不是红)?
swing java class

------解决方案--------------------
JScrollPane相当于一个容器,你要在里面先有个画布panel之类的才可以显示

楼主可以去看下 api ,里面有详细的说明 JScrollPane 的用法