边界布局管理器 BorderLayout
package com.JFrame;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 边界布局管理器 BorderLayout
*/
public class JfBo extends JFrame {
JButton jb1, jb2, jb3, jb4, jb5;
public static void main(String[] args) {
JfBo demo3 = new JfBo();
}
public JfBo() {
jb1 = new JButton("东方");
jb2 = new JButton("西方");
jb3 = new JButton("南方");
jb4 = new JButton("北方");
jb5 = new JButton("中部");
this.add(jb1, BorderLayout.EAST);
this.add(jb2, BorderLayout.WEST);
this.add(jb3, BorderLayout.SOUTH);
this.add(jb4, BorderLayout.NORTH);
this.add(jb5, BorderLayout.CENTER);
this.setTitle("图形界面");
this.setVisible(true);
this.setLocation(300, 300);
this.setSize(600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
流式布局管理器 FlowLayout
package com.JFrame;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 流式布局管理器 FlowLayout
*/
public class JfFl extends JFrame {
// 定义需要的组件
JButton[] button = new JButton[8];
// 主函数只需要调用
public static void main(String[] args) {
JfFl demo4 = new JfFl();
}
// 所有初始化放到构造函数
public JfFl() {
button[0] = new JButton("锅巴");
button[1] = new JButton("香蕉");
button[2] = new JButton("苹果");
button[3] = new JButton("火龙果");
button[4] = new JButton("开心果");
button[5] = new JButton("腰果");
button[6] = new JButton("巧克力");
button[7] = new JButton("西红柿");
// java默认边界布局管理器,添加布局管理器
this.setLayout(new FlowLayout());
// 左对齐
// this.setLayout(new JfFlowLayout(JfFlowLayout.LEFT));
// 右对齐
// this.setLayout(new JfFlowLayout(JfFlowLayout.RIGHT));
this.add(button[0]);
this.add(button[1]);
this.add(button[2]);
this.add(button[3]);
this.add(button[4]);
this.add(button[5]);
this.add(button[6]);
this.add(button[7]);
this.setTitle("流式布局FlowLayout");
this.setSize(600, 500);
this.setLocation(350, 300);
// 是否允许对边界进行放大缩小
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
网格布局管理器 GridLayout
package com.JFrame;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 网格布局管理器 GridLayout
*/
public class Jfgr extends JFrame {
// 定义需要的组件
JButton[] button = new JButton[9];
int s = 9;
public static void main(String[] args) {
Jfgr jfgr = new Jfgr();
}
public Jfgr() {
button[0] = new JButton("锅巴");
button[1] = new JButton("香蕉");
button[2] = new JButton("苹果");
button[3] = new JButton("火龙果");
button[4] = new JButton("开心果");
button[5] = new JButton("腰果");
button[6] = new JButton("巧克力");
button[7] = new JButton("西红柿");
button[8] = new JButton("饼干");
this.add(button[0]);
this.add(button[1]);
this.add(button[2]);
this.add(button[3]);
this.add(button[4]);
this.add(button[5]);
this.add(button[6]);
this.add(button[7]);
this.add(button[8]);
// 网格布局,三行三列
this.setLayout(new GridLayout(3, 3,12,13));
for (int i = 0; i < s; i++) {
this.add(button[i]);
}
this.setTitle("网格布局 GridLayout");
this.setSize(600, 500);
this.setLocation(350, 300);
// 是否允许对边界进行放大缩小
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
拆分窗口 列表
package com.JFrame;
import javax.swing.*;
/**
* 拆分窗口
* 列表
*/
public class Jchaifen extends JFrame {
JSplitPane cf;// 拆分 把屏幕分成两份
JList lb;// 列表
JLabel bq;// 标签
public static void main(String[] args) {
Jchaifen jliaotian = new Jchaifen();
}
public Jchaifen() {
String[] asd = { "游戏开发", "软件开发", "桌面开发", "动画制作", "影视后期", "室内设计" };
lb = new JList(asd);
bq = new JLabel(new ImageIcon("imgage/zwj.jpg"));
cf = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lb, bq);// 左右拆分
cf.setOneTouchExpandable(true);// 收缩左右
this.add(cf);
this.setTitle("聊天框");
this.setSize(600, 500);
this.setLocation(300, 300);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
登录(标签 文本 密码框)
package com.JFrame;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* 登录
* 标签 文本 密码框
*/
public class Jdenglu1 extends JFrame {
JPanel mb1, mb2, mb3;
JButton an1, an2;
JLabel bq1, bq2;// 标签
JTextField wbk;// 文本
JPasswordField mmk;// 密码框
public static void main(String[] args) {
Jdenglu1 jdenglu = new Jdenglu1();
}
public Jdenglu1() {
mb1 = new JPanel();
mb2 = new JPanel();
mb3 = new JPanel();
bq1 = new JLabel("用户名");// 标签
bq2 = new JLabel("密 码");
an1 = new JButton("登录");
an2 = new JButton("取消");
wbk = new JTextField(10);// 文本框 长度10
mmk = new JPasswordField(10);
this.setLayout(new GridLayout(3, 1));
mb1.add(bq1); mb1.add(wbk);
mb2.add(bq2); mb2.add(mmk);
mb3.add(an1); mb3.add(an2);
this.add(mb1);
this.add(mb2);
this.add(mb3);
this.setTitle("登录界面");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLocation(300, 280);
}
}
聊天窗口 (文本域)
package com.JFrame;
import java.awt.BorderLayout;
import javax.swing.*;
/**
* 聊天窗口 文本域
*/
public class Jliaotian extends JFrame {
JTextArea wby;// 文本域
JButton an;
JPanel mb;
JComboBox xlk;// 下拉框
JTextField wbk;// 文本
JScrollPane gd;// 滚动
public static void main(String[] args) {
Jliaotian jliaotian = new Jliaotian();
}
public Jliaotian() {
wby = new JTextArea();
mb = new JPanel();
String[] ad = { "小孙", "老猪", "师傅", "沙僧" };
xlk = new JComboBox(ad);
wbk = new JTextField(10);// 文本框长度10
an = new JButton("发送");
gd = new JScrollPane(wby);
mb.add(xlk); mb.add(wbk); mb.add(an);
this.add(gd);
this.add(mb, BorderLayout.SOUTH);
this.setTitle("聊天框");
this.setIconImage((new ImageIcon("imgage/qq.jpg")).getImage());
this.setSize(300, 200);
this.setLocation(300, 300);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
QQ登录界面
package com.JFrame;
import javax.swing.*;
import java.awt.*;
/**
* QQ登录界面
*/
public class JQQ extends JFrame {
// 上部
JLabel bq1;
// 中部
JTabbedPane xxk;// 选项卡
JPanel mb2, mb3, mb4;
JLabel bq2, bq3, bq4, bq5;
JTextField wbk;
JPasswordField mmk;
JButton an4;
JCheckBox fxk1, fxk2;
// 下部
JPanel mb1;
JButton an1, an2, an3;
public static void main(String[] args) {
JQQ jdenglu = new JQQ();
}
public JQQ() {
bq2 = new JLabel("QQ号码", JLabel.CENTER);
bq3 = new JLabel("QQ密码", JLabel.CENTER);
bq4 = new JLabel("忘记密码", JLabel.CENTER);
bq4.setFont(new Font("汉仪南宫体", Font.PLAIN, 16));
bq4.setForeground(Color.BLUE);
bq5 = new JLabel("<html><a href='www.baidu.com'>申请密码保护 </a>");
// 鼠标移动到网址链接,鼠标变成手势
bq5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
wbk = new JTextField();
mmk = new JPasswordField();
an4 = new JButton("清除密码");
// an4=new JButton(new ImageIcon("imgage/qq.jpg"));
// 上部
bq1 = new JLabel(new ImageIcon("imgage/2.jpg"));
mb1 = new JPanel();
an1 = new JButton("登录");
an2 = new JButton("取消");
an3 = new JButton("注册");
// 中部
xxk = new JTabbedPane();
mb2 = new JPanel();
mb3 = new JPanel();
mb3.setBackground(Color.orange);
mb4 = new JPanel();
mb4.setBackground(Color.green);
fxk1 = new JCheckBox("隐身登录");
fxk2 = new JCheckBox("记住密码");
xxk.add("普通用户", mb2);
xxk.add("VIP用户", mb3);
xxk.add("管理员", mb4);
mb2.setLayout(new GridLayout(3, 3));
mb1.add(an1);
mb1.add(an2);
mb1.add(an3);
mb2.add(bq2);
mb2.add(wbk);
mb2.add(an4);
mb2.add(bq3);
mb2.add(mmk);
mb2.add(bq4);
mb2.add(fxk1);
mb2.add(fxk2);
mb2.add(bq5);
this.add(mb1, BorderLayout.SOUTH);
this.add(bq1, BorderLayout.NORTH);
this.add(xxk, BorderLayout.CENTER);
this.setIconImage((new ImageIcon("imgage/qq.jpg")).getImage());
this.setTitle("登录界面");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 502);
this.setLocation(100, 280);
}
}
记事本(快捷键 二级菜单)
package com.JFrame;
import java.awt.BorderLayout;
import javax.swing.*;
public class Jtxt extends JFrame {
JMenuBar cd;// 菜单载体
JMenu cd1, cd2, cd3, cd4, cd5;// 菜单
JMenuItem cdx2, cdx3, cdx4, cdx5, cdx6, cdx7;// 菜单项
// 二级菜单
JMenu ej;
JMenuItem ej1, ej2;
// JMenuItem 是最终菜单,无下一级菜单
// JMenu 不是最终,有下一级菜单
JToolBar git;// 工具条
JButton an1, an2, an3, an4, an5, an6;
JTextArea wby;
JScrollPane gdt;// 滚动条
public static void main(String[] args) {
Jtxt jtxt = new Jtxt();
}
public Jtxt() {
git = new JToolBar();
an1 = new JButton(new ImageIcon("imgage/1.png"));
an1.setToolTipText("新建");
an2 = new JButton(new ImageIcon("imgage/1.png"));
an2.setToolTipText("打开");
an3 = new JButton(new ImageIcon("imgage/1.png"));
an3.setToolTipText("保存");
an4 = new JButton(new ImageIcon("imgage/1.png"));
an4.setToolTipText("剪贴");
an5 = new JButton(new ImageIcon("imgage/1.png"));
an5.setToolTipText("复制");
an6 = new JButton(new ImageIcon("imgage/1.png"));
an6.setToolTipText("粘贴");
cd = new JMenuBar();
cd1 = new JMenu("文件(F)");
cd1.setMnemonic('F');// 单引号
cd2 = new JMenu("编辑(E)");
cd2.setMnemonic('E');
cd3 = new JMenu("格式(V)");
cd3.setMnemonic('V');
cd4 = new JMenu("查看(O)");
cd4.setMnemonic('O');
cd5 = new JMenu("帮助(H)");
cd5.setMnemonic('H');
ej = new JMenu("新建");
ej1 = new JMenuItem("文件", new ImageIcon("imgage/1.png"));
ej2 = new JMenuItem("模板");
cdx2 = new JMenuItem("打开");
cdx3 = new JMenuItem("保存");
cdx3.setMnemonic('S');
cdx4 = new JMenuItem("另存为");
cdx5 = new JMenuItem("页面设置");
cdx6 = new JMenuItem("打印");
cdx7 = new JMenuItem("退出");
wby = new JTextArea();
gdt = new JScrollPane(wby);
git.add(an1);
git.add(an2);
git.add(an3);
git.add(an4);
git.add(an5);
git.add(an6);
ej.add(ej1);
ej.add(ej2);
cd1.add(ej);
cd1.add(cdx2);
cd1.add(cdx3);
cd1.add(cdx4);
cd1.addSeparator();
cd1.add(cdx5);
cd1.add(cdx6);
cd1.addSeparator();
cd1.add(cdx7);
cd.add(cd1);
cd.add(cd2);
cd.add(cd3);
cd.add(cd4);
cd.add(cd5);
this.setJMenuBar(cd);
this.add(git, BorderLayout.NORTH);
this.add(gdt);
this.setTitle("记事本");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(693, 527);
this.setLocation(300, 280);
}
}
下拉框 滚动条
package com.JFrame;
import java.awt.GridLayout;
import javax.swing.*;
/**
* 下拉框 滚动条
*/
public class Jxialakuang extends JFrame {
JPanel mb1, mb2;
JLabel bq1, bq2;
JComboBox xlk;// 下拉框
JList lb;// 列表
JScrollPane gd;//滚动
public static void main(String[] args) {
Jxialakuang jxialakuang = new Jxialakuang();
}
public Jxialakuang() {
mb1 = new JPanel();
mb2 = new JPanel();
bq1 = new JLabel("籍贯");
bq2 = new JLabel("学历");
String[] jg = { "北京", "上海", "四川", "山西", "云南", "福建", "江苏" };
xlk=new JComboBox(jg);
String[] xl= {"小学","初中","高中","大专","本科","硕士","博士"};
lb=new JList(xl);
lb.setVisibleRowCount(3);
gd=new JScrollPane(lb);
this.setLayout(new GridLayout(2, 1));
mb1.add(bq1);mb1.add(xlk);
mb2.add(bq2);mb2.add(gd);//和单选按钮不同,这里添加滚动
this.add(mb1);
this.add(mb2);
this.setTitle("用户调查");
this.setSize(200,190);
this.setLocation(300, 300);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
注册(单选框 复选框 )
package com.JFrame;
import java.awt.*;
import javax.swing.*;
/**
* 注册
* 单选框 复选框
*/
public class Jzhuce extends JFrame {
JPanel mb1, mb2, mb3;
JButton an1, an2;
JLabel bq1, bq2;// 标签
JCheckBox fxk1, fxk2, fxk3;// 复选框
JRadioButton dx1, dx2;// 单选
ButtonGroup anz;// 按钮组,默认只能选一个
public static void main(String[] args) {
Jzhuce jzhuce = new Jzhuce();
}
public Jzhuce() {
mb1 = new JPanel();
mb2 = new JPanel();
mb3 = new JPanel();
an1 = new JButton("注册");
an2 = new JButton("取消");
bq1 = new JLabel("特长");
bq2 = new JLabel("性别");
fxk1 = new JCheckBox("体育");
fxk2 = new JCheckBox("文艺");
fxk3 = new JCheckBox("唱歌");
dx1 = new JRadioButton("男");
dx2 = new JRadioButton("女");
anz=new ButtonGroup();
anz.add(dx1); anz.add(dx2);
this.setLayout(new GridLayout(3, 1));
mb1.add(bq1);
mb1.add(fxk1);
mb1.add(fxk2);
mb1.add(fxk3);
mb2.add(bq2);
mb2.add(dx1);
mb2.add(dx2);
mb3.add(an1);
mb3.add(an2);
this.add(mb1);
this.add(mb2);
this.add(mb3);
this.setTitle("用户注册");
this.setSize(300, 300);
this.setLocation(300, 300);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}