java小白请问这样怎么设置按钮点击事件来读取两个文本框里的值,谢谢各位大佬了
package hello;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("unused")
public class Login {
private String account;
private String password;
public static void main(String[]args) {
Login login=new Login();//*在主函数中,实例化Login类的对象,然后用对象名调用初始化界面的方法*
login.initUI();
}
JTextField textName;
JTextField textname1;
JButton buttonname;
JButton buttonname1;
public void initUI() {
JFrame frame=new JFrame();
frame.setTitle("Login");
frame.setSize(500,500);
frame.setLocation(250, 300);
FlowLayout f1=new FlowLayout(FlowLayout.CENTER);
frame.setLayout(f1);
frame.setDefaultCloseOperation(3);
ImageIcon icon = new ImageIcon("...");
JLabel labelIcon = new JLabel(icon);
frame.add(labelIcon);
JLabel labelName = new JLabel("账号:");
frame.add(labelName);
textName = new JTextField();
account = textName.getText();
textName.setPreferredSize(new Dimension(410, 30));
frame.add(textName);
JLabel labelname=new JLabel("密码:");
frame.add(labelname);
textname1=new JTextField();
password = textname1.getText();
textname1.setPreferredSize(new Dimension(410,30));
frame.add(textname1);
buttonname=new JButton("登录");
frame.add(buttonname);
buttonname1=new JButton("取消");
frame.add(buttonname1);
frame.setVisible(true);//*设置登录界面可见*
}
}