我想在新窗口中单击java中的按钮打开一个文件

时间:2014-12-13 08:40:07

标签: java applet

我的文件是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!!!

                    }
    }
}

我只想打开文件,别的......

2 个答案:

答案 0 :(得分:1)

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

Desktop.getDesktop().open(file);

这意味着外部应用程序。

您可以考虑编写HTML文件,以使其更加时尚。


@MadProgrammer关注后:

对于沙盒applet,在互联网上运行时有额外的安全限制, 让applet打开第二个窗口并显示文本可能会更好。然后不需要将文本写入文件。

如果您希望文件在您的服务器上 来保存输入的数据,那将是错误的想法:applet在客户端的浏览器中运行。< / p>

答案 1 :(得分:0)

下面是在我的netbeans GUI程序中打开.exe的代码:

{{1}}