Swing 混合布局

案例一:Border边境边界

package swing;

/**
 * swing 混合布局
 */
import java.awt.*;
import javax.swing.*;

public class Myframe extends JFrame {
    // 准备工作
    JPanel jp1, jp2;// 面板
    JButton jb1, jb2, jb3, jb4, jb5, jb6;

    public static void main(String[] args) {
        Myframe myframe = new Myframe();
    }

    // 构造函数
    public Myframe() {
        jp1 = new JPanel();// 默认流式布局
        jp2 = new JPanel();
        jb1 = new JButton("西瓜");
        jb2 = new JButton("苹果");
        jb3 = new JButton("桃子");
        jb4 = new JButton("哈密瓜");
        jb5 = new JButton("蟠桃");
        jb6 = new JButton("玉米");
        // 设置布局 默认流式布局
        // 添加JPanel
        jp1.add(jb1);
        jp1.add(jb2);
        jp2.add(jb3);
        jp2.add(jb4);
        jp2.add(jb5);
        // 把JPanel放入JFrame中‘
        this.add(jp1, BorderLayout.NORTH);
        this.add(jb6, BorderLayout.CENTER);
        this.add(jp2, BorderLayout.SOUTH);
        this.setSize(300, 250);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }
}
View Code

 

案例二 登录界面

Label标签

文本框:JTextField

密码框:JPasswordField

标签:JLabel

package swing;

/**
 * swing 混合布局
 */
import java.awt.*;
import javax.swing.*;

public class Myframe extends JFrame {
    // 定义组件
    JPanel jp1, jp2, jp3;// 面板
    JLabel jl1, jl2, jl3;
    JTextField jtf1, jtf2;
    JPasswordField jp;
    JButton jb1, jb2, jb3, jb4, jb5, jb6;

    public static void main(String[] args) {
        Myframe myframe = new Myframe();
    }

    // 构造函数
    public Myframe() {
        jp1 = new JPanel();// 默认流式布局
        jp2 = new JPanel();
        jp3 = new JPanel();
        jl1 = new JLabel("用户名");
        jl2 = new JLabel("密    码");

        jb1 = new JButton("登录");
        jb2 = new JButton("注册");

        jtf1 = new JTextField(10);
        jp = new JPasswordField(10);
        // 设置布局管理
        this.setLayout(new GridLayout(3, 1));
        // 加入组件
        jp1.add(jl1);
        jp1.add(jtf1);
        jp2.add(jl2);
        jp2.add(jp);
        jp3.add(jb1);
        jp3.add(jb2);
        //
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.setSize(300, 150);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }
}
View Code

 

案例三:用户注册界面

 

package swing;

/**
 * swing 混合布局
 */
import java.awt.*;
import javax.swing.*;

public class Myframe extends JFrame {
    // 定义组件
    JPanel jp1, jp2, jp3;// 面板
    JLabel jl1, jl2, jl3;
    JTextField jtf1, jtf2;
    JPasswordField jp;
    JButton jb1, jb2, jb3, jb4, jb5, jb6;
    JCheckBox jcb1, jcb2, jcb3;
    JRadioButton jrb1, jrb2;
    ButtonGroup bg;

    public static void main(String[] args) {
        Myframe myframe = new Myframe();
    }

    // 构造函数
    public Myframe() {
        jp1 = new JPanel();// 默认流式布局
        jp2 = new JPanel();
        jp3 = new JPanel();
        jl1 = new JLabel("你喜欢的运动");
        jl2 = new JLabel("你的性别");

        jb1 = new JButton("注册用户");
        jb2 = new JButton("取消注册");

        jcb1 = new JCheckBox("足球");
        jcb2 = new JCheckBox("篮球");
        jcb3 = new JCheckBox("排球");

        jrb1 = new JRadioButton("男");
        jrb2 = new JRadioButton("女");
        ButtonGroup bg = new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);
        // 设置布局管理
        this.setLayout(new GridLayout(3, 1));
        // 加入组件
        jp1.add(jl1);
        jp1.add(jcb1);
        jp1.add(jcb2);
        jp1.add(jcb3);

        jp2.add(jl2);

        jp3.add(jrb1);
        jp3.add(jrb2);
        //
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.setSize(300, 150);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }
}
View Code

 案例四:

package swing;

/**
 * swing 混合布局
 */
import java.awt.*;
import javax.swing.*;

public class Myframe extends JFrame {
    // 定义组件
    JPanel jp1, jp2, jp3;// 面板
    JLabel jl1, jl2, jl3;
    JTextField jtf1, jtf2;
    JPasswordField jp;
    JButton jb1, jb2, jb3, jb4, jb5, jb6;
    // JCheckBox jcb1, jcb2, jcb3;
    JRadioButton jrb1, jrb2;
    ButtonGroup bg;
    JComboBox jcb1;
    JScrollPane jsp;
    JList jlist;

    public static void main(String[] args) {
        Myframe myframe = new Myframe();
    }

    // 构造函数
    public Myframe() {
        jp1 = new JPanel();// 默认流式布局
        jp2 = new JPanel();

        jl1 = new JLabel("你的籍贯");
        jl2 = new JLabel("旅游地点");

        String[] jg = { "北京", "上海", "广州", "深圳" };
        jcb1 = new JComboBox(jg);

        String[] dd = { "长城", "天安门", "九寨沟", "庐山", "香港" };
        jlist = new JList(dd);

        jsp = new JScrollPane(jlist);
        // 设置显示多少选项

        jlist.setVisibleRowCount(2);
        // 设置布局管理
        this.setLayout(new GridLayout(3, 1));
        // 加入组件
        jp1.add(jl1);
        jp1.add(jcb1);

        jp2.add(jl2);
        jp2.add(jlist);

        //
        this.add(jp1);
        this.add(jp2);

        this.setSize(300, 300);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }
}
View Code

package swing;

/**
 * swing 混合布局
 */
import java.awt.*;
import javax.swing.*;

public class Myframe extends JFrame {
    // 定义组件
    JPanel jp1, jp2, jp3;// 面板
    JLabel jl1, jl2, jl3;
    JTextField jtf1, jtf2;
    JPasswordField jp;
    JButton jb1, jb2, jb3, jb4, jb5, jb6;
    // JCheckBox jcb1, jcb2, jcb3;
    JRadioButton jrb1, jrb2;
    ButtonGroup bg;
    JComboBox jcb1;
    JScrollPane jsp;
    JList jlist;

    public static void main(String[] args) {
        Myframe myframe = new Myframe();
    }

    // 构造函数
    public Myframe() {
        jp1 = new JPanel();// 默认流式布局
        jp2 = new JPanel();

        jl1 = new JLabel("你的籍贯");
        jl2 = new JLabel("旅游地点");

        String[] jg = { "北京", "上海", "广州", "深圳" };
        jcb1 = new JComboBox(jg);

        String[] dd = { "长城", "天安门", "九寨沟", "庐山", "香港" };
        jlist = new JList(dd);

        jsp = new JScrollPane(jlist);
        // 设置显示多少选项

        jlist.setVisibleRowCount(2);
        // 设置布局管理
        this.setLayout(new GridLayout(3, 1));
        // 加入组件
        jp1.add(jl1);
        jp1.add(jcb1);

        jp2.add(jl2);
        jp2.add(jsp);

        //
        this.add(jp1);
        this.add(jp2);

        this.setSize(300, 300);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }
}
View Code

 

posted @ 2019-04-16 22:49  Hello_World2020  阅读(263)  评论(0编辑  收藏  举报