读书人

抛出错误出错的程序

发布时间: 2012-02-22 19:36:55 作者: rapoo

抛出异常出错的程序!
package yan1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class PasException extends Exception
{

public String getMessage(){
return "password is error ";

}



}
public class Yan2 extends JApplet implements ActionListener
{String name;
String custompassword;
JPanel panelobj;
JLabel labname;
JLabel lanpassword;
JButton button;
JTextField text1;
JPasswordField text2;
void setPassword(String password) throws PasException(){
if(password.length() <6||password.length()> 10)
throw new PasException();
else
custompassword=password;
}

public void init(){
panelobj=new JPanel();
getContentPane().add(panelobj);
FlowLayout layout=new FlowLayout(FlowLayout.RIGHT,5,5);
panelobj.setLayout(layout);
labname=new JLabel( "custom name: ");
lanpassword=new JLabel( "password: ");
text1=new JTextField(15);
text2=new JPasswordField(5);
button=new JButton( "logn ");
panelobj.add(labname);
panelobj.add(text1);
panelobj.add(lanpassword);
panelobj.add(text2);
panelobj.add(button);
button.addActionListener(this);
}


public void actionPerformed(ActionEvent evt) {
Object obj=evt.getSource();
if(obj==button)
{name=text1.getText();
custompassword=new String(text2.getPassword());
String entry=name+ ": "+custompassword;
if(name.length()==0)
{
getAppletContext().showStatus( "jiang gao! ");

}
if(custompassword.length()==0)
{ getAppletContext().showStatus( "jiang gao! ");

}
else
{try
{setPassword(custompassword);

}

catch( PasException e)
{

getAppletContext().showStatus(e.getMessage());

return;


}
}
try{
File fileobj=new File( "d:\\yan.txt ");
RandomAccessFile log=new RandomAccessFile(fileobj, "rw ");
log.seek(log.length());
log.writeBytes(entry);


}
catch(IOException e)
{


getAppletContext().showStatus( "not write to file ");






}
}
}
}

有两出错误请高手解决!

[解决办法]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class PasException extends Exception
{

public String getMessage(){
return "password is error ";

}



}
public class Yan2 extends JApplet implements ActionListener
{String name;
String custompassword;
JPanel panelobj;
JLabel labname;
JLabel lanpassword;
JButton button;
JTextField text1;
JPasswordField text2;
void setPassword(String password) throws PasException
{
if(password.length() <6||password.length()> 10)
throw new PasException();
else
custompassword=password;
}

public void init(){
panelobj=new JPanel();
getContentPane().add(panelobj);
FlowLayout layout=new FlowLayout(FlowLayout.RIGHT,5,5);
panelobj.setLayout(layout);
labname=new JLabel( "custom name: ");
lanpassword=new JLabel( "password: ");
text1=new JTextField(15);
text2=new JPasswordField(5);
button=new JButton( "logn ");
panelobj.add(labname);
panelobj.add(text1);
panelobj.add(lanpassword);
panelobj.add(text2);
panelobj.add(button);
button.addActionListener(this);
}


public void actionPerformed(ActionEvent evt) {
Object obj=evt.getSource();
if(obj==button)
{name=text1.getText();
custompassword=new String(text2.getPassword());
String entry=name+ ": "+custompassword;
if(name.length()==0)
{
getAppletContext().showStatus( "jiang gao! ");

}
if(custompassword.length()==0)
{ getAppletContext().showStatus( "jiang gao! ");

}
else
{try
{setPassword(custompassword);

}

catch( PasException e)
{

getAppletContext().showStatus(e.getMessage());

return;


}
}
try{
File fileobj=new File( "d:\\yan.txt ");
RandomAccessFile log=new RandomAccessFile(fileobj, "rw ");
log.seek(log.length());
log.writeBytes(entry);


}
catch(IOException e)
{


getAppletContext().showStatus( "not write to file ");




}
}
}
}

读书人网 >J2SE开发

热点推荐