import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;public class MyClass extends JFrame
{
  
   private JPanel panel = new JPanel();
      public MyClass()
   {
      setTitle("测试");
      panel.add(new JButton("1"));
      panel.add(new JButton("221"));
      panel.add(new JButton("3"));
      add(panel,BorderLayout.SOUTH);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLayout(new FlowLayout());
      setResizable(false);
     
     
      setSize(400,300);
      setVisible(true);
   }    public static void main(String[] args)
    {
       JFrame.setDefaultLookAndFeelDecorated(true);
       new MyClass();
    }
}上面这段程序的效果应该是按钮将沿窗体的底部从左至右排列啊!可是怎么得不到想要的结果啊?结果还是沿窗体的顶部从左至右排列.难道两个布局合在一起不起作用吗?

解决方案 »

  1.   

    import   javax.swing.JFrame; 
    import   javax.swing.JButton; 
    import   javax.swing.JPanel; 
    import   java.awt.BorderLayout; 
    import   java.awt.FlowLayout; public   class   MyClass   extends   JFrame 

        
          private   JPanel   panel   =   new   JPanel(); 
                public   MyClass() 
          { 
                setTitle("测试"); 
                panel.add(new   JButton("1")); 
                panel.add(new   JButton("221")); 
                panel.add(new   JButton("3")); 
                add(panel,BorderLayout.SOUTH); 
                setDefaultCloseOperation(EXIT_ON_CLOSE); 
                setLayout(new   FlowLayout(FlowLayout.LEFT));   //需要在这里设置一下就可以了
                setResizable(false); 
              
              
                setSize(400,300); 
                setVisible(true); 
          }         public   static   void   main(String[]   args) 
            { 
                  JFrame.setDefaultLookAndFeelDecorated(true); 
                  new   MyClass(); 
            } 
      

  2.   

    按有几个群,你不妨加进去,可以和大家一起讨论啊.........46986340,28039577,4804620               
    在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
      

  3.   

    import   javax.swing.JFrame; 
    import   javax.swing.JButton; 
    import   javax.swing.JPanel; 
    import   java.awt.BorderLayout; 
    import   java.awt.FlowLayout; public   class   MyClass   extends   JFrame 

        
          private   JPanel   panel   =   new   JPanel(); 
                public   MyClass() 
          { 
                setTitle("测试"); 
                panel.add(new   JButton("1")); 
                panel.add(new   JButton("221")); 
                panel.add(new   JButton("3")); 
                add(panel,BorderLayout.SOUTH); 
                setDefaultCloseOperation(EXIT_ON_CLOSE); 
                setLayout(new   FlowLayout(FlowLayout.LEFT));   //需要在这里设置一下就可以了
                setResizable(false); 
              
              
                setSize(400,300); 
                setVisible(true); 
          }         public   static   void   main(String[]   args) 
            { 
                  JFrame.setDefaultLookAndFeelDecorated(true); 
                  new   MyClass(); 
            } 
      

  4.   

      楼上的几位错了,你那样设置了也只是把FlowLayout布局的位置往左靠,并没有解决BorderLayout布局的问题啊,我现在关键是还要得到一个BorderLayout布局的设置效果.我就是要两个布局加在一起使用啊
      

  5.   

    import   javax.swing.JFrame; 
    import   javax.swing.JButton; 
    import   javax.swing.JPanel; 
    import   java.awt.BorderLayout; 
    import   java.awt.FlowLayout; public   class   MyClass   extends   JFrame 

        
          private   JPanel   panel   =   new   JPanel(); 
                public   MyClass() 
          { 
                setTitle("测试"); 
                 setLayout(new   BorderLayout());
                panel.add(new   JButton("1")); 
                panel.add(new   JButton("221")); 
                panel.add(new   JButton("3")); 
                add(panel,BorderLayout.SOUTH); 
                setDefaultCloseOperation(EXIT_ON_CLOSE); 
                
                setResizable(false); 
              
              
                setSize(400,300); 
                setVisible(true); 
          }         public   static   void   main(String[]   args) 
            { 
                  JFrame.setDefaultLookAndFeelDecorated(true); 
                  new   MyClass(); 
            } 
    } 这样就可以了,可是我想不通的是为什么FlowLayout布局不起作用
      

  6.   

    一楼代码就等价于这样吧add(panel);
    setLayout(new FlowLayout());layout被重置了