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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PilouFrame extends JFrame {
private JCheckBox cb;
private PilouButton pb;
private JComboBox combo;
public PilouFrame() {
pb = new PilouButton("Name", "Text", "Com", 150, 50, true);
JPanel panel = new JPanel();
panel.add(pb);
Vector comboContent = new Vector();
comboContent.add("Accèes aux services de base 1.0 Français");
comboContent.add("Accèes aux services de base 2.0 Anglais");
comboContent.add("Accèes aux services de base 1.0 Allemand");
combo = new JComboBox(comboContent);
combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println((String) combo.getSelectedItem());
}
});
panel.add(combo);
cb = new JCheckBox("enable");
cb.setSelected(true);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
changeButtonState();
}
});
panel.add(cb);
getContentPane().add(panel);
}
protected void changeButtonState() {
pb.setEnabled(cb.isSelected());
}
public static void main(String[] args){
PilouFrame frame = new PilouFrame();
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class PilouButton extends JButton{
// public PilouButton(String iconE, String iconD, String name, String text,
public PilouButton(String name, String text,
String com, int x, int y, boolean state) {
//état
this.setEnabled(state);
//icone
this.setIcon(new ImageIcon(ClassLoader.getSystemResource("7x7.gif")));
this.setDisabledIcon(new ImageIcon(ClassLoader.getSystemResource("7x7_ddb.gif")));
this.setName(name);
this.setText(text);
this.setActionCommand(com);
this.setVerticalTextPosition(AbstractButton.CENTER);
this.setHorizontalTextPosition(AbstractButton.CENTER);
this.setPreferredSize(new Dimension(x, y));
this.setMinimumSize(new Dimension(10, 10));
this.setMaximumSize(new Dimension(x, y));
/**
* Choisis l'affichage que tu préfères
*/
//1) à gauche
// this.setVerticalTextPosition(AbstractButton.CENTER);
// this.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
//2) au milieu
// setVerticalTextPosition(AbstractButton.BOTTOM);
// setHorizontalTextPosition(AbstractButton.CENTER);
//3) à droite
setVerticalTextPosition(AbstractButton.CENTER);
setHorizontalTextPosition(AbstractButton.RIGHT);
}
}
} |
Partager