62,614
社区成员
发帖
与我相关
我的任务
分享
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class HandleAction implements ActionListener{
JLabel JLabel1,JLabel2;
JFrame mainJFrame;
Container con;
JButton loginbtn,cancelbtn;
JTextField userText;
JTextField passwordField;
public void actionPerformed(ActionEvent e){
String msg;
if(e.getSource()==loginbtn){
msg="your username:"+userText.getText()+"\n your password:"
+new String(passwordField.getText());
JOptionPane.showMessageDialog(mainJFrame,msg);
}
else if(e.getSource()==cancelbtn){
passwordField.setText("");
userText.setText("");
}
}
public HandleAction(){
JLabel1=new JLabel("用户名");
JLabel1.setBounds(new Rectangle(20,20,60,20));//设置位置左边距、上边距、宽度、高度
JLabel2=new JLabel("密码");
JLabel2.setBounds(new Rectangle(20,50,60,20));//设置位置左边距、上边距、宽度、高度
mainJFrame=new JFrame("后台登陆");
con=mainJFrame.getContentPane();
con.setLayout(null);//要设置成null,不然默认是BorderLayout布局
loginbtn=new JButton("登陆");
loginbtn.setBounds(new Rectangle(20,80,60,20));//设置位置左边距、上边距、宽度、高度
loginbtn.addActionListener(this);
cancelbtn=new JButton("取消");
cancelbtn.setBounds(new Rectangle(90,80,60,20));//设置位置左边距、上边距、宽度、高度
cancelbtn.addActionListener(this);
userText=new JTextField();
userText.setColumns(20);
userText.setBounds(new Rectangle(90,20,60,20));//设置位置左边距、上边距、宽度、高度
passwordField=new JPasswordField();
passwordField.setColumns(20);
passwordField.setBounds(new Rectangle(90,50,60,20));//设置位置左边距、上边距、宽度、高度
con.add(JLabel1);
con.add(userText);
con.add(JLabel2);
con.add(passwordField);
con.add(loginbtn);
con.add(cancelbtn);
mainJFrame.setSize(300,300);
mainJFrame.setVisible(true);
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){new HandleAction();}
}