Erstellen einer GUI-KLasse, die sowohl als Applet, als auch als Applikation verwendet werden kann
|
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class Hybrid extends Applet
{
public String text;
public String paramtext;
public int x,y;
public void init()
{
text="Hallo";
paramtext="Parameter1";
x=100;
y=150;
}
public void start()
{
System.out.println("Das Applet wurde gestartet.");
try
{
PrintWriter pw = new PrintWriter(new FileWriter("test.txt"));
pw.println("Test");
pw.close();
}
catch(Exception e)
{
System.out.println("Fileoperationen nicht erlaubt!");
System.out.println(e);
}
}
public void paint(Graphics g)
{
g.setColor(new Color(200,255,200));
g.fillRect(5,5,200,100);
g.setColor(new Color(0,0,0));
g.drawString(text,10,100);
g.setColor(new Color(255,0,0));
g.drawString(paramtext,x,y);
}
public static void main(String[] args)
{
Hybrid hb = new Hybrid();
hb.init();
hb.start();
hb.repaint();
JFrame mF = new JFrame("Differentialgeometrie");
mF.getContentPane().add(hb);
mF.pack();
mF.setSize(500,500);
mF.setLocation(10,10);
mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mF.setVisible(true);
}
}
Code 0-1: Beispiel eines Java-Programms, das sowohl als Applet, als auch als Applikation verwendet werden kann.