补充:先在窗口上加ScrollPane(AWT)或JScrollPane(Swing),再把按钮等控件加在ScrollPane或JScrollPane里。

解决方案 »

  1.   

    我试过用JScrollPane就是不行,希望有位兄台,可以用
    for 循环做一个可执行的小应用程序,示范一下!
    请问 xuancao(飞云) 能详细一点告诉我怎样设置状态属性来实现吗?
    谢谢
      

  2.   

    就如以下面程序滚动条就没有出现,请问怎样才能令滚动条出现呢?
    谢谢import java.awt.*;
    import javax.swing.*;
    public class ShowButton extends JFrame 
    {  JButton f1 = new JButton();
       JButton f2 = new JButton();
       JButton f3 = new JButton();
       JButton f4 = new JButton();
       JButton f5 = new JButton();
       JButton f6 = new JButton();
       JButton f7 = new JButton();
       JButton f8 = new JButton();
       JButton f9 = new JButton();
       private JButton buttonArray[]={f1,f2,f3,f4,f5,f6,f7,f8,f9};
       JScrollPane scrollPane = new JScrollPane();
       public static void main(String args[])
       {  new ShowButton();
       }
       public ShowButton() 
       {  try
          {  jbInit();
          }
          catch(Exception e) 
          {  e.printStackTrace();
          }  
       }
       private void jbInit() throws Exception 
       {  scrollPane.setLayout(null);
          this.getContentPane().add(scrollPane, BorderLayout.CENTER);
          for(int i=0;i<9;i++)
          {  buttonArray[i].setBounds(50,80*i+50,80,40);
             buttonArray[i].setText("Button"+i);
             scrollPane.add(buttonArray[i]);
          }
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setBounds(50,50,300,400);
          this.show();
       }
    }
      

  3.   

    呵呵,应该是
    scrollPane.getViewport().add(buttonArray[i]);
      

  4.   

    回复 Apocalypse(逍遥思辨),用
    scrollPane.getViewport().add(buttonArray[i]);
    还是不行,而且不能显示呢!
      

  5.   

    把scrollPane.setLayout(null)去掉试试
      

  6.   

    把scrollPane.setLayout(null)去掉也不行!
    埃,有没有人告诉我该怎么办呢?
    谢谢
      

  7.   

    很不好意思,我试了多次,只能去掉你的scrollPane.setLayout(null),并改代码为:
    import java.awt.*;
    import javax.swing.*;public class ShowButton extends JFrame 
    {  
    JButton f1 = new JButton();
    JButton f2 = new JButton();
    JButton f3 = new JButton();
    JButton f4 = new JButton();
    JButton f5 = new JButton();
    JButton f6 = new JButton();
    JButton f7 = new JButton();
    JButton f8 = new JButton();
    JButton f9 = new JButton(); private JButton[] buttonArray={f1,f2,f3,f4,f5,f6,f7,f8,f9};
    JPanel pane = new JPanel(); public static void main(String args[]){  
    new ShowButton();
    } public ShowButton(){  
    try{  
    jbInit();
    }catch(Exception e) {
    e.printStackTrace();
    }  
    } private void jbInit() throws Exception {

    for(int i=0;i<9;i++) {
    buttonArray[i].setText("Button"+i);
    pane.add(buttonArray[i]);
    } JScrollPane scrollPane=new JScrollPane(pane);

    // scrollPane.setPreferredSize(new Dimension(200,300));
    this.getContentPane().add(scrollPane, BorderLayout.CENTER);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(50,50,300,200);
    this.show();
    }
    }以后希望多用布局管理器,免得害的我差点谁不了觉.
      

  8.   

    1、将所有的Button加在JPanel中。
    2、把这个JPanel加在JScrollPane中。
    3、把JScrollPane的viewport指定为(1)中的JPanel就可以实现滚动了。
      

  9.   

    我将你的程序改过,没有问题了,愿对你有用。原代码如下:import java.awt.*;
    import javax.swing.*;
    public class ShowButton extends JFrame {
        JPanel jPanel1 = new JPanel();
        JButton f1 = new JButton();
        JButton f2 = new JButton();
        JButton f3 = new JButton();
        JButton f4 = new JButton();
        JButton f5 = new JButton();
        JButton f6 = new JButton();
        JButton f7 = new JButton();
        JButton f8 = new JButton();
        JButton f9 = new JButton();
        private JButton buttonArray[]={f1,f2,f3,f4,f5,f6,f7,f8,f9};
        JScrollPane scrollPane = new JScrollPane();
        public static void main(String args[]) {
            new ShowButton();
        }
        public ShowButton() {
            try{
                jbInit();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        private void jbInit() throws Exception {
            scrollPane.setAutoscrolls(true);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS) );
            for(int i=0;i<9;i++)
            {
               buttonArray[i].setBounds(50,80*i+50,80,40);
               buttonArray[i].setMaximumSize(new Dimension(300,100));
               buttonArray[i].setMinimumSize(new Dimension(300,100));
               buttonArray[i].setPreferredSize(new Dimension(300,100));
               buttonArray[i].setText("Button"+(i+1));
               jPanel1.add(buttonArray[i]);
            }
            scrollPane.getViewport().add(jPanel1, null);
            this.getContentPane().add(scrollPane, BorderLayout.CENTER);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setBounds(50,50,300,200);
            this.show();
       }
    }
      

  10.   

    真的非常谢谢hhjgx(何躝)的帮助,在这里还有两个小问题,
    就是 buttonArray[i].setBounds(50,80*i+50,80,40);
    在这里好像没有起作用,请问如何将 buttonArray[i] 定位?
    还有究竟buttonArray[i]在显示时的大小是由下面哪个语句确定的?
    buttonArray[i].setMaximumSize(new Dimension(300,100));
    buttonArray[i].setMinimumSize(new Dimension(300,100));
    buttonArray[i].setPreferredSize(new Dimension(300,100));
    再一次充心感谢hhjgx(何躝)
      

  11.   

    把这段
    jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS) );
    for(int i=0;i<9;i++)
      {
         buttonArray[i].setBounds(50,80*i+50,80,40);
         buttonArray[i].setMaximumSize(new Dimension(300,100));
         buttonArray[i].setMinimumSize(new Dimension(300,100));
         buttonArray[i].setPreferredSize(new Dimension(300,100));
         buttonArray[i].setText("Button"+(i+1));
         jPanel1.add(buttonArray[i]);
       }
    改为:
    jPanel1.setLayout(new XYLayout());
    for(int i=0;i<9;i++)
      {
         buttonArray[i].setText("Button"+(i+1));
         jPanel1.add(buttonArray[i],new XYConstraints(50,80*i+50,80,40));
      }
      

  12.   

    好多谢 namowen(寒号不已) 的指点,
    但是前面已经讨论过,这种方法是行不通的。
      

  13.   

    Button的Bounds是由Layout决定的,直接setBounds是没用的。
    还有,JScrollPane中的容器大小是由Scrollable接口的getPreferredScrollableViewportSize()来确定的,而不是像楼上诸位只是单纯的把Panel加到JScrollPane中就可以的。
      

  14.   

    farawayzheng_necas(遥远) 请原谅小第愚昧,
    或者可不可以请 farawayzheng_necas(遥远) 高抬贵手,
    把上面的程序修改一下,给小弟一个示范。
    谢谢
      

  15.   

    有没有人告诉我在BoxLayout()中怎么样定位,得到好像setBounds()的效果呢?