Java swing(纯代码和含部分个人解析)插入png或gif图片的方法,切换界面功能的实现







package swing;
public class mains {
public static void main(String[] args) {
new swing();
}
}

package swing;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.ImageIcon;/*插图片时可上这个*/
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class shijian implements ActionListener, FocusListener {
static swing d = null;
/* 下面的是构造方法,无void */
public shijian(swing dl) {
d = dl;
}
@Override
/*
*
* actionperformed方法是ActionListener监听事件中的一个重写方法,
*
* 如果要求按钮执行一些动作,可以在这个方法中添加、修改、删除以及判断。
*/
public void actionPerformed(ActionEvent arg0) {
/* 当按钮中字符为"登录"时,就执行equals("登录")中的内容 */
if (arg0.getActionCommand().equals("已经注册过,可直接登录")) {
/* trim()函数移除字符串两侧的空白字符或其他预定义字符。 */
String uname = swing.uname.getText().trim();
String pwd = swing.pwd.getText().trim();
if (uname.isEmpty() || pwd.isEmpty()) {
JOptionPane.showMessageDialog(null, "用户名或密码为空");
} else {
if (uname.equals(yonghu.uname) && pwd.equals(yonghu.pwd)) {
JOptionPane.showMessageDialog(null, "用户名和密码输入正确,可以登录");
new chenggongjiemian();
d.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用户名或密码输入错误");
}
}
}
else if (arg0.getActionCommand().equals("没注册过,先注册")) {
JOptionPane.showMessageDialog(null, "没注册过,先注册");
} else {
int o = JOptionPane.showConfirmDialog(null, "是否确认关闭页面");
if (o == 0) {
System.exit(0);
}
}
}
/*
*
* arg是java主函数的入口参数,它是一个字符串数组变量 void focusGained(FocusEvent e):当获得焦点时发生.
*
* void focusLost(FocusEvent e):当失去焦点时发生.
*/
@Override
public void focusGained(FocusEvent arg0) {
if (arg0.getSource() == d.uname) {
System.out.println("选中了用户名框");
} else if (arg0.getSource() == d.pwd) {
System.out.println("选中了密码框");
} else if (arg0.getSource() == d.pwd1) {
System.out.println("选中了确认密码框");
}
}
@Override
public void focusLost(FocusEvent arg0) {
if (arg0.getSource() == d.uname) {
/* getSource获得所选 */
String uname = d.uname.getText();
if (uname.isEmpty()) {
d.lb_uname_panduan.setText("请输入用户名");
d.lb_uname_panduan.setForeground(Color.red);
} else {
if (uname.length() > 12 || uname.length() == 0) {
d.lb_uname_panduan.setText("用户名格式输入错误");
d.lb_uname_panduan.setForeground(Color.red);
} else {
d.lb_uname_panduan.setText("用户名输入正确");
d.lb_uname_panduan.setForeground(Color.green);
}
}
} else if (arg0.getSource() == d.pwd) {
String pwd = d.pwd.getText().trim();
if (pwd.length() < 6 || pwd.length() == 0) {
d.lb_pwd_panduan.setText("密码格式输入错误,不可少于6位");
d.lb_pwd_panduan.setForeground(Color.red);
} else {
d.lb_pwd_panduan.setText("输入正确");
d.lb_pwd_panduan.setForeground(Color.green);
}
} else if (arg0.getSource() == d.pwd1) {/*
* 做判断两次密码是否一致的判断
*
* 先获取第一次输入的密码,再获取第二次输入的密码,
* 然后比较一下两次密码是否一致不就完啦
*/
String pwd1 = d.pwd.getText().trim();// 获取第一次的密码
String pwd2 = d.pwd1.getText().trim();// 获取第二次输入的密码.
if (pwd1.equals(pwd2)) {
d.lb_pwd_queren.setText("输入正确");
d.lb_pwd_queren.setForeground(Color.GREEN);
} else {
d.lb_pwd_queren.setText("两次密码输入不一致");
d.lb_pwd_queren.setForeground(Color.red);
}
}
}
}
public class swing extends JFrame {
static JCheckBox aihao1, aihao2, aihao3 = null;
static JComboBox com1 = null;
static JButton DengLuAnNu, QuXiaoAnNu, ZhuCeMiMaAnNu = null;
static JTextArea jt1 = null;
static JLabel lb_uname, lb_qrmima, lb_sex, lb_aihao, lb_address,
lb_jieshao = null;
static JLabel lb_uname_panduan, lb_pwd_panduan, lb_pwd,
lb_pwd_queren = null;
static JPasswordField pwd, pwd1 = null;
static JRadioButton sex_man, sex_woman = null;
static JTextField uname;
shijian sj = new shijian(this);
/* this就是指对象自己,表示自己,在那个对象的成员函数里出现就表示哪个对象 */
public swing() {
/* 总界面设置 */
this.setTitle("登录界面");
this.setSize(700, 600);
this.setLocationRelativeTo(null);
this.setLayout(null);
/* 用户名 */
lb_uname = new JLabel("用户名");
this.add(lb_uname);
lb_uname.setBounds(100, 100, 40, 30);
/* setBounds(x,y,size_x,size_y) */
uname = new JTextField(20);
uname.setBounds(180, 100, 140, 30);
uname.addFocusListener(sj);
this.add(uname);
lb_uname_panduan = new JLabel("用户名长度不能大于12位");
lb_uname_panduan.setForeground(Color.red);
lb_uname_panduan.setBounds(330, 100, 250, 30);
this.add(lb_uname_panduan);
/* 密 码 */
lb_pwd = new JLabel("密 码");
this.add(lb_pwd);
lb_pwd.setBounds(100, 140, 40, 30);
pwd = new JPasswordField();
pwd.setBounds(180, 140, 140, 30);
pwd.addFocusListener(sj);
this.add(pwd);
lb_pwd_panduan = new JLabel("请输入密 码,不可少与6位");
lb_pwd_panduan.setForeground(Color.red);
lb_pwd_panduan.setBounds(330, 140, 240, 30);
this.add(lb_pwd_panduan);
// lb_pwd_panduan = new JLabel("请确认密码");
// lb_pwd_panduan.setForeground(Color.red);
// lb_pwd_panduan.setBounds(330, 140, 150, 30);
// this.add(lb_pwd_panduan);
/* 确认密码 */
lb_qrmima = new JLabel("确认密码");
lb_qrmima.setBounds(100, 180, 70, 30);
this.add(lb_qrmima);
pwd1 = new JPasswordField(20);
pwd1.setBounds(180, 180, 140, 30);
// pwd1.addFocusListener(sj);
pwd1.addFocusListener(new shijian(this));
this.add(pwd1);
lb_pwd_queren = new JLabel("请输入和上一 次一致的密码");
lb_pwd_queren.setForeground(Color.red);
lb_pwd_queren.setBounds(330, 180, 180, 30);
this.add(lb_pwd_queren);
lb_pwd_queren.addFocusListener(sj);
lb_sex = new JLabel("性别");
lb_sex.setBounds(100, 210, 70, 30);
this.add(lb_sex);
sex_woman = new JRadioButton("女");
sex_man = new JRadioButton("男", true);
sex_man.setBounds(180, 210, 70, 30);
sex_woman.setBounds(260, 210, 70, 30);
this.add(sex_man);
this.add(sex_woman);
ButtonGroup bg1 = new ButtonGroup();
bg1.add(sex_man);
bg1.add(sex_woman);
lb_aihao = new JLabel("爱好");
lb_aihao.setBounds(100, 250, 70, 30);
this.add(lb_aihao);
aihao1 = new JCheckBox("唱");
aihao2 = new JCheckBox("跳");
aihao3 = new JCheckBox("rap");
aihao1.setBounds(180, 250, 70, 30);
aihao2.setBounds(250, 250, 70, 30);
aihao3.setBounds(320, 250, 70, 30);
this.add(aihao1);
this.add(aihao2);
this.add(aihao3);
lb_address = new JLabel("家庭地址");
lb_address.setBounds(100, 290, 70, 30);
this.add(lb_address);
com1 = new JComboBox();
com1.addItem("请选择家庭地址");
com1.addItem("北京");
com1.addItem("上海");
com1.addItem("广州");
com1.setBounds(180, 290, 140, 30);
this.add(com1);
lb_jieshao = new JLabel("个人介绍");
lb_jieshao.setBounds(100, 330, 70, 30);
this.add(lb_jieshao);
jt1 = new JTextArea();
jt1.setColumns(8);
jt1.setRows(4);
jt1.setBounds(180, 330, 140, 40);
jt1.setLineWrap(true);
this.add(jt1);
JButton btn_dl = new JButton("已经注册过,可直接登录");
btn_dl.setBounds(20, 400, 180, 35);
QuXiaoAnNu = new JButton("取消");
ZhuCeMiMaAnNu = new JButton("没注册过,先注册");
ZhuCeMiMaAnNu.setBounds(368, 400, 180, 35);
QuXiaoAnNu.setBounds(240, 400, 80, 35);
btn_dl.addActionListener(sj);
ZhuCeMiMaAnNu.addActionListener(sj);
QuXiaoAnNu.addActionListener(sj);
this.add(btn_dl);
this.add(ZhuCeMiMaAnNu);
this.add(QuXiaoAnNu);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class yonghu {
static String pwd = "pwdpwd";
static String uname = "1";
}



package swing;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class chenggongjiemian extends JFrame {
static JButton btn1, btn2, btn3, btn4 = null;
static JPanel jp1, jp2, jp3, jp4, jp5, jp6 = null;
static JLabel lb3, lb4, lb5, lb6 = null;
public chenggongjiemian() {
this.setTitle("登录后的主界面");
this.setLayout(null);
this.setSize(700, 700);
this.setLocationRelativeTo(null);
// 实例化Jpanel
jp1 = new JPanel(null);
btn1 = new JButton("切换用户");
btn1.setBounds(100, 20, 100, 60);
btn2 = new JButton("借阅排行榜");
btn2.setBounds(250, 20, 100, 60);
btn3 = new JButton("系统管理");
btn3.setBounds(400, 20, 100, 60);
btn4 = new JButton("退出系统");
btn4.setBounds(550, 20, 100, 60);
jp1.add(btn1);
jp1.add(btn2);
jp1.add(btn3);
jp1.add(btn4);
jp2 = new JPanel(null);
jp3 = new JPanel(null);
jp4 = new JPanel(null);
jp5 = new JPanel(null);
jp6 = new JPanel(null);
lb3 = new JLabel("切换用户页面");
lb4 = new JLabel("借阅排行榜页面");
lb5 = new JLabel("系统管理页面");
lb6 = new JLabel("退出系统页面");
lb3.setBounds(200, 200, 200, 50);
lb4.setBounds(200, 200, 200, 50);
lb5.setBounds(200, 200, 200, 50);
lb6.setBounds(200, 200, 200, 50);
//lb3.setIcon(new ImageIcon("img//14.png"));
jp3.add(lb3);
jp4.add(lb4);
jp5.add(lb5);
jp6.add(lb6);
btn1.addActionListener(new shijian_mian(this));
btn2.addActionListener(new shijian_mian(this));
btn3.addActionListener(new shijian_mian(this));
btn4.addActionListener(new shijian_mian(this));
// 给投块区域设置他们的大小和位置,也是使用setBound进行设置
jp1.setBounds(0, 0, 700, 100);
jp2.setBounds(0, 100, 150, 600);
jp3.setBounds(150, 100, 550, 600);
jp4.setBounds(150, 100, 550, 600);
jp5.setBounds(150, 100, 550, 600);
jp6.setBounds(150, 100, 550, 600);
jp1.setBorder(BorderFactory.createLineBorder(Color.red));
jp2.setBorder(BorderFactory.createLineBorder(Color.green));
jp3.setBorder(BorderFactory.createLineBorder(Color.blue));
jp4.setBorder(BorderFactory.createLineBorder(Color.red));
jp5.setBorder(BorderFactory.createLineBorder(Color.green));
jp6.setBorder(BorderFactory.createLineBorder(Color.blue));
// 将这三个区域添加到当前窗体中
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// this.setTitle("登录后的界面");
// this.setLayout(null);
// this.setSize(800, 800);
// this.setLocationRelativeTo(null);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setVisible(true);
}
}
class shijian_mian implements ActionListener {
public chenggongjiemian dd = null;
public shijian_mian(chenggongjiemian d) {
dd = d;
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == dd.btn1) {
// 点击的是第-一个按钮执行第一个按钮的内容
dd.jp3.setVisible(true);// 让jp3的区域显示,其他的区域隐藏
dd.jp4.setVisible(false);
dd.jp5.setVisible(false);
dd.jp6.setVisible(false);
} else if (arg0.getSource() == dd.btn2) {
// 点击的是第2个按钮执行第2个按钮的内容
dd.jp4.setVisible(true);// 让jp4的区域显示,其他的区域隐藏
dd.jp3.setVisible(false);
dd.jp5.setVisible(false);
dd.jp6.setVisible(false);
} else if (arg0.getSource() == dd.btn3) {
// 点击的是第3个按钮执行第3个按钮的内容
dd.jp5.setVisible(true);// 让jp5的区域显示,其他的区域隐藏
dd.jp4.setVisible(false);
dd.jp3.setVisible(false);
dd.jp6.setVisible(false);
} else if (arg0.getSource() == dd.btn4) {
// 点击的是第4个按钮执行第4个按钮的内容
dd.jp6.setVisible(true);// 让jp6的区域显示,其他的区域隐藏
dd.jp4.setVisible(false);
dd.jp5.setVisible(false);
dd.jp3.setVisible(false);
}
}
}


执行效果展示:





界面不够美观?那么加下图片(png或gif),只要下面的部分改变其他的和上面的可以一样:









package swing;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class chenggongjiemian extends JFrame {
static JButton btn1, btn2, btn3, btn4 = null;
static JPanel jp1, jp2, jp3, jp4, jp5, jp6 = null;
static JLabel lb3, lb4, lb5, lb6 = null;
public chenggongjiemian() {
this.setTitle("登录后的主界面");
this.setLayout(null);
this.setSize(700, 700);
this.setLocationRelativeTo(null);
// 实例化Jpanel
jp1 = new JPanel(null);
btn1 = new JButton("切换用户");
btn1.setBounds(100, 20, 100, 60);
btn2 = new JButton("借阅排行榜");
btn2.setBounds(250, 20, 100, 60);
btn3 = new JButton("系统管理");
btn3.setBounds(400, 20, 100, 60);
btn4 = new JButton("退出系统");
btn4.setBounds(550, 20, 100, 60);
jp1.add(btn1);
jp1.add(btn2);
jp1.add(btn3);
jp1.add(btn4);
jp2 = new JPanel(null);
jp3 = new JPanel(null);
jp4 = new JPanel(null);
jp5 = new JPanel(null);
jp6 = new JPanel(null);
lb3 = new JLabel();
lb4 = new JLabel();
lb5 = new JLabel();
lb6 = new JLabel();
lb3.setBounds(0, 0, 534, 564);// QQ截图界面可知道要的图片大小
/* setBounds(x,y,size_x,size_y) */
lb4.setBounds(0, 0, 534, 564);
lb5.setBounds(0, 0, 534, 564);
lb6.setBounds(0, 0, 534, 564);
lb3.setIcon(new ImageIcon("img//11.png"));
lb4.setIcon(new ImageIcon("img//22.png"));
lb5.setIcon(new ImageIcon("img//33.png"));
lb6.setIcon(new ImageIcon("img//44.png"));
jp3.add(lb3);
jp4.add(lb4);
jp5.add(lb5);
jp6.add(lb6);
btn1.addActionListener(new shijian_mian(this));
btn2.addActionListener(new shijian_mian(this));
btn3.addActionListener(new shijian_mian(this));
btn4.addActionListener(new shijian_mian(this));
// 给投块区域设置他们的大小和位置,也是使用setBound进行设置
jp1.setBounds(0, 0, 700, 100);
jp2.setBounds(0, 100, 150, 600);
jp3.setBounds(150, 100, 550, 600);
jp4.setBounds(150, 100, 550, 600);
jp5.setBounds(150, 100, 550, 600);
jp6.setBounds(150, 100, 550, 600);
jp1.setBorder(BorderFactory.createLineBorder(Color.red));
jp2.setBorder(BorderFactory.createLineBorder(Color.green));
jp3.setBorder(BorderFactory.createLineBorder(Color.blue));
jp4.setBorder(BorderFactory.createLineBorder(Color.red));
jp5.setBorder(BorderFactory.createLineBorder(Color.green));
jp6.setBorder(BorderFactory.createLineBorder(Color.blue));
// 将这三个区域添加到当前窗体中
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// this.setTitle("登录后的界面");
// this.setLayout(null);
// this.setSize(800, 800);
// this.setLocationRelativeTo(null);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setVisible(true);
}
}
class shijian_mian implements ActionListener {
public chenggongjiemian dd = null;
public shijian_mian(chenggongjiemian d) {
dd = d;
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == dd.btn1) {
// 点击的是第-一个按钮执行第一个按钮的内容
dd.jp3.setVisible(true);// 让jp3的区域显示,其他的区域隐藏
dd.jp4.setVisible(false);
dd.jp5.setVisible(false);
dd.jp6.setVisible(false);
} else if (arg0.getSource() == dd.btn2) {
// 点击的是第2个按钮执行第2个按钮的内容
dd.jp4.setVisible(true);// 让jp4的区域显示,其他的区域隐藏
dd.jp3.setVisible(false);
dd.jp5.setVisible(false);
dd.jp6.setVisible(false);
} else if (arg0.getSource() == dd.btn3) {
// 点击的是第3个按钮执行第3个按钮的内容
dd.jp5.setVisible(true);// 让jp5的区域显示,其他的区域隐藏
dd.jp4.setVisible(false);
dd.jp3.setVisible(false);
dd.jp6.setVisible(false);
} else if (arg0.getSource() == dd.btn4) {
// 点击的是第4个按钮执行第4个按钮的内容
dd.jp6.setVisible(true);// 让jp6的区域显示,其他的区域隐藏
dd.jp4.setVisible(false);
dd.jp5.setVisible(false);
dd.jp3.setVisible(false);
}
}
}
