谁能帮我解决问题,分就给谁。两种情况选其一都行。1,我下面的代码,谁能让滚动条显示出来。。2,不用我这种方式,实现这种效果:JScrollPane的viewport必须是一个Container的子类,然后不能以它的内容(如JTextArea就是它中间的文字)增加到一定程度而挤出滚动条。而是要用Container的setSize方法,设置大小,当它的大小大于viewport时,滚动条显示意思就是说我需要在JScrollPane里头的面板里加组件,组件的位置是绝对布局,当显示的位置超出JScrollPane的范围,出滚动条。谁帮忙解决了 分不是问题!
我目前写的测试代码,没出滚动条public class MFrame extends JFrame
{
public static void main(String[] args)
{
new MFrame().setVisible(true);
}
public MFrame()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
JScrollPane scrollPane=new JScrollPane();
SPanel sp=new SPanel();
sp.setLayout(null);
sp.setAutoscrolls(true);
JButton b=new JButton("123456");
sp.add(b);
sp.setBorder(BorderFactory.createLineBorder(Color.BLUE));
b.setBounds(250, 250, 80, 30);
JViewport port = new JViewport();
port.setLayout(null);
port.setView(sp);
sp.setBounds(0, 0, 400, 400) ;
scrollPane.setViewport(port) ;
scrollPane.setBorder(BorderFactory.createLineBorder(Color.RED));
this.setLayout(new BorderLayout());
this.getContentPane().add(scrollPane,BorderLayout.CENTER);
}
}class SPanel extends JPanel implements Scrollable
{
private JScrollPane pane;
public void setParentScrollPane(JScrollPane pane){
this.pane=pane;
} public Dimension getPreferredScrollableViewportSize()
{
System.out.println(1);
return this.getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
System.out.println(2);
return 20;
}
public boolean getScrollableTracksViewportHeight() {
System.out.println(3);
return true ;
}
public boolean getScrollableTracksViewportWidth() {
System.out.println(4);
return true ;
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
System.out.println(5);
return 20;
}}
我目前写的测试代码,没出滚动条public class MFrame extends JFrame
{
public static void main(String[] args)
{
new MFrame().setVisible(true);
}
public MFrame()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
JScrollPane scrollPane=new JScrollPane();
SPanel sp=new SPanel();
sp.setLayout(null);
sp.setAutoscrolls(true);
JButton b=new JButton("123456");
sp.add(b);
sp.setBorder(BorderFactory.createLineBorder(Color.BLUE));
b.setBounds(250, 250, 80, 30);
JViewport port = new JViewport();
port.setLayout(null);
port.setView(sp);
sp.setBounds(0, 0, 400, 400) ;
scrollPane.setViewport(port) ;
scrollPane.setBorder(BorderFactory.createLineBorder(Color.RED));
this.setLayout(new BorderLayout());
this.getContentPane().add(scrollPane,BorderLayout.CENTER);
}
}class SPanel extends JPanel implements Scrollable
{
private JScrollPane pane;
public void setParentScrollPane(JScrollPane pane){
this.pane=pane;
} public Dimension getPreferredScrollableViewportSize()
{
System.out.println(1);
return this.getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
System.out.println(2);
return 20;
}
public boolean getScrollableTracksViewportHeight() {
System.out.println(3);
return true ;
}
public boolean getScrollableTracksViewportWidth() {
System.out.println(4);
return true ;
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
System.out.println(5);
return 20;
}}
JScrollPane scrollPane=new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);