简体   繁体   English

我想在新窗口中单击Java中的按钮即可打开文件

[英]I want to open a file just with one click of button in java in a new window

My file is 1.txt and I want to open it using java such that on pressing a button, that file should open up!! 我的文件是1.txt,我想使用java来打开它,以便在按下按钮时打开该文件!
Is there any command through actionlistener to open the file and patch it with the button?? 是否有通过actionlistener发出的命令来打开文件并使用按钮对其打补丁?
This is my simple applet program in java.. 这是我在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!!!

                    }
    }
}

I just want to open the file, nothing else.. 我只想打开文件,没别的。

One can use the Desktop class to let the system open, edit, print, browse a file. 可以使用Desktop类让系统打开,编辑,打印,浏览文件。

Desktop.getDesktop().open(file);

This means an external application. 这意味着外部应用程序。

You might consider writing an HTML file, to make all a bit more stylish. 您可能会考虑编写HTML文件,以使外观更加时尚。


After coomenting of @MadProgrammer: 在@MadProgrammer进行拼写之后:

For a sandboxed applet, that has extra security restrictions when run over the internet, it might be better to let the applet open a second window and display the text. 对于在Internet上运行时具有额外安全限制的沙盒小程序,最好让小程序打开第二个窗口并显示文本。 The text then need not be written to a file. 然后,无需将文本写入文件。

If you want the file to be on your server to save the typed data, that would be a wrong thought: the applet is run in the client's browser. 如果希望文件位于服务器上以保存键入的数据,那将是一个错误的想法:applet在客户端的浏览器中运行。

below it is my code to open an .exe in my netbeans GUI program: 下面是在我的netbeans GUI程序中打开.exe的代码:

 private void clockActionPerformed(java.awt.event.ActionEvent evt) {                                      
      try { 
            // file path to  open
            File u = new File("C:\\Program Files (x86)\\Desktop Countdown Timer\\DesktopCountdownTimer.exe"); 

            Desktop d = Desktop.getDesktop(); 
            d.open(u); 
        } catch (IOException eevt) { 
            JOptionPane.showMessageDialog(this, eevt.getMessage()); 
        } 
    }        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM