Ⅰ 用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();
 }
}