用java做个登陆界面输入的用户名和密码输出到纪事本
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//用java做个登陆界面输入的用户名和密码输出到纪事本
public class test_login extends JFrame
{
static String name;
static String pass;
public void write2txt()
{
try
{
File f1 = new File ("c:\\temp\\java1.txt");
f1.createNewFile();
FileWriter f=new FileWriter(f1);
f.write(name+" "+pass);
f.write(13);
f.write(10);
f.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void create()
{
JFrame fra = new JFrame("这里是窗口");
fra.setLayout(new FlowLayout()); //布局管理器
JTextField j1 = new JTextField("用户名:",5); j1.setEnabled(false);
JTextField j2 = new JTextField("密码:",5); j2.setEnabled(false);
final JTextField j3 = new JTextField("",16);
final JTextField j4 = new JPasswordField("",16);
JButton b1 = new JButton("确定");
JButton b2 = new JButton("取消");
class Listener_ok implements ActionListener
{//“确定按钮监听类”
public void actionPerformed(ActionEvent e)
{
name = j3.getText();
pass = j4.getText();
if (name.compareTo("")==0||pass.compareTo("")==0)
{//用户名密码不为空
JOptionPane.showMessageDialog(null,"用户名或密码为空,请重新输入","错误", JOptionPane.PLAIN_MESSAGE);
}
else
{//这里开始写盘
System.out.println("写盘");
write2txt();
}
}
}
b1.addActionListener(new Listener_ok());
class Listener_cancel implements ActionListener
{//“取消”按钮监听类
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
}
b2.addActionListener(new Listener_cancel());
fra.add(j1);
fra.add(j3);
fra.add(j2);
fra.add(j4);
fra.add(b1);
fra.add(b2);
fra.setSize(300,150);
fra.setVisible(true);
}
static public void main(String args[])
{
test_login w1 = new test_login();
w1.create();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//用java做个登陆界面输入的用户名和密码输出到纪事本
public class test_login extends JFrame
{
static String name;
static String pass;
public void write2txt()
{
try
{
File f1 = new File ("c:\\temp\\java1.txt");
f1.createNewFile();
FileWriter f=new FileWriter(f1);
f.write(name+" "+pass);
f.write(13);
f.write(10);
f.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void create()
{
JFrame fra = new JFrame("这里是窗口");
fra.setLayout(new FlowLayout()); //布局管理器
JTextField j1 = new JTextField("用户名:",5); j1.setEnabled(false);
JTextField j2 = new JTextField("密码:",5); j2.setEnabled(false);
final JTextField j3 = new JTextField("",16);
final JTextField j4 = new JPasswordField("",16);
JButton b1 = new JButton("确定");
JButton b2 = new JButton("取消");
class Listener_ok implements ActionListener
{//“确定按钮监听类”
public void actionPerformed(ActionEvent e)
{
name = j3.getText();
pass = j4.getText();
if (name.compareTo("")==0||pass.compareTo("")==0)
{//用户名密码不为空
JOptionPane.showMessageDialog(null,"用户名或密码为空,请重新输入","错误", JOptionPane.PLAIN_MESSAGE);
}
else
{//这里开始写盘
System.out.println("写盘");
write2txt();
}
}
}
b1.addActionListener(new Listener_ok());
class Listener_cancel implements ActionListener
{//“取消”按钮监听类
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
}
b2.addActionListener(new Listener_cancel());
fra.add(j1);
fra.add(j3);
fra.add(j2);
fra.add(j4);
fra.add(b1);
fra.add(b2);
fra.setSize(300,150);
fra.setVisible(true);
}
static public void main(String args[])
{
test_login w1 = new test_login();
w1.create();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询