2014-12-13 56 views
1

我的文件是1.txt,我想用java打开它,这样按下按钮时,该文件应该打开!
是否有任何命令通过actionlistener打开文件并用按钮打补丁?
这是Java我简单的Applet程序..
我想打开一个文件,只需点击一下按钮,在一个新的窗口

import java.awt.*; 
import java.applet.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.io.PrintWriter; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class CalculatorApplet extends Applet implements ActionListener { 

    Button save, view; 
    Label fname, lname, email, city, phno; 
    TextField t, u, v, w, x; 
    Label ans; 
    Scanner sc = new Scanner(System.in); 

    @Override 
    public void init() { 
     setLayout(null); 

     // create label to display enter no 
     fname = new Label("Enter First Name : "); 
     fname.setBounds(10, 50, 100, 20); 

     lname = new Label("Enter Last Name : "); 
     lname.setBounds(10, 70, 100, 20); 

     email = new Label("Enter Email : "); 
     email.setBounds(10, 90, 80, 20); 

     city = new Label("Enter City : "); 
     city.setBounds(10, 110, 80, 20); 

     phno = new Label("Enter Phno : "); 
     phno.setBounds(10, 130, 80, 20); 

     // create textbox for entering number 
     t = new TextField(); 
     t.setBounds(120, 50, 200, 20); 

     u = new TextField(); 
     u.setBounds(120, 70, 100, 20); 

     v = new TextField(); 
     v.setBounds(120, 90, 200, 20); 

     w = new TextField(); 
     w.setBounds(120, 110, 80, 20); 

     x = new TextField(); 
     x.setBounds(120, 130, 80, 20); 

     // create button for finding sqr 
     save = new Button("Save"); 
     save.setBounds(120, 150, 70, 30); 

     // add the action listner on this button 
     save.addActionListener(this); 

     // create button 
     view = new Button("View"); 
     view.setBounds(190, 150, 70, 30); 

     // add the action listner on this button 
     view.addActionListener(this); 

     // add all the components to the frame 
     add(fname); 
     add(lname); 
     add(email); 
     add(city); 
     add(phno); 
     add(t); 
     add(u); 
     add(v); 
     add(w); 
     add(x); 
     add(save); 
     add(view); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     String fnme = t.getText(); 
     String lnme = u.getText(); 
     String emal = v.getText(); 
     String cty = w.getText(); 
     String phn = x.getText(); 

     if (e.getSource() == save) { 
      try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("1.txt", true)))) { 
       out.println("First Name :" + fnme); 
       out.println("Last Name :" + lnme); 
       out.println("Email Name : " + emal); 
       out.println("City : " + cty); 
       out.println("Contact : " + phn); 
       out.println("----------------------------------------\n"); 
       out.println("----------------------------------------\n"); 
       out.close(); 
       t.setText(""); 
       u.setText(""); 
       v.setText(""); 
       w.setText(""); 
       x.setText(""); 
      } catch (Exception ex) { 
       Logger.getLogger(CalculatorApplet.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     }else if(e.getSource() == view){ 
//   what to type in here???? 
//   needed code here!!! 

        } 
    } 
} 

我只是想打开的文件,没有别的..

+0

看看'java.io'包,你会发现打开一个文件,读/写它所需的所有东西,等等。'BufferedReader'是一个基本的标准需求,可以让你阅读'String's。 – 2014-12-13 08:43:48

+0

避免使用'null'布局,像素完美的布局是现代UI设计中的幻想。影响组件的个体大小的因素太多,其中没有一个可以控制。 Swing旨在与布局经理一起工作,放弃这些将导致问题和问题无法结束,您将花费越来越多的时间来尝试纠正 – MadProgrammer 2014-12-13 08:46:19

+0

Applets驻留在一个非常有限的沙盒中,这会阻止他们执行那些您可能通常会执行基本I/O操作,请参阅[Applets可以和不可以执行的操作](https://docs.oracle.com/javase/tutorial/deployment/applet/security.html)以获取更多详细信息 – MadProgrammer 2014-12-13 08:48:18

回答

1

人们可以使用Desktop类,让系统打开,编辑,打印,浏览文件。

Desktop.getDesktop().open(file); 

这意味着一个外部应用程序。

你可能会考虑编写一个HTML文件,使所有的一点点时尚。


@MadProgrammer的coomenting后:

对于沙盒的小程序,当在互联网上运行具有更高的安全性限制, 它可能是更好地让小程序打开第二个窗口并显示文字。文本不需要写入文件。

如果你想要的文件是你的服务器保存类型的数据上,那将是一个错误的想法:小程序在客户端的浏览器中运行。

+0

从[JavaDocs](https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open(java.io)中的applet上下文中, .File))*“SecurityException - 如果安全管理器存在且其SecurityManager.checkRead(java。lang.String)方法拒绝对文件的读取访问,或者拒绝AWTPermission(“showWindowWithoutWarningBanner”)权限,或者调用线程不被允许创建子进程“* – MadProgrammer 2014-12-13 08:50:30

+0

'Runtime.getRuntime().exec(”notepad“ + file);'用你想使用的程序替换'notepad' – Charlie 2014-12-13 10:07:27

+0

@MadProgrammer事实上,一个文件被写入,误导了我的结论是它被用在同一台计算机上,但你是对的,也许我应该提到:* *文件写作可能在互联网上受到保护** – 2014-12-13 18:58:14

相关问题