További appletek készítése



SzovegApplet.java: a szerveren levõ fájlok adatainak kiírása

a SzovegApplet.html fájl kódja:

<html>
<head>
<title>Fájlkiíró Applet</title>
</head>
<body>
<applet code="SzovegApplet.class" width=600 height=450>
</applet>
</body>
</html>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*; 

public class SzovegApplet extends Applet implements ActionListener {

 public Label label=new Label("letöltendõ fájl neve:");
 public TextField field=new TextField();
 public Button oke=new Button("OK");
 public TextArea kiir=new TextArea();

 public void init() {
  setLayout(null);
  label.setBounds(5,5,120,20);
  label.setBackground(Color.yellow);
  label.setForeground(Color.blue);
  label.setFont(new Font("Arial",Font.BOLD+Font.ITALIC,12));
  add(label);
  field.setBounds(130,5,430,20);
  add(field);
  oke.setBounds(565,5,30,20);
  add(oke);
  oke.addActionListener(this);
  kiir.setBounds(5,30,590,415);
  add(kiir);
  }

 public void actionPerformed(ActionEvent ev) {
  String f=field.getText();
  URL url;
  URLConnection kapcsolat;
  try {
   url=new URL(getCodeBase(),f);
   kapcsolat=url.openConnection();
   String protocol=url.getProtocol();
   String host=url.getHost();
   int port=url.getPort();
   String file=url.getFile();
   String contentType=kapcsolat.getContentType();
   int contentLength=kapcsolat.getContentLength();
   kiir.append("URL: "+url.toString()+"\n");
   kiir.append("protocol: "+protocol+"\n");
   kiir.append("host: "+host+"\n");
   kiir.append("port: "+port+"\n");
   kiir.append("file: "+file+"\n");
   kiir.append("Content-Type: "+contentType+"\n");
   kiir.append("Content-Length: "+contentLength+"\n\n");
   }
  catch(MalformedURLException e) {
   kiir.append("Hibas URL!"+"\n\n");
   }
  catch(Exception e) {
   kiir.append("Egyeb hiba"+"\n\n");
   }
  validate();
  }

 public void paint(Graphics g) {
  setBackground(Color.cyan);
  }
 }

1. variáció (SzovegAppletV1.java): a szerveren levõ fájlok adatainak kiírása és a fájlok letöltése

a SzovegApplet.html fájl kódja:

<html>
<head>
<title>Fájlkiíró Applet</title>
</head>
<body>
<applet code="SzovegAppletV1.class" width=600 height=450>
</applet>
</body>
</html>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*; 
import java.io.*; 

public class SzovegAppletV1 extends Applet implements ActionListener {

 public Label label=new Label("letöltendõ fájl neve:");
 public TextField field=new TextField();
 public Button oke=new Button("OK");
 public TextArea adatok=new TextArea();
 public TextArea kiir=new TextArea();

 public void init() {
  setLayout(null);
  label.setBounds(5,5,120,20);
  label.setBackground(Color.yellow);
  label.setForeground(Color.blue);
  label.setFont(new Font("Arial",Font.BOLD+Font.ITALIC,12));
  add(label);
  field.setBounds(130,5,430,20);
  add(field);
  oke.setBounds(565,5,30,20);
  add(oke);
  oke.addActionListener(this);
  adatok.setBounds(5,30,590,110);
  add(adatok);
  kiir.setBounds(5,145,590,300);
  kiir.setFont(new Font("Times New Roman",Font.BOLD,12));
  add(kiir);
  }

 public void actionPerformed(ActionEvent ev) {
  String f=field.getText();
  URL url;
  URLConnection kapcsolat;
  kiir.setText("");
  try {
   url=new URL(getCodeBase(),f);
   kapcsolat=url.openConnection();
   String protocol=url.getProtocol();
   String host=url.getHost();
   int port=url.getPort();
   String file=url.getFile();
   String contentType=kapcsolat.getContentType();
   int contentLength=kapcsolat.getContentLength();
   adatok.append("URL: "+url.toString()+"\n");
   adatok.append("protocol = "+protocol+";   ");
   adatok.append("host = "+host+";   ");
   adatok.append("port = "+port+";   ");
   adatok.append("file = "+file+"\n");
   adatok.append("Content-Type: "+contentType+"\n");
   adatok.append("Content-Length: "+contentLength+"\n\n");
   adatok.setCaretPosition(0);
   LineNumberReader tart=new LineNumberReader(
                         new InputStreamReader(kapcsolat.getInputStream()));
   String s=tart.readLine();
   while(s!=null) {
    kiir.append(s+"\n");
    s=tart.readLine();
    }
   kiir.setCaretPosition(0);
   }
  catch(MalformedURLException e) {
   kiir.append("Hibas URL!"+"\n\n");
   }
  catch(IOException e) {
   kiir.append("Sikertelen letoltes!"+"\n\n");
   }
  catch(Exception e) {
   kiir.append("Egyeb hiba"+"\n\n");
   }
  validate();
  }

 public void paint(Graphics g) {
  setBackground(Color.cyan);
  }
 }



KepApplet.java: a szerveren levõ képek adatainak kiírása és a képek kirajzolása

a KepApplet.html fájl kódja:

<html>
<head>
<title>Fájlkiíró Applet</title>
</head>
<body>
<applet code="KepApplet.class" width=600 height=450>
</applet>
</body>
</html>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*; 

public class KepApplet extends Applet implements ActionListener {

 public Label label=new Label("letöltendõ fájl neve:");
 public TextField field=new TextField();
 public Button oke=new Button("OK");
 public TextArea adatok=new TextArea("A letöltendõ fájl adatai:");
 public Image kep=null;

 public void init() {
  setLayout(null);
  label.setBounds(5,5,120,20);
  label.setBackground(Color.yellow);
  label.setForeground(Color.blue);
  label.setFont(new Font("Arial",Font.BOLD+Font.ITALIC,12));
  add(label);
  field.setBounds(130,5,430,20);
  add(field);
  oke.setBounds(565,5,30,20);
  add(oke);
  oke.addActionListener(this);
  adatok.setBounds(5,30,590,110);
  add(adatok);
  }

 public void actionPerformed(ActionEvent ev) {
  String f=field.getText(); //képfájl neve
  URL url;
  URLConnection kapcsolat;

  try {
   url=new URL(getCodeBase(),f); //képfájl helye
   kapcsolat=url.openConnection();
   String protocol=url.getProtocol();
   String host=url.getHost();
   int port=url.getPort();
   String file=url.getFile();
   String contentType=kapcsolat.getContentType();
   int contentLength=kapcsolat.getContentLength();
   adatok.append("\n\nURL: "+url.toString()+"\n");
   adatok.append("protocol = "+protocol+";   ");
   adatok.append("host = "+host+";   ");
   adatok.append("port = "+port+";   ");
   adatok.append("file = "+file+"\n");
   adatok.append("Content-Type: "+contentType+"\n");
   adatok.append("Content-Length: "+contentLength);
   if(contentType.indexOf("image")!=-1) kep=getImage(url);
   else kep=null;
   }
  catch(MalformedURLException e) {
   adatok.append("Hibas URL!"+"\n\n");
   }
  catch(Exception e) {
   adatok.append("Egyeb hiba"+e+"\n\n");
   }
  validate();
  repaint();
  }

 public void paint(Graphics g) {
  if(kep!=null) {
   setBackground(Color.blue);
   g.drawString("Kép betöltése...",5,160);
   g.drawImage(kep,5,145,this);
   }
  else {
   setBackground(Color.cyan);
   }
  }
 }



HTMLApplet.java: képek és weblapok letöltése a szerverrõl a böngészõbe

a HTMLKeret.html fájl kódja:

<HTML>
<HEAD>
    <TITLE>egyszerû HTML keretdokumentum</TITLE>
</HEAD>
<FRAMESET COLS="35%,65%" BORDER=0>
    <FRAME SRC="HTMLApplet.html" NAME="bal">
    <FRAME SRC="ures.html" NAME="jobb">
</FRAMESET>
</HTML>
a HTMLApplet.html fájl kódja:
<html>
<head>
<title>HTML Applet</title>
</head>
<body bgcolor=blue>
<applet code="HTMLApplet.class" width=250 height=400>
</applet>
</body>
</html>
az ures.html fájl kódja:
<HTML>
<HEAD>
    <TITLE>a legegyszerûbb HTML dokumentum</TITLE>
</HEAD>
<BODY BGCOLOR="gray">
</BODY>
</HTML>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*; 

public class HTMLApplet extends Applet implements ActionListener {

 public static final Color BGCOLOR=Color.blue;
 public static final Color TEXT=Color.yellow;
 public static final Color BGLINK1=Color.cyan;
 public static final Color BGLINK2=Color.pink;
 public static final Color LINK=Color.black;
 public static final int LINKNUM=6;
 public static final String FNAME="jobb";

 public Label label=new Label();
 public Button[] b=new Button[LINKNUM];
 public boolean[] click=new boolean[LINKNUM];
 public String[] link={"sakk.gif",
                       "sakk1.gif",
                       "sakk2.gif",
                       "ElsoAppletV3.html",
                       "MasodikAppletV1.html",
                       "HarmadikApplet.html"};
 public String[] cimke={"Elsõ kép",
                        "Második kép",
                        "Harmadik kép",
                        "Elsõ applet",
                        "Második applet",
                        "Harmadik applet"};

 public void init() {
  int x=35,y=50,dy=30,w=200,h=20;

  setLayout(null);
  label.setBounds(5,5,200,30);
  label.setBackground(BGCOLOR);
  label.setForeground(TEXT);
  label.setFont(new Font("Arial",Font.BOLD+Font.ITALIC,18));
  label.setText("Megjeleníthetõ fájlok:");
  add(label);

  for(int i=0;i<LINKNUM;i++) {
   b[i]=new Button();
   b[i].setBounds(x,y,w,h);
   y+=dy;
   click[i]=false;
   b[i].setBackground(BGLINK1);
   b[i].setForeground(LINK);
   b[i].setFont(new Font("Arial",Font.BOLD,12));
   b[i].setLabel(cimke[i]);
   add(b[i]);
   b[i].setActionCommand(""+i);
   b[i].addActionListener(this);
   }
  }

 public void actionPerformed(ActionEvent ev) {
  URL url=null;
  AppletContext ac=getAppletContext();

  try {
   int i=Integer.parseInt(ev.getActionCommand());
   url=new URL(getCodeBase(),link[i]);
   ac.showDocument(url,FNAME);
   click[i]=!click[i];
   if(click[i]) b[i].setBackground(BGLINK2);
   else b[i].setBackground(BGLINK1);
   }
  catch(Exception e) {
   System.out.println(e.toString());
   }
  validate();
  }

 public void paint(Graphics g) {
  setBackground(BGCOLOR);
  }
 }



Boda István, 2005 szeptember 11.