Ввод файла Java-апплета

#java #file-io #applet #japplet #calculated-field

#java #file-io #апплет #japplet #вычисляемое поле

Вопрос:

Я пытаюсь создать апплет, который вычисляет значения, а также принимает файл и отображает его в текстовом поле. Я знаю, что я относительно близок к этому, но я не могу заставить программу работать, говоря, что она не инициализирована. Любая помощь была бы высоко оценена.

 import java.awt.*;
import java.applet.*;
import java.util.*; 
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;


public class FoV2_Calculator extends JApplet
{
    double FoV = 0, nt = 0, gamma = 0;
    String nr, wl, es;
    JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9;
    JButton calculate;
    JTextField t1, t2, t3;
    JTextArea a1;
    String fileToRead = "test.txt";
    StringBuffer strBuff;

public static void main(String[] args)
{
    new FoV2_Calculator();
}
public FoV2_Calculator()
{
    this.setSize(1000,400);
    this.add(getCustPanel());       
    this.setVisible(true);
}


private JPanel getCustPanel() 
{   
    JPanel panel = new JPanel ();
    SpringLayout layout = new SpringLayout();
    panel.setLayout (layout);

    a1 = new JTextArea(10, 80);
    a1.setEditable(false);

    l1 = new JLabel("Input");
    layout.putConstraint(SpringLayout.WEST, l1, 5,  SpringLayout.WEST, panel);
    panel.add(l1);

    l2 = new JLabel("Intermediate Values");
    layout.putConstraint(SpringLayout.WEST, l2, 400,  SpringLayout.WEST, panel);
    panel.add(l2);

    l3 = new JLabel("Output");
    layout.putConstraint(SpringLayout.WEST, l3, 650,  SpringLayout.WEST, panel);
    panel.add(l3);

    l4 = new JLabel("Number of Rings");
    layout.putConstraint(SpringLayout.WEST, l4, 5,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, l4, 50,  SpringLayout.NORTH, panel);
    panel.add(l4);

    l5 = new JLabel("Wavelength");
    layout.putConstraint(SpringLayout.WEST, l5, 5,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, l5, 100,  SpringLayout.NORTH, panel);
    panel.add(l5);

    l6 = new JLabel("Emitter Space");
    layout.putConstraint(SpringLayout.WEST, l6, 5,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, l6, 150,  SpringLayout.NORTH, panel);
    panel.add(l6);

    t1 = new JTextField(20);
    layout.putConstraint(SpringLayout.WEST, t1, 115,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, t1, 50,  SpringLayout.NORTH, panel);
    panel.add(t1);


    t2 = new JTextField(20);
    layout.putConstraint(SpringLayout.WEST, t2, 115,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, t2, 100,  SpringLayout.NORTH, panel);
    panel.add(t2);

    t3 = new JTextField(20);
    layout.putConstraint(SpringLayout.WEST, t3, 115,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, t3, 150,  SpringLayout.NORTH, panel);
    panel.add(t3);

    l6 = new JLabel("Grating Lobe Separation      "   gamma);
    layout.putConstraint(SpringLayout.WEST, l6, 400,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, l6, 50,  SpringLayout.NORTH, panel);
    panel.add(l6);

    l7 = new JLabel("Number of Tiles                      "   nt);
    layout.putConstraint(SpringLayout.WEST, l7, 400,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, l7, 100,  SpringLayout.NORTH, panel);
    panel.add(l7);

    l8 = new JLabel("Field of View      "   FoV);
    layout.putConstraint(SpringLayout.WEST, l8, 650,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, l8, 50,  SpringLayout.NORTH, panel);
    panel.add(l8);

    calculate = new JButton("Calculate");
    layout.putConstraint(SpringLayout.WEST, calculate, 825,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, calculate, 425,  SpringLayout.NORTH, panel);
    panel.add(calculate);


    layout.putConstraint(SpringLayout.WEST, a1, 25,  SpringLayout.WEST, panel);
    layout.putConstraint(SpringLayout.NORTH, a1, 200,  SpringLayout.NORTH, panel);

    String prHtml = this.getParameter("fileToRead");
    if (prHtml != null) fileToRead = new String(prHtml);  
    readFile();



    panel.add(a1);


    calculate.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            nr = t1.getText();
            wl = t2.getText();
            es = t3.getText();

            double nrNUM = Double.parseDouble(nr);
            double wlNUM = Double.parseDouble(wl);
            double esNUM = Double.parseDouble(es);

            gamma = wlNUM / (2 * nrNUM);
            nt = (nrNUM   100);
            FoV = gamma * (nrNUM   90);

            l6.setText("Grating Lobe Separation      "   String.format("%.3g%n", gamma));
            l7.setText("Number of Tiles                      "   String.format("%.3g%n", nt));
            l8.setText("Field of View      "   String.format("%.3g%n", FoV));
        }
    });
    return panel;
}
 public void readFile(){
  String line;
  URL url = null;
  try{
     url = new URL(getCodeBase(), fileToRead);
  }
  catch(MalformedURLException e){}
  try{
     InputStream in = url.openStream();
     BufferedReader bf = new BufferedReader
     (new InputStreamReader(in));
     strBuff = new StringBuffer();
     while((line = bf.readLine()) != null){
        strBuff.append(line   "n");
     }
     a1.append("File Name : "   fileToRead   "n");
     a1.append(strBuff.toString());
  }
  catch(IOException e){
  e.printStackTrace();
   }

}



}
  

Ответ №1:

Апплеты не используют public static void main(String[] args) . Они используют инициализацию.

Заменить

 public static void main(String[] args)
{
    new FoV2_Calculator();
}
public FoV2_Calculator()
{
    this.setSize(1000,400);
    this.add(getCustPanel());       
    this.setVisible(true);
}
  

С

 public void init()
{
    this.setSize(1000,400);
    this.add(getCustPanel());       
    this.setVisible(true);
}