题目:设计一个界面,第一行含有三个按钮,第二行正中间含有一个按钮,第三行含有两个按钮。
这道题目不使用面板能不能做出来?怎么换行?

解决方案 »

  1.   

    可以啊,运用网格/网格包布局管理器就行,不过运用网格包管理器更强大,这是我用网格包写的:
    import java.awt.Button;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    public class Tt extends Frame {
    static Button b1,b2,b3,b4,b5,b6;
    static GridBagLayout gb=new GridBagLayout();
    GridBagConstraints gbc=new GridBagConstraints();

    public static void main(String[]args){
        Tt t=new Tt();
    t.setSize(500, 500);
    t.setLayout(gb);
    t.setVisible(true);
    t.run();

    }
    public Tt(){
    b1=new Button("按钮一");
    b2=new Button("按钮二");
    b3=new Button("按钮三");
    b4=new Button("按钮四");
    b5=new Button("按钮五");
    b6=new Button("按钮六");
    addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent evt){
       
    System.exit(0);
    }
    });
    }
    public  void run(){
    addComponent(b1,0,0,2,1);
    addComponent(b2,0,2,2,1);
    addComponent(b3,0,4,2,1);
    addComponent(b4,1,2,2,1);
    addComponent(b5,2,0,2,1);
    addComponent(b6,2,2,2,1);

    }
    public  void addComponent(Component c,int row,int col,int nrow,int ncol){
    gbc.gridx=col;
    gbc.gridy=row;
    gbc.gridheight=ncol;
    gbc.gridwidth=nrow;
    gb.setConstraints(c, gbc);
    add(c);
    }}
      

  2.   


    import java.awt.Button;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    public class Tt extends Frame {
    static Button b1,b2,b3,b4,b5,b6;
    static GridBagLayout gb=new GridBagLayout();
    GridBagConstraints gbc=new GridBagConstraints();

    public static void main(String[]args){
        Tt t=new Tt();
    t.setSize(500, 500);
    t.setLayout(gb);
    t.setVisible(true);
    t.run();

    }
    public Tt(){
    b1=new Button("按钮一");
    b2=new Button("按钮二");
    b3=new Button("按钮三");
    b4=new Button("按钮四");
    b5=new Button("按钮五");
    b6=new Button("按钮六");
    addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent evt){
       
    System.exit(0);
    }
    });
    }
    public  void run(){
    addComponent(b1,0,0,2,1);
    addComponent(b2,0,2,2,1);
    addComponent(b3,0,4,2,1);
    addComponent(b4,1,2,2,1);
    addComponent(b5,2,0,2,1);
    addComponent(b6,2,2,2,1);

    }
    public  void addComponent(Component c,int row,int col,int nrow,int ncol){
    gbc.gridx=col;
    gbc.gridy=row;
    gbc.gridheight=ncol;
    gbc.gridwidth=nrow;
    gb.setConstraints(c, gbc);
    add(c);
    }}
      

  3.   

    setLayout(null)
    然后自己布局,so easy的问题
      

  4.   

    //你最后两个是分两边吗?你看看吧。想怎么放都行
    public class JButtonTest extends JFrame {
    JButton bt1=new JButton("一一");
    JButton bt2=new JButton("一二");
    JButton bt3=new JButton("一三");
    JButton bt21=new JButton("二一");
    JButton bt31=new JButton("三一");
    JButton bt32=new JButton("三二");

    public JButtonTest(){
    this.setSize(300, 200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    init();
    }
    public void init(){
    JPanel panel=new JPanel();
    panel.setLayout(new GridBagLayout());
    panel.add(bt1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
    panel.add(bt2, new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
    panel.add(bt3, new GridBagConstraints(2, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
    panel.add(bt21, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
    panel.add(bt31, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
    panel.add(new JPanel(), new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
    panel.add(bt32, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
    this.add(panel);
    }
    public static void main(String [] args){
    JButtonTest bt=new JButtonTest();
    }
    }