如何使用Java在JApplet中写入textfile.txt [英] How do you write into a textfile.txt in a JApplet using Java

查看:82
本文介绍了如何使用Java在JApplet中写入textfile.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的小程序代码:

/主程序

Here''s my applet code:

/Main Program

//Gross and Net biweekely salary
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;  
import java.lang.String; 
import java.io.*;
import java.util.ArrayList;
//Net = after
//gross = before
public class Payrollclear 
{
  JFrame frame;
  JPanel contentPane;
  JLabel prompt1, prompt2, prompt3, prompt4, prompt5, stat;
  JTextField movieT, yearR,time;
  JButton addDvd, exitB;
  
  String[] genreStrings = { "Action \t", "Adventure", "Comedy", "Crime", "Disaster", "Documentary", "Drama", "Epic", "Family", "Fantasy", "Film-noir", "GLBT", "Horror", "Musical", "Mystery", "Romance", "Science Fiction", "Sport", "Thriller", "War", "Western" };	
  JComboBox genre = new JComboBox(genreStrings);

  String[] ageStrings = { "Family","PG","PG 13","16","18","R18"};
  JComboBox ageClass = new JComboBox(ageStrings);
  
  
  public Payrollclear()
  {
    /* Create and set up the frame */
    frame = new JFrame("Add new Dvd(s)");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 220);
    
    /* Create a content pane with a GridLayout and empty borders */
    contentPane = new JPanel();
    contentPane.setLayout(new GridLayout(0, 2, 10, 5));
    contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    contentPane.setBackground(Color.white);
    
    /* Create and add a prompt and then a text field */
    prompt1 = new JLabel("Enter movie title: ");
    contentPane.add(prompt1);
    movieT = new JTextField(10);
    contentPane.add(movieT);
    
    /* Create and add a prompt and then a text field */
    prompt1 = new JLabel("Enter year of release: ");
    contentPane.add(prompt1);
    
    yearR = new JTextField(10);
    contentPane.add(yearR);
    
    /* Create and add a second prompt and then a text field */
    prompt2 = new JLabel("Select age classification: ");
    contentPane.add(prompt2);
    
    contentPane.add(genre);
    
    /* Create and add a third prompt and then a text field */
    prompt3 = new JLabel("Select movie genre: ");
    contentPane.add(prompt3);
    
    contentPane.add(ageClass);
    
    prompt3 = new JLabel("Enter playing time in minutes: ");
    contentPane.add(prompt3);
    
    time = new JTextField(10);
    contentPane.add(time);
    
    /* Create and add button that will display the gross pay */
    addDvd = new JButton("Add DVD");
    addDvd.addActionListener(new addDvdDetails());
    contentPane.add(addDvd);
    
    /*Create and add button that will display the net pay*/
    exitB = new JButton("Exit");
    exitB.addActionListener(new NetPay());
    contentPane.add(exitB);
    
    
    /* Create and add a label that will display stats(whatever is selected) */
    stat = new JLabel(" ");
    stat.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0));
    contentPane.add(stat);
    
  //  String g3 = genre.getSelectedItem();
  //  String g4 = ageClass.getSelectedItem();
    
    /* Add content pane to frame */
    frame.setContentPane(contentPane);
    /* Size and then display the frame. */
    frame.pack();
    frame.setVisible(true);
  }
  
  
  /*
   * PayCalculator class that calculates the gross pay
   */ 
  class addDvdDetails implements ActionListener 
  {
    public void actionPerformed(ActionEvent event) 
    {
      String g1 = movieT.getText();
      String g2 = yearR.getText();
      String g3 = time.getText();
      
      JOptionPane.showMessageDialog(null,"Movies title: "+g1+"\nYear released: "+g2+"\nAge classification: "+ageClass.getSelectedItem()+"\nMovie genre: "+genre.getSelectedItem()+"\nEnter time in minutes: "+g3);

    }
  }


   /* 
   * NetPay class that calculates and displays the Net Pay
   */
  class NetPay implements ActionListener 
  {
    public void actionPerformed(ActionEvent event) 
    {
    	
    	
    //  String g5 = wageT.getText();
     // String g6 = hoursT.getText();
    //  String g7 = charityT.getText();

    //  double net = 0.0;
    //  double charity = 0.0;
//      double x = (Double.parseDouble(g5) * Double.parseDouble(g6)) * 2.00;
     // 
      //Call CPP class to calculate CPP amount;
     // CanadaPensionPlanDeduction cppAmount = new CanadaPensionPlanDeduction();
    //  double cppa = cppAmount.CanadaPensionPlanDeduction(x);
       
      //Call EI class to calculate EI amount;
    //  double s = x * 2.00;
    //  EmploymentInsuranceDeduction eiAmount = new EmploymentInsuranceDeduction();
   //   double ei = eiAmount.EmploymentInsuranceDeduction(x);
      
    //  charity = (Double.parseDouble(g7)/100) * x;
   //   net = (x - charity);
   //   net = net - cppa;
   //   net = net - ei;
   //   stat.setText(" Net Pay = $" + Double.toString(net));
    }
  }

   

private static void runGUI() 
{
  JFrame.setDefaultLookAndFeelDecorated(true);
  Payrollclear myGrade = new Payrollclear();
}


  public static void main(String[] args) 
  {
  		int loopC=0;
  		String mTitle="",ageClass="",genre="";
		int time=0,year=0,total=0,mTime=0,mYear=0;
		
		ArrayList<dvdClass> movies = new ArrayList<dvdClass>();
	
	String loopCo = JOptionPane.showInputDialog(null,"Please enter number of Dvds u want to input");
	loopC = Integer.parseInt(loopCo);
	
	
		// Some data to load into a list.
		String[] movTitle = new String[loopC];
		int[] movYear = new int[loopC];
		String[] movAgeClass = new String[loopC];
		String[] movGenre = new String[loopC];
		int[] movTime = new int[loopC];
		int x=0;
		
	
		
  	
    javax.swing.SwingUtilities.invokeLater(new Runnable() 
    {
      public void run() 
      {
        runGUI();
      }
    });
    	if(x<loopC)
		{
    	//	time = Integer.parseInt(mTime);
		//	year = Integer.parseInt(mYear);
			
			dvdClass mov = new dvdClass(mTitle,year,ageClass,genre,time);
			movies.add (mov);
			
    		movTitle[x] = mTitle;
			movYear[x] = year;
			movAgeClass[x] = ageClass;
			movGenre[x] = genre;
			movTime[x] = time;
			
			x = x+1;
    
    }
    else
    
    				
				for(int y=0;y<loopC;y++)
				{	
					dvdClass dvdC = new dvdClass(movTitle[y],movYear[y],movAgeClass[y],movGenre[y],movTime[y]);
					dvdC.display();
				}
			
			
  }
}

推荐答案

"+ Double.toString(net)); } } 私有 静态 void runGUI() { JFrame.setDefaultLookAndFeelDecorated(true); Payrollclear myGrade = Payrollclear(); } 公共 静态 void main(字符串 []参数) { int loopC = 0; 字符串 mTitle = " ,ageClass = " ,genre = " "; int time = 0,year = 0,total = 0,mTime = 0,mYear = 0; ArrayList< dvdClass>电影= ArrayList< dvdClass>(); 字符串 loopCo = JOptionPane.showInputDialog(null," ); loopC = Integer.parseInt(loopCo); // 一些要加载到列表中的数据. 字符串 [] movTitle = 字符串 [ loopC]; int [] movYear = int [ loopC]; 字符串 [] movAgeClass = 字符串 [ loopC]; 字符串 [] movGenre = 字符串 [ loopC]; int [] movTime = int [ loopC]; int x = 0; javax.swing.SwingUtilities.invokeLater( Runnable() { 公共 无效 run() { runGUI(); } }); 如果(x< loopC) { // time = Integer.parseInt(mTime); // year = Integer.parseInt(mYear); dvdClass mov = dvdClass(mTitle,year,ageClass,类型,时间); movie.add(mov); movTitle [x] = mTitle; movYear [x] =年; movAgeClass [x] = ageClass; movGenre [x] =体裁; movTime [x] =时间; x = x + 1; } 其他 for ( int y = 0; y< loopC; y ++) { dvdClass dvdC = dvdClass(movTitle [y],movYear [y],movAgeClass [y],movGenre [y],movTime [y]); dvdC.display(); } } }
" + Double.toString(net)); } } private static void runGUI() { JFrame.setDefaultLookAndFeelDecorated(true); Payrollclear myGrade = new Payrollclear(); } public static void main(String[] args) { int loopC=0; String mTitle="",ageClass="",genre=""; int time=0,year=0,total=0,mTime=0,mYear=0; ArrayList<dvdClass> movies = new ArrayList<dvdClass>(); String loopCo = JOptionPane.showInputDialog(null,"Please enter number of Dvds u want to input"); loopC = Integer.parseInt(loopCo); // Some data to load into a list. String[] movTitle = new String[loopC]; int[] movYear = new int[loopC]; String[] movAgeClass = new String[loopC]; String[] movGenre = new String[loopC]; int[] movTime = new int[loopC]; int x=0; javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { runGUI(); } }); if(x<loopC) { // time = Integer.parseInt(mTime); // year = Integer.parseInt(mYear); dvdClass mov = new dvdClass(mTitle,year,ageClass,genre,time); movies.add (mov); movTitle[x] = mTitle; movYear[x] = year; movAgeClass[x] = ageClass; movGenre[x] = genre; movTime[x] = time; x = x+1; } else for(int y=0;y<loopC;y++) { dvdClass dvdC = new dvdClass(movTitle[y],movYear[y],movAgeClass[y],movGenre[y],movTime[y]); dvdC.display(); } } }


从Applet作为基于Web的应用程序执行I/O受到安全限制.

尽管可以删除安全限制.
为此,您需要创建一个策略文件,告知您的应用程序将使用I/O,并且在部署时必须要求客户端安装策略文件.
There are security restriction to perform I/O from an Applet as a web based application.

Although you can remove the security restriction.
To do so you have a create a policy file telling that your application will use I/O and at the time of your deployment you have to ask your client to install the policy file.


这篇关于如何使用Java在JApplet中写入textfile.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆