Java GUI 实现登录界面
生活随笔
收集整理的這篇文章主要介紹了
Java GUI 实现登录界面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
具體效果圖:
詳細代碼:
package java_gui;import java.awt.Color; import java.awt.Container; import java.awt.Cursor; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.ImageIcon; 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.JRootPane; import javax.swing.JTextField;public class Login extends JFrame {private static final long serialVersionUID = -6788045638380819221L;//用戶名private JTextField ulName;//密碼private JPasswordField ulPasswd;//小容器private JLabel j1;private JLabel j2;private JLabel j3;private JLabel j4;//小按鈕private JButton b1;private JButton b2;private JButton b3;//復選框private JCheckBox c1;private JCheckBox c2;//列表框private JComboBox<String> cb1;/*** 初始化QQ登錄頁面* */public Login() {//設置登錄窗口標題this.setTitle("QQ登錄");//去掉窗口的裝飾(邊框)// this.setUndecorated(true);//采用指定的窗口裝飾風格this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);//窗體組件初始化init();this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置布局為絕對定位this.setLayout(null);this.setBounds(0, 0, 355, 265);//設置窗體的圖標Image img0 = new ImageIcon("D:/logo.png").getImage();this.setIconImage(img0);//窗體大小不能改變this.setResizable(false);//居中顯示this.setLocationRelativeTo(null);//窗體顯示this.setVisible(true);}/*** 窗體組件初始化* */public void init() {//創建一個容器,其中的圖片大小和setBounds第三、四個參數要基本一致(需要自己計算裁剪)Container container = this.getContentPane();j1 = new JLabel();//設置背景色Image img1 = new ImageIcon("D:/bgimg.png").getImage();j1.setIcon(new ImageIcon(img1));j1.setBounds(0, 0, 355, 265);//qq頭像設定j2 = new JLabel();Image img2 = new ImageIcon("D:/hdimg.png").getImage();j2.setIcon(new ImageIcon(img2));j2.setBounds(40, 95, 50, 53);//用戶名輸入框ulName = new JTextField();ulName.setBounds(100, 100, 150, 20);//注冊賬號j3 = new JLabel("注冊賬號");j3.setBounds(260, 100, 70, 20);//密碼輸入框ulPasswd = new JPasswordField();ulPasswd.setBounds(100, 130, 150, 20);//找回密碼j4= new JLabel("找回密碼");j4.setBounds(260, 130, 70, 20);//記住密碼c1 = new JCheckBox("記住密碼");c1.setBounds(105, 155, 80, 15);//自動登陸c2 = new JCheckBox("自動登陸");c2.setBounds(185, 155, 80, 15);//用戶登陸狀態選擇cb1 = new JComboBox<String>();cb1.addItem("在線");cb1.addItem("隱身");cb1.addItem("離開");cb1.setBounds(40, 150, 55, 20);//登陸按鈕b1 = new JButton("登錄");//設置字體和顏色和手形指針b1.setFont(new Font("宋體", Font.PLAIN, 12));b1.setForeground(Color.RED);b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));b1.setBounds(280, 200, 65, 20);//給按鈕添加b1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String cmd = e.getActionCommand();if("登錄".equals(cmd)) {String username = ulName.getText();String userpassword = ulPasswd.getText();if(username.equals("tskk") && userpassword.equals("123456")) {JOptionPane.showConfirmDialog(null, "登錄成功");} else {JOptionPane.showConfirmDialog(null, "登錄失敗");}}}});//多賬號b2 = new JButton("多賬號");b2.setBounds(5, 200, 75, 20);//設置b3 = new JButton("設置");b3.setBounds(100, 200, 65, 20);//所有組件用容器裝載j1.add(j2);j1.add(j3);j1.add(j4);j1.add(c1);j1.add(c2);j1.add(cb1);j1.add(b1);j1.add(b2);j1.add(b3);container.add(j1);container.add(ulName);container.add(ulPasswd);}public static void main(String[] args) {new Login();} }總結
以上是生活随笔為你收集整理的Java GUI 实现登录界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 动力不佳什么牌子机油提升快?
- 下一篇: 二级java考什么_计算机二级Java考