美文网首页
2.2响应用户输入

2.2响应用户输入

作者: linhaoyou | 来源:发表于2017-07-05 16:51 被阅读0次
1.png 图片.png 图片.png

import java.awt.;
import javax.swing.
;
import java.awt.event.*;

public class QQLogin implements ActionListener{
public static void main(String args[]){
JFrame w = new JFrame();
w.setSize(250, 125);

    //new 组件
    JLabel labUser = new JLabel("用户名");
    JLabel labPass = new JLabel("密码");
    
    JTextField txtUser = new JTextField();
    JPasswordField txtPass = new JPasswordField();
    
    JButton btnLogin = new JButton("登录");
    JButton btnReg = new JButton("注册");
    JButton btnCancel = new JButton("取消");
    
    //注册事件监听
    QQLogin e = new QQLogin();
    btnLogin.addActionListener(e);
    btnReg.addActionListener(e);
    btnCancel.addActionListener(e);
    
    //布置输入面板
    JPanel panInput = new JPanel();
    panInput.setLayout(new GridLayout(2, 2));
    
    panInput.add(labUser);
    panInput.add(txtUser);
    
    panInput.add(labPass);
    panInput.add(txtPass);
    
    //布置按钮面板
    JPanel panButton = new JPanel();
    panButton.setLayout(new FlowLayout());
    
    panButton.add(btnLogin);
    panButton.add(btnReg);
    panButton.add(btnCancel);
    
    //布置窗体
    w.setLayout(new BorderLayout());
    
    w.add(panInput, BorderLayout.CENTER);
    w.add(panButton, BorderLayout.SOUTH);
    
    w.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent arg0){
    if(arg0.getActionCommand() == "登录"){
        System.out.println("用户点击了登录");
    }
    if(arg0.getActionCommand() == "注册"){
        System.out.println("用户点击了注册");
    }
    if(arg0.getActionCommand() == "取消"){
        System.out.println("用户点击了取消");
    }
}

}

图片.png

相关文章

网友评论

      本文标题:2.2响应用户输入

      本文链接:https://www.haomeiwen.com/subject/cxuqhxtx.html