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
|
public class teste extends JFrame {
private JPanel jContentPane = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
teste thisClass = new teste();
JPanel pan = new JPanel();
pan.setLayout(null);
pan.add(new monTextField(0, 0, 100, 20));
pan.add(new monTextField(0, 50, 100, 20));
thisClass.getContentPane().add(pan);
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public teste() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setTitle("JFrame");
}
}
class monTextField extends JTextField {
public monTextField(int x, int y, int l, int h) {
this.setBounds(x, y, l, h);
JPopupMenu jPopupMenu = new JPopupMenu();
jPopupMenu.addSeparator();
this.setComponentPopupMenu(jPopupMenu);
jPopupMenu.add(new Option("Couper", 'X', "Enlève le texte")).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
monTextField.this.cut();
}
});
jPopupMenu.add(new Option("Copier", 'C', "Copie le texte")).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
monTextField.this.copy();
}
});
jPopupMenu.add(new Option("Coller", 'V', "Ajoute le texte copier")).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
monTextField.this.paste();
}
});
}
private class Option extends JMenuItem {
public Option(String intitulé, char raccourci, String aide) {
super(intitulé, new ImageIcon(intitulé.toLowerCase() + ".gif"));
setAccelerator(KeyStroke.getKeyStroke("control " + raccourci));
setToolTipText(aide);
}
}
} |