我的文件是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!!!
}
}
}
我只是想打开的文件,没有别的..
看看'java.io'包,你会发现打开一个文件,读/写它所需的所有东西,等等。'BufferedReader'是一个基本的标准需求,可以让你阅读'String's。 – 2014-12-13 08:43:48
避免使用'null'布局,像素完美的布局是现代UI设计中的幻想。影响组件的个体大小的因素太多,其中没有一个可以控制。 Swing旨在与布局经理一起工作,放弃这些将导致问题和问题无法结束,您将花费越来越多的时间来尝试纠正 – MadProgrammer 2014-12-13 08:46:19
Applets驻留在一个非常有限的沙盒中,这会阻止他们执行那些您可能通常会执行基本I/O操作,请参阅[Applets可以和不可以执行的操作](https://docs.oracle.com/javase/tutorial/deployment/applet/security.html)以获取更多详细信息 – MadProgrammer 2014-12-13 08:48:18