现在是这样; 有几个JTextField, 一个JPanel; 把这几个JTextFiled加入到JPanel; 但如果不加修改的话只是简单地从左到右排列. 现在想让JTextField能在垂直方向上对齐, 就类似这样排列:JTextFiled
JTextFiled
JTextFiled
JTextFiled而不是这样:
JTextFiled JTextFiled JTextFiled JTextFiled应该如何设置? 谢谢!

解决方案 »

  1.   

    用布局模式控制吧。
    JPanel jpl1 = new JPanel(new GridLayout(;根据你的窗口大小设置下吧。
      

  2.   

    使用网格布局
    import java.awt.GridLayout;import javax.swing.*;public class Test extends JFrame{
    JTextField f1 = new JTextField(12);
    JTextField f2= new JTextField(12);
    JTextField f3 = new JTextField(12);
    JPanel jp = new JPanel();
    public Test(){
    //panel 使用网格布局
    jp.setLayout(new GridLayout(3,1)); //网格布局 3行1列
    jp.add(f1);
    jp.add(f2);
    jp.add(f3);
    this.add(jp);
    this.pack();
    this.setVisible(true);

    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Test();
    }}
      

  3.   

    LZ,用下布局吧,就像楼上的那样,现在最好去看下java界面的布局管理器,很简单的,以后写java界面都需要的。。
      

  4.   


    import javax.swing.*;
    import java.awt.*;
    public class Box 
    {
    JFrame mainFrame ;
    JTextField[] text ;
    Container con;
    public Box()
    {
    mainFrame = new JFrame();
    con = mainFrame.getContentPane();
    BoxLayout box = new BoxLayout(con,BoxLayout.Y_AXIS);
    con.setLayout(box);
    text = new JTextField[6];
    for(int i=0;i<6;i++)
    {
    text[i] = new JTextField(8);
    con.add(text[i]);
    }
    mainFrame.setSize(300,300);
    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args)
    {
    new Box();
    }
    }
      

  5.   


    import javax.swing.*;
    import java.awt.*;
    public class Box 
    {
    JFrame mainFrame ;
    JTextField[] text ;
    JPanel panel;
    Container con;
    public Box()
    {
    mainFrame = new JFrame();
    con = mainFrame.getContentPane();
    con.setLayout(new BorderLayout());
    panel = new JPanel();
    BoxLayout box = new BoxLayout(panel,BoxLayout.Y_AXIS);
    panel.setLayout(box);
    text = new JTextField[6];
    for(int i=0;i<6;i++)
    {
    text[i] = new JTextField(8);
    panel.add(text[i]);
    }
    con.add(panel,BorderLayout.NORTH);
    mainFrame.setSize(300,300);
    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args)
    {
    new Box();
    }
    }
      

  6.   

    用GridLayout  布局就可以实现
    import java.awt.*;
    import javax.swing.*;public class testLayout extends JFrame{
       private JTextField filed1=new JTextField(9);
       private JTextField filed2=new JTextField(9);
       private JTextField filed3=new JTextField(9);
       private JTextField filed4=new JTextField(9);
       
       public testLayout(){
        Container c=getContentPane();
        c.setLayout(new GridLayout(4,1));
        c.add(filed1);
        c.add(filed2);
        c.add(filed3);
        c.add(filed4);
     }  
       public static void main(String[] args){
       testLayout test1=new testLayout();
       test1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       test1.setSize(400,300);
       test1.setVisible(true);
       test1.setTitle("Test Layout");
     }
    }  
      

  7.   

    import java.awt.*;
    import javax.swing.*;public class testLayout extends JFrame{
       private JTextField filed1=new JTextField(9);
       private JTextField filed2=new JTextField(9);
       private JTextField filed3=new JTextField(9);
       private JTextField filed4=new JTextField(9);
       
       public testLayout(){
        Container c=getContentPane();
        c.setLayout(new GridLayout(4,1));
        c.add(filed1);
        c.add(filed2);
        c.add(filed3);
        c.add(filed4);
     }  
       public static void main(String[] args){
       testLayout test1=new testLayout();
       test1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       test1.setSize(400,200);
       test1.setVisible(true);
       test1.setTitle("Test Layout");
     }
    }  
      

  8.   

    看代码费时间  直接说比较简单
    用布局管理器 
     
    JPanel jp = new JPanel();
    jp.setLayout(null);
    jp.setLayout(new GridLayout(4,1));