Bekijk de onderstaande video om te zien hoe je onze site als een web app op je startscherm installeert.
Opmerking: Deze functie is mogelijk niet beschikbaar in sommige browsers.
if (e.getSource() == cirkel) {la.setText("" + aantal + " maal geklikt");}
if (e.getSource() == vierkant) {la.setText("" + aantal + " maal geklikt");}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame {
private JPanel buttonPanel;
private CustomPanel myPanel;
private JButton cirkel, vierkant;
[COLOR="Red"]private JLabel l1;[/COLOR]
public MyFrame() {
[COLOR="Red"]l1 = new JLabel("0 maal geklikt", JLabel.CENTER);[/COLOR]
MyButton b = new MyButton();
myPanel = new CustomPanel(); // instantiate canvas
myPanel.setBackground(Color.yellow);
cirkel = new JButton("Rond");
cirkel.addActionListener(b);
vierkant = new JButton("Vierkant");
vierkant.addActionListener(b);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
buttonPanel.add(cirkel);
buttonPanel.add(vierkant);
l1.setOpaque(true);
l1.setBackground(Color.WHITE);
l1.setForeground(Color.BLUE);
l1.setPreferredSize(new Dimension(20, 40));
l1.setFont(new Font("Serif", Font.PLAIN, 24));
Container c = getContentPane();
c.add(l1, BorderLayout.NORTH);
c.add(myPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
setSize(300, 300);
show();
}
public static void main(String args[]) {
MyFrame app = new MyFrame();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class MyButton implements ActionListener {
private int aantal = 0;
public void actionPerformed(ActionEvent e) {
[COLOR="Red"] this.aantal++;[/COLOR]
[COLOR="Red"] if (cirkel.equals(e.getSource())) {
l1.setText("" + aantal + " maal geklikt");
myPanel.draw(CustomPanel.CIRKEL);[/COLOR]
}
[COLOR="Red"] if (vierkant.equals(e.getSource())) {
l1.setText("" + aantal + " maal geklikt");
myPanel.draw(CustomPanel.VIERKANT);[/COLOR]
}
}
}
}
import java.awt.*;
import javax.swing.*;
public class CustomPanel extends JPanel {
public final static int CIRKEL = 1, VIERKANT = 2;
private int shape;
@Override
public void paintComponent( Graphics g )
{
super.paintComponent( g );
if ( shape == CIRKEL )
g.fillOval( 100, 50, 100, 100 );
else if ( shape == VIERKANT )
g.fillRect( 100, 50, 100, 100 );
}
public void draw( int s )
{
shape = s;
repaint();
}
}
We gebruiken essentiële cookies om deze site te laten werken, en optionele cookies om de ervaring te verbeteren.