首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

抛出错误出错的程序

2012-02-22 
抛出异常出错的程序!packageyan1importjavax.swing.*importjava.awt.*importjava.awt.event.*importja

抛出异常出错的程序!
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 ");




}
}
}
}

热点排行