PowerPoint-presentatie

advertisement
1. JFrame
JAVA
1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JApplet
public class HerhalingAppletH10 extends JApplet implements
ActionListener
{
private JButton een,twee;
private JTextField tekst;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
tekst=new JTextField(10);
tekst.setEditable(false);
een=new JButton("Een");
twee=new JButton("Twee");
JAVA
2
JApplet
een.addActionListener(this);
twee.addActionListener(this);
c.add(tekst);
c.add(een);
c.add(twee);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==een)
tekst.setText("één");
else
if (e.getSource()==twee)
tekst.setText("twee");
}
}
JAVA
3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JFrame
public class HerhalingH10 extends JFrame implements ActionListener
{
private JButton een,twee, stop;
private JTextField tekst;
public HerhalingH10()
{
super("JFrame zonder innerklassen");
Container c=getContentPane();
c.setLayout(new FlowLayout());
tekst=new JTextField(10);
tekst.setEditable(false);
een=new JButton("Een");
twee=new JButton("Twee");
stop=new JButton("Stop");
JAVA
4
JFrame
een.addActionListener(this);
twee.addActionListener(this);
stop.addActionListener(this);
c.add(tekst);
c.add(een);
c.add(twee);
c.add(stop);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==een)
tekst.setText("één");
else
if (e.getSource()==twee)
tekst.setText("twee");
else
System.exit(0);
}
JAVA
5
JFrame
public static void main(String args[])
{
HerhalingH10 scherm=new HerhalingH10();
scherm.setSize(400,200);
scherm.setVisible(true);
}
}
JAVA
6
JFrame met een innerklasse
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HerhalingH10InnerClass extends JFrame
{
private JButton een,twee,stop;
private JTextField tekst;
public HerhalingH10InnerClass()
{
super("JFrame met innerklassen");
Container c=getContentPane();
c.setLayout(new FlowLayout());
tekst=new JTextField(10);
tekst.setEditable(false);
een=new JButton("Een");
twee=new JButton("Twee");
stop=new JButton ("Stop");
JAVA
7
JFrame met een innerklasse
KlasseDieEventsAfhandelt objectInnerClass =
new KlasseDieEventsAfhandelt();
een.addActionListener(objectInnerClass);
twee.addActionListener(objectInnerClass);
stop.addActionListener(objectInnerClass);
c.add(tekst);
c.add(een);
c.add(twee);
c.add(stop);
}
JAVA
8
JFrame met een innerklasse
private class KlasseDieEventsAfhandelt implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==een)
tekst.setText("één");
else
if (e.getSource()==twee)
tekst.setText("twee");
else
System.exit(0);
}
}
public static void main(String args[])
{
HerhalingH10InnerClass scherm = new HerhalingH10InnerClass();
scherm.setSize( 400, 200 );
scherm.setVisible( true );
}
}
JAVA
9
JFrame met anonieme innerklassen
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HerhalingH10AnoniemeInnerClass extends JFrame
{
private JButton een,twee,stop;
private JTextField tekst;
public HerhalingH10AnoniemeInnerClass()
{
super("JFrame met anonieme innerklassen");
Container c=getContentPane();
c.setLayout(new FlowLayout());
tekst=new JTextField(10);
tekst.setEditable(false);
c.add(tekst);
JAVA
10
JFrame met anonieme innerklassen
een=new JButton("Een");
twee=new JButton("Twee");
stop=new JButton("Stop");
//Anonieme innerklasse:
een.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tekst.setText("één");
}
}
);
JAVA
11
JFrame met anonieme innerklassen
//Anonieme innerklasse:
twee.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tekst.setText("twee");
}
}
);
//Anonieme innerklasse:
stop.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
JAVA
);
12
JFrame met anonieme innerklassen
c.add(een);
c.add(twee);
c.add(stop);
}
public static void main(String args[])
{
HerhalingH10AnoniemeInnerClass scherm=
new HerhalingH10AnoniemeInnerClass();
scherm.setSize(400,200);
scherm.setVisible(true);
}
}
JAVA
13
JAVA
14
…
JFrame implements ActionListener, WindowListener
public class HerhalingH10_2 extends JFrame implements
ActionListener, WindowListener
{
private JButton een,twee;
private JTextField tekst;
public HerhalingH10_2()
{
...
een.addActionListener(this);
twee.addActionListener(this);
addWindowListener(this);
…
}
JAVA
15
JFrame implements ActionListener, WindowListener
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==een)
tekst.setText("één");
else
tekst.setText("twee");
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosing ( WindowEvent event)
{
System.exit(0);
}
JAVA
16
JFrame met innerklasse
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HerhalingH10InnerClass_2 extends JFrame
{
private JButton een,twee;
private JTextField tekst;
public HerhalingH10InnerClass_2()
{
...
KlasseDieWindowEventsAfhandelt objectWindowInnerClass=
new KlasseDieWindowEventsAfhandelt();
addWindowListener(objectWindowInnerClass);
…
}
JAVA
17
JFrame met innerklasse
//Innerklasse
private class KlasseDieWindowEventsAfhandelt implements
WindowListener
{
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosing( WindowEvent event )
{
System.exit( 0 );
}
}
JAVA
18
JFrame met anonieme innerklasse
…
public class HerhalingH10AnoniemeInnerClass_2 extends JFrame
{ private JButton een,twee; private JTextField tekst;
public HerhalingH10AnoniemeInnerClass_2() { ... }
public static void main(String args[])
{
HerhalingH10AnoniemeInnerClass_2 scherm=
new HerhalingH10AnoniemeInnerClass_2();
scherm.addWindowListener(
new WindowAdapter ()
{
public void windowClosing ( WindowEvent event )
{
System.exit(0);
}
}
);
scherm.setSize(400,200);
scherm.setVisible(true);
}
JAVA
19
Download