当前位置:首页 > java > 正文

java开源门户系统源码

  • java
  • 2024-05-30 22:30:26
  • 4999

⑴急求一段简单的java源代码(用户名、密码操作界面)
下面的程序可以直接编译运行,就可以找到自己想要使用的代码段了。
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.FocusEvent;
importjava.awt.event.FocusListener;
importjava.io.File;
importjava.io.IOException;
importjava.io.RandomAccessFile;
importjavax.swing.BoxLayout;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JPasswordField;
importjavax.swing.JTextField;
publicclassUserLoginextendsJPanelimplementsActionListener{
JTextFielduserjt=null;//用户输入框
JPasswordFieldpwdjt=null;
JTextFieldsysUserjt=null;//系统显示用户名输入框
JTextFieldsysPwdjt=null;
publicUserLogin(){
super(newGridLayout(1,2));
JPaneluserPanel=newJPanel();//用户界面,left this.add(userPanel);
JPaneluserUpPanel=newJPanel();//用户上半部分interface
userPanel.add(userUpPanel);
JPaneluserDownPanel=newJPanel();//用户界面下部
userPanel.add(userDownPanel);
JPanelsysPanel=newJPanel();//系统界面,右
sysPanel.setLayout(newBoxLayout(sysPanel,BoxLayout.Y_AXIS));
this.add(sysPanel);
JPanelsysUserPanel=newJPanel();//系统上半部分界面
sysPanel.add(sysUserPanel);
JPanelsysPwdPanel=newJPanel();//系统下半部分界面
sysPanel.add(sysPwdPanel);
userjt=newJTextField(5);
userjt.setText("用户名");
userUpPanel.add(userjt);
pwdjt=newJPasswordField(5);
pwdjt.setText("密码");
pwdjt.setEchoChar('\0');
userDownPanel.add(pwdjt);
JLabelsysUserjl=newJLabel("用户名:");
sysUserPanel。 add(sysUserjl);
sysUserjt=newJTextField(5);
sysUserPanel.add(sysUserjt);
JLabelsysPwdjl=newJLabel("密码是:");
sysPwdPanel.add(sysPwdjl);
sysPwdjt=newJTextField(5);
sysPwdPanel.add(sysPwdjt);
userjt.addActionListener(this);
pwdjt.addActionListener(this);
userjt.addFocusListener(newFocusListener(){
publicvoidfocusGained(FocusEvente){
if(userjt.getText().equals("用户名"))
userjt.setText("");
}
publicvoidfocusLost(FocusEvente){
if(userjt.getText().equals(""))
userjt.setText("用户名")
}});
pwdjt.addFocusListener(newFocusListener(){
publicvoidfocusGained(FocusEvente){
if(newString(pwdjt.getPassword()).equals("密码")){
pwdjt.setText("");
pwdjt.setEchoChar('*');
}
}
publicvoidfocusLost(FocusEvente){
if(newString(pwdjt.getPassword()).equals("")){
pwdjt.setText("密码");
pwdjt.setEchoChar('\0');
}
}});
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource().equals(userjt)){
pwdjt.requestFocus();
}else{
if(newString(pwdjt.getPassword()).equals("")||userjt.getText().equals("")||userjt.getText().equals("用户名"))return;
sysUserjt.setText(userjt.getText());
sysPwdjt.setText(newString(pwdjt.getPassword()));
try{
writetoFile();
}catch(IOExceptione1){
System.out.println("写入文件时发生异常!");
e1.printStackTrace();
}
}
}
privatevoidwritetoFile()抛出IOException{
Filef=newFile("User_Psd.txt");
//if(!f.exists())f.createNewFile();
RandomAccessFileaccessFile=newRandomAccessFile(f,"rw");
accessFile.seek(accessFile.length());
accessFile.write(("用户:"+userjt.getText()+"\r\n密码:"+newString(pwdjt.getPassword())+"\r\n\r\n").getBytes());
}
publicstaticvoidmain(Stringargs[]){
JFramejf=newJFrame("用户登录模块测试");
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.add(newUserLogin());
jf.setBounds(400,300,280,150);
jf.setVisible(true);
}
}