java源码pdf
⑴ java导出PDF文档
java导出pdf需要用到iText库,iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf
的文档,而且可以将XML、Html文件转化为PDF文件。 
iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用
iText类库了。
代码如下:
public class createPdf { 
//自己做的一个简单例子,中间有图片之类的 
//先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document 
Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F); 
public void getPDFdemo() throws DocumentException, IOException{ 
//这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见: 
//首先 
//字体的定义:这里用的是自带的jar里面的字体 
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); 
// 当然你也可以用你电脑里面带的字体库 
//BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
//定义字体 注意在最新的包里面 颜色是封装的 
Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54)); 
//生成pdf的第一个步骤: 
//保存本地指定路径 
saveLocal(); 
document.open(); 
ByteArrayOutputStream ba = new ByteArrayOutputStream(); 
// PdfWriter writer = PdfWriter.getInstance(document, ba); 
document.open(); 
//获取此编译的文件路径 
String path = this.getClass().getClassLoader().getResource("").getPath(); 
//获取根路径 
String filePath = path.substring(1, path.length()-15); 
//获取图片路径 找到你需要往pdf上生成的图片 
//这里根据自己的获取的路径写 只要找到图片位置就可以 
String picPath = filePath +"\\WebContent" +"\\images\\"; 
//往PDF中添加段落 
Paragraph pHeader = new Paragraph(); 
pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1))); 
//pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 ); 
document.add(pHeader);//在文档中加入你写的内容 
//获取图片 
Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png"); 
//定义图片在文档中显示的绝对位置 
img2.scaleAbsolute(137.0F, 140.0F); 
img2.setAbsolutePosition(330.0F, 37.0F); 
//将图片添加到文档中 
document.add(img2); 
//关闭文档 
document.close(); 
/*//设置文档保存的文件名 
response.setHeader("Content-
disposition", "attachment;filename=\""+ new String(("CCF会员资格确认
函.pdf").getBytes("GBK"),"ISO-8859-1") + "\""); 
//设置类型 
response.setContentType("application/pdf"); 
response.setContentLength(ba.size()); 
ServletOutputStream out = response.getOutputStream(); 
ba.writeTo(out); 
out.flush();*/ 
} 
public static void main(String[]args) throws DocumentException, IOException{ 
createPdf pdf= new createPdf(); 
pdf.getPDFdemo(); 
} 
//指定一个文件进行保存 这里吧文件保存到D盘的text.pdf 
public void saveLocal() throws IOException, DocumentException{ 
//直接生成PDF 制定生成到D盘test.pdf 
File file = new File("D:\\text2.pdf"); 
file.createNewFile(); 
PdfWriter.getInstance(document, new FileOutputStream(file)); 
} 
}
⑵ java怎么输出pdf格式的文件
java导出pdf需要用到iText库,iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf
的文档,而且可以将XML、Html文件转化为PDF文件。 
iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用
iText类库了。
代码如下:
public class createPdf {  
    //自己做的一个简单例子,中间有图片之类的  
    //先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document   
    Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);  
    public  void getPDFdemo() throws DocumentException, IOException{  
        //这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见:  
        //首先  
        //字体的定义:这里用的是自带的jar里面的字体  
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);  
        // 当然你也可以用你电脑里面带的字体库  
        //BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
        //定义字体 注意在最新的包里面 颜色是封装的  
        Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));  
        //生成pdf的第一个步骤:  
        //保存本地指定路径  
         saveLocal();  
         document.open();  
         ByteArrayOutputStream ba = new ByteArrayOutputStream();         
    //   PdfWriter writer = PdfWriter.getInstance(document, ba);  
         document.open();  
         //获取此编译的文件路径  
         String path = this.getClass().getClassLoader().getResource("").getPath();  
         //获取根路径  
         String filePath  = path.substring(1, path.length()-15);  
         //获取图片路径 找到你需要往pdf上生成的图片    
         //这里根据自己的获取的路径写 只要找到图片位置就可以  
         String  picPath = filePath +"\\WebContent" +"\\images\\";  
         //往PDF中添加段落  
         Paragraph pHeader = new Paragraph();  
         pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));  
         //pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 );  
         document.add(pHeader);//在文档中加入你写的内容  
        //获取图片   
         Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png");  
        //定义图片在文档中显示的绝对位置  
         img2.scaleAbsolute(137.0F, 140.0F);  
         img2.setAbsolutePosition(330.0F, 37.0F);  
         //将图片添加到文档中  
         document.add(img2);  
         //关闭文档  
         document.close();  
        /*//设置文档保存的文件名 
         response.setHeader("Content-
disposition", "attachment;filename=\""+ new String(("CCF会员资格确认
函.pdf").getBytes("GBK"),"ISO-8859-1") + "\""); 
        //设置类型 
         response.setContentType("application/pdf"); 
         response.setContentLength(ba.size()); 
         ServletOutputStream out = response.getOutputStream(); 
         ba.writeTo(out); 
         out.flush();*/  
    }  
    public static void main(String[]args) throws DocumentException, IOException{  
        createPdf pdf= new createPdf();  
        pdf.getPDFdemo();  
    }  
      
    //指定一个文件进行保存 这里吧文件保存到D盘的text.pdf  
    public void saveLocal() throws IOException, DocumentException{  
        //直接生成PDF 制定生成到D盘test.pdf  
         File file = new File("D:\\text2.pdf");  
         file.createNewFile();  
         PdfWriter.getInstance(document, new FileOutputStream(file));  
           
    }  
}
⑶ 求编写一个超级简单的Java的程序源代码
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
 public static void main(String args[]) {
  LoginFrm frame = new LoginFrm();
 }
}
class LoginFrm extends JFrame implements ActionListener{
 JLabel nameLabel=new JLabel("用户名:");
 JLabel pwdLabel=new JLabel("密码:");
 JTextField name=new JTextField(10);
 JPasswordField password=new JPasswordField(10);
 JButton butnSure=new JButton("确定");
 JButton butnCancel=new JButton("取消");
 public LoginFrm() {
  super("登陆");
  setBounds(500, 200, 280, 220);
  setVisible(true);
  setLayout(null);
  nameLabel.setBounds(45, 20, 100, 25);
  add(nameLabel);
  add(name);
  name.setBounds(105, 20, 110, 25);
  add(pwdLabel);
  pwdLabel.setBounds(45, 60, 100, 25);
  add(password);
  password.setBounds(105, 60, 110, 25);
  add(butnSure);
  butnSure.setBounds(45, 100, 80, 25);
  add(butnCancel);
  butnCancel.setBounds(135, 100, 80, 25);
  butnSure.addActionListener(this);
  butnCancel.addActionListener(this);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  validate();//刷新橡猜
 }
 public void actionPerformed(ActionEvent e) {
  if (e.getSource() ==butnSure){
   System.out.println("用户名:"+name.getText());
   System.out.println("密码:"+name.getText());
   if("admin".equals(name.getText().trim())&&"123".equals(password.getText().trim())){
    this.dispose();
    new MainFrm("码迅用户界面",name.getText().trim(),password.getText().trim());
   }else {
    JOptionPane.showMessageDialog(this, "用迟如此户不存在");
   }
  }else if(e.getSource()==butnCancel){
   System.exit(1);
  }
 }
 
 class MainFrm extends JFrame{
  private JLabel info;
  
  public MainFrm(String s,String name,String password) {
   super(s);
   setBounds(400, 200, 500, 400);
   setLayout(new FlowLayout());
   info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);
   add(info);
   setVisible(true);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   validate();
  }
 }
}
⑷ 求java题源代码,最好有注释,
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
/**
 * 多线程,小球演示. 打开Windows任务管理器,可看到线程变化。 可搜索到,run()方法/.start()
 * 
 * : 程序技巧体会: 所谓产生一个小球,即是 new 其类对象,其属性携带画小球的 坐标、颜色、所在容器 等参数。
 * 
 * 一个类,属性用来作为参数容器用, 方法....完成功能。
 * */
// 运行类
public class BouncePress {
    //
    public static void main(String[] args) {
        JFrame frame = new BouncePressFrame(); // 生成窗口。执行构造。-----业务逻辑。
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // similar to
                                                                // window
                                                                // listener
        frame.show();
    }
}
class BouncePressFrame extends JFrame {
    private BallPressCanvas canvas;
    public BouncePressFrame() {
        setSize(600, 500); // 窗口大小
        setTitle("Bounce Ball");
        Container contentPane = getContentPane(); // Swing的窗口不能直接放入东西,只能在其上的ContentPane上放。
        canvas = new BallPressCanvas(); // 生成一个新面板。-----canvas
        contentPane.add(canvas, BorderLayout.CENTER); // 窗口中心 加入该面板。
        JPanel buttonPanel = new JPanel(); // 再生成一个新面板。----buttonPanel
        // 调用本类方法addButton。
        addButton(buttonPanel, "Start", // 生成一个按钮"Start"---加入面板buttonPanel
                new ActionListener() { // |------>按钮绑上 action监听器。
                    public void actionPerformed(ActionEvent evt) { // | 小球容器对象的
                        addBall(Thread.NORM_PRIORITY - 4, Color.black); // 事件处理时,执行---addBall()方法。--->产生小球(参数对象)--->加入List中--->开始画球。
                    }
                }); // 按一次,addBall()一次--->产生一个新小球--->加入List中--->开始画此新小球。
                    // --->画球线程BallPressThread的run()--->小球(参数对象).move()--->每次画时,先移动,再判断,再画。
                    // --->BallPressCanvas类的canvas对象.paint()--->自动调BallPressCanvas类的paintComponent(Graphics
                    // g)方法。
                    // --->该方法,从List中循环取出所有小球,第i个球,--->调该小球BallPress类
                    // .draw()方法--->调Graphics2D方法画出小球。--使用color/
        addButton(buttonPanel, "Express", new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                addBall(Thread.NORM_PRIORITY + 2, Color.red);
            }
        });
        addButton(buttonPanel, "Close", new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.exit(0);
            }
        });
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
    }
    public void addButton(Container c, String title, ActionListener listener) {
        JButton button = new JButton(title); // 生成一个按钮。
        c.add(button); // 加入容器中。
        button.addActionListener(listener); // 按钮绑上 action监听器。
    }
    /** 主要业务方法。 */
    public void addBall(int priority, Color color) {
        // 生成 小球(参数对象)
        BallPress b = new BallPress(canvas, color); // 生成BallPress对象,携带、初始化
                                                    // 画Ball形小球,所需参数:所在容器组件,所需color--black/red.
        // 小球加入 List中。
        canvas.add(b); // 面板canvas 的ArrayList中 加入BallPress对象。
        BallPressThread thread = new BallPressThread(b); // 生成画小球的线程类BallPressThread对象。传入BallPress对象(携带了画球所需
                                                            // 容器、color参数)。
        thread.setPriority(priority);
        thread.start(); // call run(), ball start to move
        // 画球线程开始。--->BallPressThread的run()--->小球(参数对象).move()--->先移动,再画。canvas.paint--->BallPressCanvas类的
    }
}
// 画球的线程类。
class BallPressThread extends Thread {
    private BallPress b;
    public BallPressThread(BallPress aBall) {
        b = aBall;
    }
    // 画球开始。
    public void run() {
        try {
            for (int i = 1; i <= 1000; i++) { // 画1000次。
                b.move(); // 每次画时,先移动,再判断,再画。
                sleep(5); // 所以移动比Bounce.java的球慢。
            }
        } catch (InterruptedException e) {
        }
    }
}
// swing面板类.
// 作用1) 本类面板对象.paint()方法---->自动绘制面板,且自动调paintComponent(Graphics
// g)方法,--->重写该方法,绘制面板(及其上组件)。
// 作用2) 该类对象 属性ArrayList balls---兼作小球(参数对象)的容器。
class BallPressCanvas extends JPanel {
    private ArrayList balls = new ArrayList();
    public void add(BallPress b) {
        balls.add(b); // 向ArrayList中添加球。当按下按钮,添加多个球时,都保存在这个List中。
    }
    // 重写了 javax.swing.JComponent的 paintComponent()方法。
    // paint()方法自动调用该方法。
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        for (int i = 0; i < balls.size(); i++) { // 循环
            BallPress b = (BallPress) balls.get(i); // 从List中取出第i个球,
            b.draw(g2); // 画此球。
        }
    }
}
/**
 * 画出球。
 * 
 * 在 canvas上画出,color色的小球图形。
 * 
 * 属性,可用于携带画小球所需参数。
 * 
 * 
 * 
 * @author congan
 * 
 */
class BallPress {
    private Component canvas;
    private Color color;
    private int x = 0;
    private int y = 0;
    private int dx = 2;
    private int dy = 2;
    // 构造 初始化 容器 颜色 参数。
    public BallPress(Component c, Color aColor) {
        canvas = c;
        color = aColor;
    }
    // 制定位置,画出小球。
    public void draw(Graphics2D g2) {
        g2.setColor(color);
        g2.fill(new Ellipse2D.Double(x, y, 15, 15)); // ellipse:椭圆形
    }
    // 移动小球。
    // 每次画时,先移动,再判断,再画。
    // 该方法每次执行,画小球的起点坐标 (x,y), 每次各自+2, 即斜向右下运动。
    public void move() {
        x += dx; // x=x+dx; 画小球的起点坐标 (x,y), 每次各自+2, 即斜向右下运动。
        y += dy; // y=y+dy;
        if (x < 0) { // 小球已到左边框。保证,从左边框开始画。
            x = 0;
            dx = -dx; // 小球横坐标变化值取反。开始反向运动。
        }
        if (x + 15 >= canvas.getWidth()) { // 小球右边已经到画板右边。
            x = canvas.getWidth() - 15;
            dx = -dx; // 开始反向运动。
        }
        if (y < 0) { // 保证,从顶框开始画。
            y = 0;
            dy = -dy;
        }
        if (y + 15 >= canvas.getHeight()) { // 小球已到画板顶。
            y = canvas.getHeight() - 15;
            dy = -dy;
        }
        canvas.paint(canvas.getGraphics()); // 画出面板对象canvas----(及其上所有组件)
                                            // //.paint()方法,自动调用
    }
}
/*import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
*//**
 * 单线程,小球演示 搜索不到,run()方法/.start()
 *//*
public class Bounce {
    public static void main(String[] args) {
        JFrame frame = new BounceFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // similar to
                                                                // window
                                                                // listener
        frame.show();
    }
}
不懂的再问啊。。。
⑸ 求编写一个超级简单的Java的程序源代码
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Caculator extends JFrame implements ActionListener,KeyListener{
 private JTextField tf=new JTextField();
 private float x=0;
 private float y=0;
 private int code=0;
 private boolean enable;
 private boolean first;
 private String str="";
 public Caculator(){
    Container ct=this.getContentPane();
    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    tf.setHorizontalAlignment(JTextField.RIGHT);
    //tf.setText("0");
    enable=true;
    first=true;
    ct.add(tf,BorderLayout.NORTH);
    JPanel panel=new JPanel();
    panel.setLayout(new GridLayout(4,4));
    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
     if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(Caculator.this,"确定要关闭程序吗?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){
      e.getWindow().setVisible(false);
      e.getWindow().dispose();
      System.exit(0);
     }
    }
    });
    Button btn=null;
    btn=new Button("1");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("2");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("3");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("+");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("4");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("5");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("6");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("-");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("7");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("8");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("9");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("*");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("0");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button(".");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("/");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    btn=new Button("=");
    panel.add(btn);
    btn.addActionListener(this);
    btn.addKeyListener(this);
    this.add(panel,BorderLayout.CENTER);
    }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Caculator mainframe=new Caculator();
  mainframe.setTitle("testing Caculator");
  mainframe.setSize(400,400);
  mainframe.setVisible(true);
  
 }
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if(e.getActionCommand()=="+"){
   x= Float.parseFloat(tf.getText());
   code=0;
   this.tf.setText("");
   
  }
  if(e.getActionCommand()=="-"){
   x= Float.parseFloat(tf.getText());
   code=1;
   this.tf.setText("");
   
  }
  if(e.getActionCommand()=="*"){
   x= Float.parseFloat(tf.getText());
   code=2;
   this.tf.setText("");
   
  }
  if(e.getActionCommand()=="/"){
   x= Float.parseFloat(tf.getText());
   code=3;
   this.tf.setText("");
   
  }
  
  if(e.getActionCommand()!="+"&&e.getActionCommand()!="-"&&e.getActionCommand()!="*"&&e.getActionCommand()!="/"&&e.getActionCommand()!="="){
   if(enable){
    if(first){
     System.out.println("haha");
     tf.setText(e.getActionCommand());
     first=false;
    }
    else {
     tf.setText(tf.getText()+e.getActionCommand());
    }
   
   }
else {
   tf.setText(e.getActionCommand());
   enable=true;
   
      }
    }
  if(e.getActionCommand()=="="){
   switch(code){
   case 0:
        y=x+Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break;
   case 1:
        y=x-Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break;
   case 2:
        y=x*Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break;
   case 3:
        y=x/Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break; 
   }
   
  }
  
 }
 public void keyPressed(KeyEvent e) {
  // TODO Auto-generated method stub
  if(e.getKeyChar()=='+'){
   x= Float.parseFloat(tf.getText());
   code=0;
   this.tf.setText("");
   
  }
  if(e.getKeyChar()=='-'){
   x= Float.parseFloat(tf.getText());
   code=1;
   this.tf.setText("");
   
  }
  if(e.getKeyChar()=='*'){
   x= Float.parseFloat(tf.getText());
   code=2;
   this.tf.setText("");
   
  }
  if(e.getKeyChar()=='/'){
   x= Float.parseFloat(tf.getText());
   code=3;
   this.tf.setText("");
   
  }
  
  if(e.getKeyChar()=='1'||e.getKeyChar()=='2'||e.getKeyChar()=='3'||e.getKeyChar()=='0'
   ||e.getKeyChar()=='4'||e.getKeyChar()=='5'||e.getKeyChar()=='6'||e.getKeyChar()=='.'
   ||e.getKeyChar()=='7'||e.getKeyChar()=='8'||e.getKeyChar()=='9'){
   System.out.println("hai");
   if(enable){
    if(first){
     System.out.println("hehe");
     str=Character.toString(e.getKeyChar());
     tf.setText(str);
     first=false;
    }
    else {
     
     str=Character.toString(e.getKeyChar());
     tf.setText(tf.getText()+str);
    }
   
   }
else {
       
       str=Character.toString(e.getKeyChar());
       tf.setText(str);
       enable=true;
   
      }
    }
  if(e.getKeyCode()==KeyEvent.VK_ENTER){
   switch(code){
   case 0:
        y=x+Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break;
   case 1:
        y=x-Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break;
   case 2:
        y=x*Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break;
   case 3:
        y=x/Float.parseFloat(this.tf.getText());
        tf.setText(Float.toString(y));
        enable=false;
        break; 
   }
   
  }
 }
 public void keyReleased(KeyEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 public void keyTyped(KeyEvent arg0) {
  // TODO Auto-generated method stub
  
 }
}
