最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Java swing(純代碼和含部分個人解析)插入png或gif圖片的方法,切換界面功能的實現(xiàn)

2020-04-03 14:02 作者:詩書畫唱  | 我要投稿



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;


/* 下面的是構(gòu)造方法,無void */


public shijian(swing dl) {


d = dl;


}


@Override

/*

*?

* actionperformed方法是ActionListener監(jiān)聽事件中的一個重寫方法,

*?

* 如果要求按鈕執(zhí)行一些動作,可以在這個方法中添加、修改、刪除以及判斷。

*/

public void actionPerformed(ActionEvent arg0) {


/* 當(dāng)按鈕中字符為"登錄"時,就執(zhí)行equals("登錄")中的內(nèi)容 */


if (arg0.getActionCommand().equals("已經(jīng)注冊過,可直接登錄")) {


/* trim()函數(shù)移除字符串兩側(cè)的空白字符或其他預(yù)定義字符。 */


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, "是否確認(rèn)關(guān)閉頁面");


if (o == 0) {


System.exit(0);


}


}


}


/*

*?

* arg是java主函數(shù)的入口參數(shù),它是一個字符串?dāng)?shù)組變量 void focusGained(FocusEvent e):當(dāng)獲得焦點時發(fā)生.

*?

* void focusLost(FocusEvent e):當(dāng)失去焦點時發(fā)生.

*/


@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("選中了確認(rèn)密碼框");


}


}


@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就是指對象自己,表示自己,在那個對象的成員函數(shù)里出現(xiàn)就表示哪個對象 */


public swing() {


/* 總界面設(shè)置 */


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("請確認(rèn)密碼");


// lb_pwd_panduan.setForeground(Color.red);


// lb_pwd_panduan.setBounds(330, 140, 150, 30);


// this.add(lb_pwd_panduan);


/* 確認(rèn)密碼 */


lb_qrmima = new JLabel("確認(rèn)密碼");


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("已經(jīng)注冊過,可直接登錄");


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("系統(tǒng)管理");

btn3.setBounds(400, 20, 100, 60);

btn4 = new JButton("退出系統(tǒng)");

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("系統(tǒng)管理頁面");

lb6 = new JLabel("退出系統(tǒng)頁面");

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));


// 給投塊區(qū)域設(shè)置他們的大小和位置,也是使用setBound進行設(shè)置

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));

// 將這三個區(qū)域添加到當(dāng)前窗體中

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) {

// 點擊的是第-一個按鈕執(zhí)行第一個按鈕的內(nèi)容

dd.jp3.setVisible(true);// 讓jp3的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp4.setVisible(false);

dd.jp5.setVisible(false);

dd.jp6.setVisible(false);

} else if (arg0.getSource() == dd.btn2) {

// 點擊的是第2個按鈕執(zhí)行第2個按鈕的內(nèi)容

dd.jp4.setVisible(true);// 讓jp4的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp3.setVisible(false);

dd.jp5.setVisible(false);

dd.jp6.setVisible(false);

} else if (arg0.getSource() == dd.btn3) {

// 點擊的是第3個按鈕執(zhí)行第3個按鈕的內(nèi)容

dd.jp5.setVisible(true);// 讓jp5的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp4.setVisible(false);

dd.jp3.setVisible(false);

dd.jp6.setVisible(false);

} else if (arg0.getSource() == dd.btn4) {


// 點擊的是第4個按鈕執(zhí)行第4個按鈕的內(nèi)容

dd.jp6.setVisible(true);// 讓jp6的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp4.setVisible(false);

dd.jp5.setVisible(false);

dd.jp3.setVisible(false);

}


}

}


執(zhí)行效果展示:




界面不夠美觀?那么加下圖片(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("系統(tǒng)管理");

btn3.setBounds(400, 20, 100, 60);

btn4 = new JButton("退出系統(tǒng)");

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));


// 給投塊區(qū)域設(shè)置他們的大小和位置,也是使用setBound進行設(shè)置

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));

// 將這三個區(qū)域添加到當(dāng)前窗體中

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) {

// 點擊的是第-一個按鈕執(zhí)行第一個按鈕的內(nèi)容

dd.jp3.setVisible(true);// 讓jp3的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp4.setVisible(false);

dd.jp5.setVisible(false);

dd.jp6.setVisible(false);

} else if (arg0.getSource() == dd.btn2) {

// 點擊的是第2個按鈕執(zhí)行第2個按鈕的內(nèi)容

dd.jp4.setVisible(true);// 讓jp4的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp3.setVisible(false);

dd.jp5.setVisible(false);

dd.jp6.setVisible(false);

} else if (arg0.getSource() == dd.btn3) {

// 點擊的是第3個按鈕執(zhí)行第3個按鈕的內(nèi)容

dd.jp5.setVisible(true);// 讓jp5的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp4.setVisible(false);

dd.jp3.setVisible(false);

dd.jp6.setVisible(false);

} else if (arg0.getSource() == dd.btn4) {


// 點擊的是第4個按鈕執(zhí)行第4個按鈕的內(nèi)容

dd.jp6.setVisible(true);// 讓jp6的區(qū)域顯示,其他的區(qū)域隱藏

dd.jp4.setVisible(false);

dd.jp5.setVisible(false);

dd.jp3.setVisible(false);

}


}

}




Java swing(純代碼和含部分個人解析)插入png或gif圖片的方法,切換界面功能的實現(xiàn)的評論 (共 條)

分享到微博請遵守國家法律
邵阳县| 宝应县| 临泉县| 威海市| 中阳县| 高密市| 巴彦县| 隆化县| 扎赉特旗| 新邵县| 安阳市| 余江县| 安达市| 中阳县| 怀集县| 潮州市| 岑溪市| 泗洪县| 玉门市| 永康市| 平谷区| 行唐县| 海淀区| 明星| 禄劝| 安多县| 沈丘县| 淮北市| 陇西县| 黔西县| 白城市| 东丽区| 留坝县| 武川县| 阿图什市| 昭通市| 长岭县| 长春市| 绥德县| 乐昌市| 靖边县|