该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这是登录ActionHandle
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class ActionHandle{
private JFrame frame = new JFrame("Welcome To") ;
private JButton submit = new JButton("登陆");
private JButton reset = new JButton("重置");
private JLabel nameLab = new JLabel("用户名:") ;
private JLabel passLab = new JLabel("密 码:") ;
private JLabel infoLab = new JLabel("用户登陆系统") ;
private JTextField nameText = new JTextField(10) ;
private JPasswordField passText = new JPasswordField() ;
private JPanel pan = new JPanel() ;
public ActionHandle(){
Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,12) ;
this.passText.setEchoChar('*');
infoLab.setFont(fnt) ;// 设置标签的显示文字
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==submit){
String tname = nameText.getText() ;
String tpass = new String(passText.getPassword()) ;
LoginCheck log = new LoginCheck(tname,tpass) ;
if(log.validate()){
infoLab.setText("登陆成功,欢迎光临!") ;
}else{
infoLab.setText("登陆失败,错误的用户名或密码!") ;
}
}
}
}) ;
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
nameText.setText("") ;
passText.setText("") ;
infoLab.setText("用户登陆系统") ;
}
}
}) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ;// 加入事件
JButton jb=new JButton("ca0");
frame.setLayout(null) ;
nameLab.setBounds(5,5,60,20) ;
passLab.setBounds(5,30,60,20) ;
infoLab.setBounds(5,65,220,30) ;
nameText.setBounds(65,5,100,20) ;
passText.setBounds(65,30,100,20) ;
submit.setBounds(165,5,60,20) ;
reset.setBounds(165,30,60,20) ;
frame.add(nameLab) ;
frame.add(passLab) ;
frame.add(infoLab) ;
frame.add(nameText) ;
frame.add(passText) ;
frame.add(submit) ;
frame.add(reset) ;
frame.setSize(280,130) ;
frame.setBackground(Color.WHITE) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
};
这是LoginCheck界面
import java.io.File;
import java.util.Scanner;
class LoginCheck{
private String name ;
private String password;
public LoginCheck(String name,String password){
this.name = name ;
this.password=password;
}
public boolean validate(){
File f=new File("h:"+File.separator+"cao.txt");
Scanner fin=null;
try
{
fin=new Scanner(f);
}catch(Exception e){}
String user=fin.next();
String code=fin.next();
if(name.equals(user)&&password.equals(code))
{
return true ;
}else
{
return false ;
}
}
};
这是一个文件的保存的打开
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.PrintStream ;
import java.util.Scanner ;
import java.awt.BorderLayout ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.JFrame ;
import javax.swing.JTextArea ;
import javax.swing.JLabel ;
import javax.swing.JButton ;
import javax.swing.JPanel ;
import javax.swing.JFileChooser ;
import javax.swing.JScrollPane ;
class Note implements ActionListener {
private JTextArea area = new JTextArea(8,10) ;// 定义文本区
private JFrame f2 = new JFrame("Welcome To") ;
private JButton open = new JButton("打开文件") ;
private JButton save = new JButton("保存文件") ;
private JLabel label = new JLabel("现在没有打开的文件") ;
private JPanel butPan = new JPanel() ;
public Note()
{
this.butPan.add(open) ;// 在面板中加入按钮
this.butPan.add(save) ;// 在面板中加入按钮
this.f2.setLayout(new BorderLayout(3,3)) ;
this.f2.add(this.label,BorderLayout.NORTH) ;
this.f2.add(this.butPan,BorderLayout.SOUTH) ;
this.f2.add(new JScrollPane(this.area),BorderLayout.CENTER) ;
this.f2.setSize(330,180) ;
this.f2.setVisible(true) ;
this.f2.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}
) ;
this.open.addActionListener(this) ;
this.save.addActionListener(this) ;
}
public void actionPerformed(ActionEvent e){
File file = null ;// 接收文件
int result = 0 ;// 接收操作状态
JFileChooser fileChooser = new JFileChooser() ;// 文件选择框
if(e.getSource()==this.open){// 表示执行的是打开操作
this.area.setText("") ;// 打开将文字区域的内容清空
fileChooser.setApproveButtonText("确定") ;
fileChooser.setDialogTitle("打开文件") ;
result = fileChooser.showOpenDialog(this.f2) ;
if(result==JFileChooser.APPROVE_OPTION){// 选择的是确定按钮
file = fileChooser.getSelectedFile() ;// 得到选择的文件
this.label.setText("打开的文件名称为:" + file.getName()) ;
}else if(result==JFileChooser.CANCEL_OPTION){
this.label.setText("没有选择任何文件") ;
}else{
this.label.setText("操作出现错误") ;
}
if(file!=null){
try{
Scanner scan = new Scanner(new FileInputStream(file)) ;
scan.useDelimiter("\n") ;
while(scan.hasNext()){
this.area.append(scan.next()) ;
this.area.append("\n") ;
}
scan.close() ;
}catch(Exception e1){}
}
}
if(e.getSource()==this.save){// 判断是否是保存操作
result = fileChooser.showSaveDialog(this.f2) ;// 显示保存框
if(result==JFileChooser.APPROVE_OPTION){// 选择的是确定按钮
file = fileChooser.getSelectedFile() ;// 得到选择的文件
this.label.setText("选择的存储文件名称为:" + file.getName()) ;
}else if(result==JFileChooser.CANCEL_OPTION){
this.label.setText("没有选择任何文件") ;
}else{
this.label.setText("操作出现错误") ;
}
if(file!=null){
try{
PrintStream out = new PrintStream(new FileOutputStream(file)) ;
out.print(this.area.getText()) ;
out.close() ;
}catch(Exception e1){}
}
}
}
}
这是主方法
public class Main{
public static void main(String args[])throws Exception{
new ActionHandle() ;
new Note();
}
};
我怎么改才能把登录成功之后打开文件管理界面