/*
* 功能:多种布局管理器
*/
package com.test11;
import java.awt.*;

import javax.swing.*;
public class Demo8_5 extends JFrame{

//定义组件
JPanel jp1,jp2;
JButton jb1,jb2,jb3,jb4,jb5,jb6;
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo8_5 demo8_5=new Demo8_5();

}

//构造函数
public Demo8_5()
{
//创建组件
//JPanel默认布局是FlowLayout
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);

//把Panel加入到JFrame

this.add(jp1,BorderLayout.NORTH);
this.add(jb6,BorderLayout.CENTER);
this.add(jp2,BorderLayout.SOUTH);

this.setSize(300, 200);
this.setLocation(200, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//显示窗体
this.setVisible(true);
}
运行结果:


}

 

package com.test11;
import java.awt.*;
import javax.swing.*;
public class Demo8_6 extends JFrame{

//定义组件
JPanel jp1,jp2,jp3;
JLabel jlb1,jlb2;
JButton jb1,jb2;
JTextField jtf1;
JPasswordField jpf1;

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

//构造函数
public Demo8_6()
{
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();

jlb1=new JLabel("用户名");
jlb2=new JLabel("密 码");
jb1=new JButton("登录");
jb2=new JButton("取消");

jtf1=new JTextField(10);
jpf1=new JPasswordField(10);

//设置布局管理
this.setLayout(new GridLayout(3,1));

//加入各个组件
jp1.add(jlb1);
jp1.add(jtf1);

jp2.add(jlb2);
jp2.add(jpf1);

jp3.add(jb1);
jp3.add(jb2);

//加入到JFrame
this.add(jp1);
this.add(jp2);
this.add(jp3);

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

this.setTitle("登录系统");
}
}

运行结果:

/*
* 功能:复选框和单选框案例
*/
package com.test11;
import java.awt.*;
import javax.swing.*;

public class Demo8_7 extends JFrame{

JPanel jp1,jp2,jp3;
JButton jb1,jb2;
JLabel jl1,jl2;
JCheckBox jcb1,jcb2,jcb3;
JRadioButton jrb1,jrb2;
ButtonGroup bg;
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo8_7 demo8_6=new Demo8_7();
}

//构造函数
public Demo8_7()

{

//创建组件
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jl1=new JLabel("sports");
jl2=new JLabel("sex");
jb1=new JButton("sign in");
jb2=new JButton("cancel");

jcb1=new JCheckBox("basketball");
jcb2=new JCheckBox("soccer");
jcb3=new JCheckBox("baseball");

jrb1=new JRadioButton("man");
jrb2=new JRadioButton("woman");
//一定要把jrb1,jrb2放到一个ButtonGroup中管理
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);
jp2.add(jrb1);
jp2.add(jrb2);

jp3.add(jb1);
jp3.add(jb2);

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

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

this.setTitle("选择系统"); }
}

运行结果:

/*
* 功能:JComboBox/JScrollPane/JList
*/
package com.test11;

import javax.swing.*;
import java.awt.*;
public class Demo8_8 extends JFrame{
JPanel jp1,jp2;

JLabel jl1,jl2;

JComboBox jcb1;
JList jlist;
JScrollPane jsp;


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

//构造函数
public Demo8_8()
{
jp1=new JPanel();
jp2=new JPanel();

jl1=new JLabel("Homeplace");
jl2=new JLabel("sightseeing");

String []jg={"bj","sh","tj","mars"};
jcb1=new JComboBox(jg);

String []ly={"jgg","byd","ems","harbin"};
jlist=new JList(ly);
jlist.setVisibleRowCount(2);
jsp=new JScrollPane(jlist);
//设置你希望显示多少个选项

//设置布局
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

运行结果:

posted on 2012-08-03 17:37  Adonstein  阅读(228)  评论(0编辑  收藏  举报