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 103 104 105 106 107 108 109 110 111 112 113
|
import javax.swing.SwingUtilities;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
public class teste extends JFrame {
private Actions actionEnregistrer = new Actions("Enregistrer", 'W', "Sauvegarder le texte");
private Actions actionCouper = new Actions("Couper", 'X', "Enlever le texte sélectionné"); // @jve:decl-index=0:
private Actions actionCopier = new Actions("Copier", 'C', "Copier le texte sélectionné");
private Actions actionColler = new Actions("Coller", 'V', "Coller le texte à l'endroit du curseur");
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JPopupMenu jPopupMenu = null; // @jve:decl-index=0:visual-constraint="94,78"
private JTextField jTextField = null;
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
}
return jJMenuBar;
}
private JPopupMenu getJPopupMenu() {
if (jPopupMenu == null) {
jPopupMenu = new JPopupMenu();
jPopupMenu .add(actionEnregistrer);
jPopupMenu .addSeparator();
jPopupMenu .add(actionCopier);
jPopupMenu .add(actionColler);
}
return jPopupMenu;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setComponentPopupMenu(jPopupMenu);
jTextField.setBounds(new Rectangle(114, 45, 118, 42));
}
return jTextField;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
teste thisClass = new teste();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public teste() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextField(), null);
}
return jContentPane;
}
private class Actions extends AbstractAction {
private String méthode;
public Actions(String libellé, char raccourci, String description) {
super(libellé, new ImageIcon(libellé.toLowerCase()+".gif"));
putValue(SHORT_DESCRIPTION, description);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control "+raccourci));
}
public void actionPerformed(ActionEvent e) {
try {
}
catch (Exception ex) { teste.this.setTitle("Problème");}
}
private void couper() {jTextField.cut(); }
private void copier() { jTextField.copy(); }
private void coller() { jTextField.paste(); }
}
} |
Partager