Ⅰ 用java編寫一個登錄界面,用SWING組件
import java.awt.Color;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
/**
 * 一個簡單的Swing窗口,輸入內容單擊「確定」按鈕後,在文本域中顯示輸入的內容。
 * 單擊「取消」按鈕,清空頁面內容。
 * @author yzg
 * 
 */
public class Register extends JFrame {
    private static final long serialVersionUID = 1L;
    private JLabel nameLabel;
    private JTextArea context;
    private JTextField name;
    private JLabel pLabel;
    JList speciality;
    JLabel mLabel;
    String[] data = { "計算機", "英語", "機械", "化工" };
    ButtonGroup bg;
    JRadioButton male;
    JRadioButton female;
    JLabel fLabel;
    JCheckBox faverite1;
    JCheckBox faverite2;
    JCheckBox faverite3;
    JCheckBox faverite4;
    public Register(String title) {
        super(title);
        this.getContentPane().setLayout(null);
        // 下面兩行是取得屏幕的高度和寬度
        double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 200));// 設定窗口出現位置
        this.setSize(340, 440);// 設定窗口大小
    }
    public void showWin() {
        // 確保窗體有一個好的外觀裝飾
        // (true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 姓名
        nameLabel = new JLabel("姓名 :");
        nameLabel.setBounds(30, 10, 50, 25);
        name = new JTextField();
        name.setBounds(80, 10, 120, 20);
        name.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        name.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent e) {
            }
            public void keyReleased(KeyEvent e) {
            }
            public void keyTyped(KeyEvent e) {
                if (name.getText().length() > 6) {
                    name.setText(name.getText().substring(0, 6));
                }
            }
        });
        // 專業 組合框
        pLabel = new JLabel("專業 :");
        pLabel.setBounds(30, 40, 50, 25);
        speciality = new JList(data);
        speciality.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        speciality.setBounds(80, 40, 80, 85);
        speciality.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        mLabel = new JLabel("性別 :");
        mLabel.setBounds(30, 130, 50, 25);
        // 性別 單選框
        bg = new ButtonGroup();
        male = new JRadioButton("男");
        female = new JRadioButton("女");
        bg.add(male);
        bg.add(female);
        male.setBounds(80, 130, 60, 25);
        female.setBounds(140, 130, 60, 25);
        fLabel = new JLabel("愛好 :");
        fLabel.setBounds(30, 160, 50, 25);
        // 愛好 復選框
        faverite1 = new JCheckBox("音樂");
        faverite2 = new JCheckBox("足球");
        faverite3 = new JCheckBox("高爾夫");
        faverite4 = new JCheckBox("游戲");
        faverite1.setBounds(80, 160, 60, 25);
        faverite2.setBounds(140, 160, 60, 25);
        faverite3.setBounds(200, 160, 65, 25);
        faverite4.setBounds(265, 160, 60, 25);
        // 內容 文本區域
        JLabel conLabel = new JLabel("輸入的內容 :");
        conLabel.setBounds(30, 250, 90, 25);
        context = new JTextArea();
        context.setBounds(30, 270, 260, 100);
        context.setBorder(BorderFactory.createLineBorder(Color.black));
        // 確定按鈕
        JButton ok = new JButton("確定");
        ok.setBounds(50, 190, 60, 25);
        ok.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                StringBuffer sb = new StringBuffer();
                sb.append(nameLabel.getText()).append(name.getText());
                sb.append("\n");
                int index = speciality.getSelectedIndex();
                if (index >= 0) {
                    sb.append(pLabel.getText()).append(data[index]);
                } else {
                    sb.append(pLabel.getText());
                }
                sb.append("\n");
                sb.append(mLabel.getText());
                if (male.isSelected()) {
                    sb.append("男");
                }
                if (female.isSelected()) {
                    sb.append("女");
                }
                sb.append("\n");
                sb.append(fLabel.getText());
                if (faverite1.isSelected()) {
                    sb.append("音樂  ");
                }
                if (faverite2.isSelected()) {
                    sb.append("足球  ");
                }
                if (faverite3.isSelected()) {
                    sb.append("高爾夫  ");
                }
                if (faverite4.isSelected()) {
                    sb.append("游戲  ");
                }
                context.setText(sb.toString());
            }
            public void mouseEntered(MouseEvent e) {
            }
            public void mouseExited(MouseEvent e) {
            }
            public void mousePressed(MouseEvent e) {
            }
            public void mouseReleased(MouseEvent e) {
            }
        });
        // 取消按鈕
        JButton cancel = new JButton("取消");
        cancel.setBounds(120, 190, 60, 25);
        cancel.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                name.setText("");
                speciality.clearSelection();
                
                if (faverite1.isSelected()) {
                    faverite1.setSelected(false);
                }
                if (faverite2.isSelected()) {
                    faverite2.setSelected(false);
                }
                if (faverite3.isSelected()) {
                    faverite3.setSelected(false);
                }
                if (faverite4.isSelected()) {
                    faverite4.setSelected(false);
                }
                context.setText("");
            }
            public void mouseEntered(MouseEvent e) {
            }
            public void mouseExited(MouseEvent e) {
            }
            public void mousePressed(MouseEvent e) {
            }
            public void mouseReleased(MouseEvent e) {
            }
        });
        this.getContentPane().add(nameLabel);
        this.getContentPane().add(name);
        this.getContentPane().add(pLabel);
        this.getContentPane().add(speciality);
        this.getContentPane().add(mLabel);
        this.getContentPane().add(male);
        this.getContentPane().add(female);
        this.getContentPane().add(fLabel);
        this.getContentPane().add(faverite1);
        this.getContentPane().add(faverite2);
        this.getContentPane().add(faverite3);
        this.getContentPane().add(faverite4);
        this.getContentPane().add(conLabel);
        this.getContentPane().add(context);
        this.getContentPane().add(ok);
        this.getContentPane().add(cancel);
        // this.pack();
        this.setVisible(true);
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        Register reg = new Register("Register");
        reg.showWin();
    }
}
Ⅱ java swing 程序中,設置了一個登錄頁面,如何從登錄後的窗口獲得登錄時的用戶名
一般來說有兩種常見的方法,根據需要任選一種就可以了。
1、在login.java裡面,你要啟動a.java的窗口,肯定會new一個a的對象,你可以在a裡面定義幾個成員變數,例如
publicclassa{
publicStringuser_name=null;
publica(){
}
}然後在new一個a對象以後直接把這個成員變數賦值,就可以了,例如在login.java裡面
aform_a=newa();
a.user_name="用戶名";
這樣在a的代碼裡面就可以盡情使用了。如果要更加規范一些,就不要使用public聲明,而把user_name設置為private變數,然後寫兩個方法 getUsername()和setUsername(Stirng username)來操作它。
2、在login.java或者a.java裡面定義靜態變數,即static變數,例如這樣:
publicclassa{
publicstaticStringuser_name=null;
publica(){
}
}這樣定義的變數,不需要類實例化成為對象就可以使用,不過全局只有一個,在某些時候非常適合,例如這里的保存用戶名,但是當變數為對象相關的時候是不適合的。
在login.java裡面這樣使用:
a.user_name="用戶名";
然後再加上你彈出a.java的窗口的代碼就可以了。
Ⅲ java實現簡單登錄界面
自己寫的比較規范的代碼,都有注釋:
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按鈕
import javax.swing.JLabel;//標簽
import javax.swing.JTextField;//文本框
import java.awt.Font;//字體
import java.awt.Color;//顏色
import javax.swing.JPasswordField;//密碼框
import java.awt.event.ActionListener;//事件監聽
import java.awt.event.ActionEvent;//事件處理
import javax.swing.JOptionPane;//消息窗口
public class UserLogIn extends JFrame{
 public JPanel pnluser;
 public JLabel lbluserLogIn;
 public JLabel lbluserName;
 public JLabel lbluserPWD;
 public JTextField txtName;
 public JPasswordField pwdPwd;
 public JButton btnSub;
 public JButton btnReset;
 
 public UserLogIn(){
  pnluser = new JPanel();
  lbluserLogIn = new JLabel();
  lbluserName = new JLabel();
  lbluserPWD = new JLabel();
  txtName = new JTextField();
  pwdPwd = new JPasswordField();
  btnSub = new JButton();
  btnReset = new JButton();
  userInit();
 }
 
 public void userInit(){
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉框架的同時結束程序
  this.setSize(300,200);//設置框架大小為長300,寬200
  this.setResizable(false);//設置框架不可以改變大小
  this.setTitle("用戶登錄");//設置框架標題
  this.pnluser.setLayout(null);//設置面板布局管理
  this.pnluser.setBackground(Color.cyan);//設置面板背景顏色
  this.lbluserLogIn.setText("用戶登錄");//設置標簽標題
  this.lbluserLogIn.setFont(new Font("宋體",Font.BOLD | Font.ITALIC,14));//設置標簽字體
  this.lbluserLogIn.setForeground(Color.RED);//設置標簽字體顏色
  this.lbluserName.setText("用戶名:");
  this.lbluserPWD.setText("密    碼:");
  this.btnSub.setText("登錄");
  this.btnReset.setText("重置");
  this.lbluserLogIn.setBounds(120,15,60,20);//設置標簽x坐標120,y坐標15,長60,寬20
  this.lbluserName.setBounds(50,55,60,20);
  this.lbluserPWD.setBounds(50,85,60,25);
  this.txtName.setBounds(110,55,120,20);
  this.pwdPwd.setBounds(110,85,120,20);
  this.btnSub.setBounds(85,120,60,20);
  this.btnSub.addActionListener(new ActionListener()//匿名類實現ActionListener介面
   {
    public void actionPerformed(ActionEvent e){
     btnsub_ActionEvent(e);
    }    
   }
  ); 
  this.btnReset.setBounds(155,120,60,20);
  this.btnReset.addActionListener(new ActionListener()//匿名類實現ActionListener介面
   {
    public void actionPerformed(ActionEvent e){
     btnreset_ActionEvent(e);
    }    
   }
  );   
  this.pnluser.add(lbluserLogIn);//載入標簽到面板
  this.pnluser.add(lbluserName);
  this.pnluser.add(lbluserPWD);
  this.pnluser.add(txtName);
  this.pnluser.add(pwdPwd);
  this.pnluser.add(btnSub);
  this.pnluser.add(btnReset);
  this.add(pnluser);//載入面板到框架
  this.setVisible(true);//設置框架可顯  
 }
 
 public void btnsub_ActionEvent(ActionEvent e){
  String name = txtName.getText();
  String pwd = String.valueOf(pwdPwd.getPassword());
  if(name.equals("")){
   JOptionPane.showMessageDialog(null,"賬號不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
   return;
  }else if (pwd.equals("")){
   JOptionPane.showMessageDialog(null,"密碼不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
   return;
  }else if(true){
   this.dispose();
  }else{
   JOptionPane.showMessageDialog(null,"賬號或密碼錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
   return;
  }
 }
 
 public void btnreset_ActionEvent(ActionEvent e){
  txtName.setText("");
  pwdPwd.setText("");
 }
 
 public static void main(String[] args){
  new UserLogIn();
 }
}
Ⅳ 登陸界面的java代碼怎麼寫
具體框架使用jframe,文本框組件:;密碼框組件:JPasswordField;標簽組件:JLabel;復選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。
登錄界面:

Swing 是一個為Java設計的GUI工具包。
Swing是JAVA基礎類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平台運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統默認的特有主題),然而不是真的使用原生平台提供的設備,而是僅僅在表面上模仿它們。這意味著你可以在任意平台上使用JAVA支持的任意麵板。輕量級組件的缺點則是執行速度較慢,優點就是可以在所有平台上採用統一的行為。
概念解析:
JFrame– java的GUI程序的基本思路是以JFrame為基礎,它是屏幕上window的對象,能夠最大化、最小化、關閉。
JPanel– Java圖形用戶界面(GUI)工具包swing中的面板容器類,包含在javax.swing 包中,可以進行嵌套,功能是對窗體中具有相同邏輯功能的組件進行組合,是一種輕量級容器,可以加入到JFrame窗體中。。
JLabel– JLabel 對象可以顯示文本、圖像或同時顯示二者。可以通過設置垂直和水平對齊方式,指定標簽顯示區中標簽內容在何處對齊。默認情況下,標簽在其顯示區內垂直居中對齊。默認情況下,只顯示文本的標簽是開始邊對齊;而只顯示圖像的標簽則水平居中對齊。
JTextField–一個輕量級組件,它允許編輯單行文本。
JPasswordField– 允許我們輸入了一行字像輸入框,但隱藏星號(*) 或點創建密碼(密碼)
JButton– JButton 類的實例。用於創建按鈕類似實例中的 "Login"。
Ⅳ 用JAVA語言編程實現一個用戶登錄窗口
可以直接拿去運行
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MainUI {
 private String username;
 private String password;
 private JFrame login_Frame;
 private JLabel username_Label;
 private JLabel password_Label;
 private JTextField username_Field;
 private JPasswordField password_Field;
 private JButton reset_Button;
 private JButton submit_Button;
 private static MainUI clientUI = new MainUI();;
 public static MainUI getUIClass() {
  return clientUI;
 }
 /**
  * 創建登陸窗口
  */
 public void createLoginUI() {
  login_Frame = new JFrame();
  login_Frame.setTitle("chaochao簡易聊天室登陸框");
  login_Frame.setSize(210, 170);
  username_Field = new JTextField(10);
  password_Field = new JPasswordField(10);
  username_Label = new JLabel("用戶名");
  password_Label = new JLabel("密    碼");
  reset_Button = new JButton("重置");
  submit_Button = new JButton("登陸");
  java.awt.FlowLayout fl = new java.awt.FlowLayout();
  login_Frame.setLayout(fl);
  login_Frame.add(username_Label);
  login_Frame.add(username_Field);
  login_Frame.add(password_Label);
  login_Frame.add(password_Field);
  login_Frame.add(submit_Button);
  login_Frame.add(reset_Button);
  login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //窗體居中
  login_Frame.setLocationRelativeTo(null);
  login_Frame.setVisible(true);
  //重置按鈕加事件監聽器
  reset_Button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    username_Field.setText("");
    password_Field.setText("");
   }
  });
  //登錄按鈕加事件監聽器
  submit_Button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    username = username_Field.getText();
    password = password_Field.getText();
    //判斷用戶名密碼是否正確
    if (username.equals("admin") && password.equals("123")) {
     JOptionPane.showMessageDialog(null, "登陸成功!", "消息",
       JOptionPane.INFORMATION_MESSAGE);
     login_Frame.dispose();
    } else {
     JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!", "錯誤",
       JOptionPane.ERROR_MESSAGE);
    }
   }
  });
 }
 public static void main(String[] args) {
  MainUI c = getUIClass();
  c.createLoginUI();
 }
}