首页 > 代码库 > java 界面编程
java 界面编程
转载之u010345869的博客,这里学习下
/* * java复选框和单选按钮 */ import javax.swing.*; import java.awt.*; public class Test0 extends JFrame{ JList jlist; //列表框 JComboBox jcb; //下拉框 JPanel jp1, jp2; //面板 JLabel jlb1, jlb2; JScrollPane jsp; //滚动控件 //构造函数 public Test0(){ jp1 = new JPanel(); jp2 = new JPanel(); jlb1 = new JLabel("你的水平:"); String str1[] = {"巨坑", "菜鸟", "一般", "大神"}; jcb = new JComboBox(str1); jlb2 = new JLabel("选择英雄:"); String str2[] = {"盖仑", "艾希", "提莫", "赵信", "李青", "安妮"}; jlist = new JList(str2); jlist.setVisibleRowCount(2);//默认显示行数 jsp = new JScrollPane(jlist); jp1.add(jlb1); jp1.add(jcb); jp2.add(jlb2); jp2.add(jsp); //网格布局2行一列 this.setLayout(new GridLayout(2, 1)); this.add(jp1); this.add(jp2); this.setSize(200,200); this.setTitle("组件演示"); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Test0 test0 = new Test0(); } }
/* java图形界面 * 登录框 */ import java.awt.*; import javax.swing.*; public class Test1 extends JFrame{ //定义组件 JPanel jp1, jp2, jp3; JLabel jl1, jl2; JTextField jtf; JPasswordField jpf; JButton jb1, jb2; public static void main(String[] args) { Test1 test = new Test1(); } //构造函数 初始化组件 public Test1(){ jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jl1 = new JLabel("用户名"); jl2 = new JLabel("密 码"); jtf = new JTextField(10); jpf = new JPasswordField(10); jb1 = new JButton("登录"); jb2 = new JButton("取消"); jp1.add(jl1); jp1.add(jtf); jp2.add(jl2); jp2.add(jpf); jp3.add(jb1); jp3.add(jb2); this.add(jp1); this.add(jp2); this.add(jp3); this.setLayout(new GridLayout(3, 1)); this.setTitle("窗体名"); this.setSize(200,200); this.setLocation(100,200); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
/* java复选框与单选按钮 * 调查窗口 */ import java.awt.*; import javax.swing.*; public class Test2 extends JFrame{ //定义组件 JPanel jp1, jp2, jp3; JLabel jl1, jl2; JCheckBox jcb1, jcb2, jcb3; //复选框 JRadioButton jrb1, jrb2; //单选按钮 ButtonGroup bg; JButton jb1, jb2; public static void main(String[] args) { Test2 test = new Test2(); } //构造函数 public Test2(){ //创建组件 jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jl1 = new JLabel("你喜欢的运动:"); jl2 = new JLabel("你的性别:"); 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); jb1 = new JButton("注册"); jb2 = new JButton("取消"); //设置布局管理 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.setResizable(false);//固定窗体大小 this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
/* * java文本域和边界布局 * 聊天框 */ import java.awt.*; import javax.swing.*; public class Test3 extends JFrame { //定义组件 JTextArea jta = null; //文本域 JPanel jp = null; //面板 JScrollPane jsp =null; JComboBox jc = null; //组合框 JTextField jtf = null; //文本框 JButton jb = null; //按钮 public static void main(String[] args) { Test3 test3 = new Test3(); } //构造函数 public Test3(){ //创建组件 jta = new JTextArea(); jsp = new JScrollPane(jta); //文本域加入滚动条功能 jp = new JPanel(); String chatter[] = {"英 拉","普 京","奥巴马"}; jc = new JComboBox(chatter); jtf = new JTextField(10); jb = new JButton("发送"); //添加组件 this.add(jsp); jp.add(jc); jp.add(jtf); jp.add(jb); this.add(jsp); //加入实现滚动功能文本域 this.add(jp, BorderLayout.SOUTH); this.setSize(300, 200); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
/* * java 窗体图标设置 * 词霸 */ import java.awt.*; import javax.swing.*; public class Test4 extends JFrame{ //定义组件 JSplitPane jsp; //拆分窗格 JLabel jlb; JList jlist; public static void main(String[] args) { Test4 test4 = new Test4(); } //构造函数 public Test4(){ jsp = new JSplitPane(); String []words = {"ah","apple","array","all"}; jlist = new JList(words); jlb = new JLabel(new ImageIcon("images/aa.jpg")); jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jlist, jlb);//水平拆分 jsp.setOneTouchExpandable(true);//单击扩展面板 this.add(jsp); this.setIconImage(new ImageIcon(("images/t.gif")).getImage());//设置窗体图标 this.setTitle("词霸"); this.setSize(510, 260); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
/* * 选项卡窗格、面板组件、网格布局 */ import java.awt.*; import javax.swing.*; public class QLogin extends JFrame{ //定义组件 //北部区域 JLabel jl1; //放置图片 //南部区域 JButton jb1, jb2, jb3;//登录、取消、注册向导 JPanel jp1;//注意:panel面板,pane是窗格 //中部区域 JTabbedPane jtp;// 选项卡窗格 JPanel jp2, jp3, jp4; //QQ选项 JLabel jl2, jl3, jl4, jl5;//QQ号码、QQ密码、忘记密码、申请保护 JTextField jtf; //文本框 JPasswordField jpf;//密码框 JButton jb4;//清除号码 JCheckBox jcb1, jcb2;//隐身登录、记住密码 //手机选项 JLabel jl2a, jl3a, jl4a, jl5a;//手机号码 、QQ密码、忘记密码、申请保护 JTextField jtfa; //文本框 JPasswordField jpfa;//密码框 JButton jb4a;//清除号码 JCheckBox jcb1a, jcb2a;//隐身登录、记住密码 public static void main(String[] args) { QLogin testLogin = new QLogin(); } //构造函数 public QLogin(){ //创建组件 //北部区域 jl1 = new JLabel(new ImageIcon("images/QQ.jpg")); //中部区域 jtp = new JTabbedPane(); //选项卡窗格 //中部QQ JPanel1 jp2 = new JPanel(); jl2 = new JLabel("QQ号码:", JLabel.CENTER); jl3 = new JLabel("QQ密码:", JLabel.CENTER); jl4 = new JLabel("忘记密码", JLabel.CENTER); jl4.setFont(new Font("宋体", Font.PLAIN, 13)); jl4.setForeground(Color.blue); jl5 = new JLabel("<html><a href=http://www.mamicode.com/‘www.qq.com‘>申请密码保护