Also ich habe dir mal ein JApplet gebastelt, welches mit einen eigenen Thread arbeitet (kann man machen muss man nicht) ich persönlich finde das besser, denn das ist die optimale Basis für evtl. parallele Prozesse (sollte das JApplet mal „wachsen“)
der LayoutManager ist GridBagLayout, kannst aber auch durch einen anderen ersetzen. damit kommen wir auch schon zu meinen hinweisen was dein programmier-style angeht.
1.
Attribute/Instanzvariablen und Methodenvariablen beginnen nicht mit Großbuchstaben (z.B. Abschicken abschicken)
2.
versuche Modularität so weit wie möglich und so weit wie nötig einzubauen.
bei mir muss nur die methode addElements() ausgetauscht werden oder man schreibt eine neue Methode parallel dazu und wenn die irgendwann ausgereift ist, kann man meine einfach löschen… was bei deiner Variante nicht so ohne weiteres möglich ist. Ok noch ist dein Proggy klein, aber bei richtig großen Projekten die aus 100´te Klassen bestehen…
3.
Kommentare nicht vergessen damit man via JavaDoc oder anderen Werkzeugen eine Dokumentation generieren kann, bzw. ein anderer Programmierer kann sich schneller einarbeiten.
Ich bin erstaunt das du es richtig erkannt hast das deine „ContentPane“ (Klasse Buttons) eine Extra-Klasse ist und beim JFrame eingebunden wird. Das zeigt das du den OO-Ansatz verstanden haben musst. Weiter so und lass dich nicht entmutigen beim erlernen der Sprache Java.
Ok genug gelabbert hier meine „musterlösung“ mit dieser lösung als Basis kannst du bestimmt dein Applet fertig basteln.
TODO:
1.
GridBagLayout „kracht“ gerne zusammen wenn das Fenster(JApplet) zu klein ist, also anderen LayoutManager oder entsprechende Modifikationen vornehmen.
2.
mein infoField musste noch rauswerfen, war nur ein Beispiel warum ich diese Klasse MyContentPane so „aufwendig“ gebaut habe. denn dann kann man wirkliche „Interaktivität“ der Komponenten untereinander basteln.
Das JApplet
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
/\*\*
\* Das ist mein Applet für irgendwas.
\* @author Daniel Nitschke
\* @version 1.0, build-system Windows XP, Eclipse WTP3, SUN Java 1.6.0
\* @since 30.10.2008
\*/
public class MyApplet extends JApplet implements Runnable
{
/\*\*
\* is the SerialVersionUID of this Class
\*/
private static final long serialVersionUID = 1L;
/\*\*
\* is my personal content pane
\*/
private MyContentPane myContent = null;
/\*\*
\* is the thread, in this class will run.
\*/
private Thread runner = null;
/\*\*
\* hier wird das JApplet mit seinen Default-Werten initialisiert
\*/
@Override
public void init()
{
myContent = new MyContentPane();
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(myContent, BorderLayout.NORTH);
//für den Fall das der Browser kein Applet.start() unterstützt....
this.start();
}
/\*\*
\* hier wird das JApplet gestartet, z.B. vom Browser
\* @override Applet {@link java.applet.Applet#start()}
\*/
@Override
public void start()
{
//is the runner does started?
//if the runner not started, then would starting the runner now
if(runner == null)
{
runner = new Thread(this);
runner.setName("MainRunner");
runner.start();
System.out.println("starting the Main-Runner");
}
if(runner.isInterrupted() || runner.getState().toString() == "TERMINATED")
{
runner = new Thread(this);
runner.setName("MainRunner");
runner.start();
System.out.println("starting the Main-Runner");
}
}
/\*\*
\* hier wird das JApplet gestopt
\* das macht der Browser wenn man er die Seite/Tab in den "Hintergrund" legt
\* @override Applet {@link java.applet.Applet#stop()}
\*/
@Override
public void stop()
{
if(runner != null)
{
runner.interrupt();
}
}
/\*\*
\* this is the runner-logical for doing his job.
\* @implements Runnable.run() {@link java.lang.Runnable#run()}
\*/
public void run()
{
boolean running = true;
while(running)
{
super.repaint();
try
{
Thread.sleep(200);
}
catch(InterruptedException error)
{
running = false;
}
}
}
/\*\*
\* hier sind nur ein paar Beispiele
\* die man benutzen kann für das Applet
\*/
private void onlyForTesting()
{
//eine Anwendungsmöglichkeit, Bilder ins Applet einbinden
ImageIcon[] pictures = new ImageIcon[9];
//the names of pictures
String nekoSrc[] = { "right1.gif", "right2.gif", "stop.gif",
"yawn.gif", "scratch1.gif", "scratch2.gif",
"sleep1.gif", "sleep2.gif", "awake.gif" };
//init array of pictures
for (int i=0; i
\*
\*
\*
\* paramColor ist dann "blue"
\*/
}
}
deine "ContentPane"
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/\*\*
\* Das ist meine persönliche ContentPane.
\* @author Daniel Nitschke
\* @version 1.0, build-system Windows XP, Eclipse WTP3, SUN Java 1.6.0
\* @since 30.10.2008
\*/
public class MyContentPane extends JPanel
{
private JButton abschicken = null;
private JButton loeschen = null;
private JLabel nameLabel = null;
private JLabel vornameLabel = null;
private JLabel emailLabel = null;
private JCheckBox cb1 = null;
private JCheckBox cb2 = null;
private JCheckBox cb3 = null;
private JCheckBox cb4 = null;
private JTextField tf1 = null;
private JTextField tf2 = null;
private JTextField tf3 = null;
private JTextField infoField = null;
/\*\*
\* is the SerialVersionUID of this Class
\*/
private static final long serialVersionUID = 1L;
/\*\*
\* the default-constructor without parameter
\*/
public MyContentPane()
{
init();
addElements();
}
/\*\*
\* the constructor with parameter
\* @param name is the name of this component in the container ContentPane
\*/
public MyContentPane(String name)
{
this.setName(name);
init();
addElements();
}
/\*\*
\* initialisiert alle Elemente mit den default-Werten
\*/
private void init()
{
abschicken = new JButton("Abschicken");
abschicken.setActionCommand("abschicken");
abschicken.addActionListener(new ActionAdapter(this));
loeschen = new JButton("löschen");
loeschen.setActionCommand("löschen");
loeschen.addActionListener(new ActionAdapter(this));
nameLabel = new JLabel("Name:");
vornameLabel = new JLabel("Vorname:");
emailLabel = new JLabel("E-Mail:");
cb1 = new JCheckBox("Freunde", false);
cb1.setActionCommand("freunde");
cb1.addActionListener(new ActionAdapter(this));
cb2 = new JCheckBox("Werbung", true);
cb2.setActionCommand("werbung");
cb2.addActionListener(new ActionAdapter(this));
cb3 = new JCheckBox("Suchmaschine", false);
cb3.setActionCommand("suchmaschine1");
cb3.addActionListener(new ActionAdapter(this));
cb4 = new JCheckBox("Suchmaschien", false);
cb4.setActionCommand("suchmaschine2");
cb4.addActionListener(new ActionAdapter(this));
tf1 = new JTextField(15);
tf2 = new JTextField(15);
tf3 = new JTextField(15);
infoField = new JTextField(15);
}
/\*\*
\* fügt alle Elemente auf das JPanel
\*/
private void addElements()
{
//Layoutfestlegen - GridBagLayout
GridBagLayout gridBagLayout = new GridBagLayout();
GridBagConstraints constr = new GridBagConstraints();
this.setLayout(new GridBagLayout());
constr.ipadx = 0;
constr.ipady = 0;
constr.gridx = 10;
constr.gridy = 10;
constr.anchor = GridBagConstraints.NORTHWEST;
this.add(abschicken, constr);
constr.gridx = 11;
this.add(loeschen, constr);
constr.gridx = 12;
constr.gridy = 9;
this.add(nameLabel, constr);
constr.gridy = 10;
this.add(vornameLabel, constr);
constr.gridy = 11;
this.add(emailLabel, constr);
constr.gridx = 13;
constr.gridy = 9;
this.add(tf1, constr);
constr.gridy = 10;
this.add(tf2, constr);
constr.gridy = 11;
this.add(tf3, constr);
constr.gridx = 14;
constr.gridy = 9;
this.add(cb1, constr);
constr.gridy = 10;
this.add(cb2, constr);
constr.gridy = 11;
this.add(cb3, constr);
constr.gridy = 12;
this.add(cb4, constr);
constr.gridx = 13;
constr.gridy = 1;
this.add(infoField, constr);
}
public JButton getAbschicken()
{
return abschicken;
}
public void setAbschicken(JButton abschicken)
{
this.abschicken = abschicken;
}
public JButton getLoeschen()
{
return loeschen;
}
public void setLoeschen(JButton loeschen)
{
this.loeschen = loeschen;
}
public JLabel getNameLabel()
{
return nameLabel;
}
public void setNameLabel(JLabel nameLabel)
{
this.nameLabel = nameLabel;
}
public JLabel getVornameLabel()
{
return vornameLabel;
}
public void setVornameLabel(JLabel vornameLabel)
{
this.vornameLabel = vornameLabel;
}
public JLabel getEmailLabel()
{
return emailLabel;
}
public void setEmailLabel(JLabel emailLabel)
{
this.emailLabel = emailLabel;
}
public JCheckBox getCb1()
{
return cb1;
}
public void setCb1(JCheckBox cb1)
{
this.cb1 = cb1;
}
public JCheckBox getCb2()
{
return cb2;
}
public void setCb2(JCheckBox cb2)
{
this.cb2 = cb2;
}
public JCheckBox getCb3()
{
return cb3;
}
public void setCb3(JCheckBox cb3)
{
this.cb3 = cb3;
}
public JCheckBox getCb4()
{
return cb4;
}
public void setCb4(JCheckBox cb4)
{
this.cb4 = cb4;
}
public JTextField getTf1()
{
return tf1;
}
public void setTf1(JTextField tf1)
{
this.tf1 = tf1;
}
public JTextField getTf2()
{
return tf2;
}
public void setTf2(JTextField tf2)
{
this.tf2 = tf2;
}
public JTextField getTf3()
{
return tf3;
}
public void setTf3(JTextField tf3)
{
this.tf3 = tf3;
}
public JTextField getInfoField()
{
return infoField;
}
public void setInfoField(JTextField infoField)
{
this.infoField = infoField;
}
}
der ActionListener
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
/\*\*
\* Diese Klasse repräsentiert ein ActionListener für die Elemente von
\* MyContentPane
\* @author Daniel Nitschke
\* @verson 1.0, Buildsystem windows XP, Eclipse WTP3, SUN Java 1.6.0
\* @since 30.10.2008
\*/
public class ActionAdapter implements ActionListener
{
/\*\*
\* ist meine MyContentPane
\*/
private MyContentPane myContent = null;
/\*\*
\* ist der default-Constructor der Klasse mit Parameterübergabe
\* @param gui ist das JFrame gui.GUI
\*/
public ActionAdapter(JPanel myContentPane)
{
this.myContent = (MyContentPane)myContentPane;
}
/\*\*
\* ist die Methode für den implementierten ActionListener
\* um auf ActionEvents zu reagieren
\* @param actionEvent ist das ausgelöste ActionEvent
\*/
public void actionPerformed(ActionEvent actionEvent)
{
if(actionEvent.getActionCommand().equals("abschicken"))
{
myContent.getInfoField().setText("ABSCHICKEN");
}
if(actionEvent.getActionCommand().equals("löschen"))
{
myContent.getInfoField().setText("LÖSCHEN");
}
if(actionEvent.getActionCommand().equals("freunde"))
{
myContent.getInfoField().setText("FREUNDE");
}
if(actionEvent.getActionCommand().equals("werbung"))
{
myContent.getInfoField().setText("WERBUNG");
}
if(actionEvent.getActionCommand().equals("suchmaschine1"))
{
myContent.getInfoField().setText("SUCHMASCHINE 1");
}
if(actionEvent.getActionCommand().equals("suchmaschine2"))
{
myContent.getInfoField().setText("SUCHMASCHINE 2");
}
}
}