1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
/**
*
* @author marc003
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestContPan extends JPanel implements ActionListener {
public TestContPan() {
super();
setBackground(Color.blue);
setLayout(new BorderLayout());
setBounds(0, 0, 50,50);
bouton = new JButton("compte clics");
bouton.addActionListener(this);
add(bouton,BorderLayout.SOUTH);
haut = new JLabel("clic pair");
add(haut,BorderLayout.WEST);
JPanel tmp = new JPanel();
add(tmp,BorderLayout.EAST);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Redimendionne le composant 1, c'est a dire le Jlabel
this.getComponent(1).setBounds(0, 0, getParent().getWidth()*50/100, getParent().getHeight());
// Redimendionne le composant 2, c'est a dire le JPanel
this.getComponent(2).setBounds(getParent().getWidth()*50/100,0, getParent().getWidth()*50/100+1, getParent().getHeight());
}
public void actionPerformed(ActionEvent ae) {
if (cliqué) {
haut.setText("clic pair");
cliqué = false;
}
else {
haut.setText("clic impair");
cliqué = true;
}
}
public JLabel haut;
public JButton bouton;
private boolean cliqué;
/*
*
* @param args
*/
public static void main(String[] args){
JFrame fen = new JFrame();
fen.add(new TestContPan());
fen.setVisible(true);
fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} |
Partager