........   
           getContentPane().setLayout(new GridLayout(2,1));
           getContentPane().add("Center",split);
  getContentPane().add("South",buttons);
  quit.addActionListener(this);
  validate();
  pack();
  setVisible(true);
       .........
  不知道为什么把加上,界面布局不太好看.去掉却比较紧凑,为什么呀?
不是先要设置布局,再添加组件吗?
   谢谢各位了!

解决方案 »

  1.   

    原测试程序是这样的:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class  ScrollingTabImage extends JFrame implements ActionListener
    {  private JLabel imageLabel=new JLabel();
       private JButton quit=new JButton("Quit");
       private String fileName="scenery.jpg";
       public ScrollingTabImage()
       {  super("Scrolling Image");
          ImageIcon icon=new ImageIcon(fileName);
      imageLabel.setIcon(icon);
      JScrollPane scrollingLabel=new JScrollPane(imageLabel);
      scrollingLabel.setPreferredSize(new Dimension(240,180));
      JPanel info=new JPanel(new GridLayout(3,1));
      info.add(new JLabel("Image Name: "+fileName));
      info.add(new JLabel("Image Width: "+icon.getIconWidth()));
      info.add(new JLabel("Image Herght: "+icon.getIconHeight()));
      JSplitPane split=new JSplitPanew(JSplitPane.HORIZONTAL_SPLIT,scrollingLabel,info);
      split.setDividerSize(10);
      split.setOneTouchExpandable(true);
      split.setContinuousLayout(true);
      JPanel buttons=new JPanel(new FlowLayout());
      buttons.add(quit);
               getContentPane().setLayout(new BorderLayout());
      getContentPane().add("Center",split);
      getContentPane().add("South",buttons);
      quit.addActionListener(this);
      validate();
      pack();
      setVisible(true);
       }   public void actionPerformed(ActionEvent ae)
       {  System.exit(0);  } public static void main(String[] args) 
    {  ScrollingTabImage sti=new ScrollingTabImage();  }
    }为什么写不写getContentPane().setLayout(new BorderLayout());
    运行结果都一样呀?
    默认布局不是FlowLayout吗?
      

  2.   

    JPanel的默认布局是FlowLayout
    JFrame的默认布局是BorderLayout
      

  3.   

    把layout设置为null
    自己排版好了
      

  4.   

    pack()
    //java.awt.Window类的方法,自动调整此窗口的大小,以适合其子组件的首选大小和布局。按理说不应该是它的问题,不过没有绝对的事,你试试吧