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 114 115 116 117 118 119 120 121 122 123
|
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
public class cEventManager extends AbstractAction {
private String m_key;
private Object m_owner;
private String m_owner_classname;
public cEventManager(Object Owner, String Key, String texte){
super(texte);
m_key = Key;
m_owner = Owner;
m_owner_classname = Owner.getClass().getName();
}
public void actionPerformed(ActionEvent e) {
if ( m_owner_classname=="_Form") {((_Form) m_owner).EventManager (m_key, e);}
}
}
import javax.swing.SwingUtilities;
public class StartUp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
_Form fenetre = new _Form("StartUp");
}
});
}
}
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.prefs.Preferences;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class _Form extends JFrame {
public _Form( String Title) {
super();
addListeners();
this.setTitle(Title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Preferences prefs = Preferences.userNodeForPackage(_Form.class);
int x = prefs.getInt("x",200);
int y = prefs.getInt("y",200);
this.setLocation(x,y);
int w = prefs.getInt("w",200);
int h = prefs.getInt("h",200);
this.setSize(w,h);
setContentPane(buildContentPane());
this.setVisible(true);
}
private JPanel buildContentPane(){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.white);
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu(new cEventManager( this, "mnMain", "Menu principal"));
menuBar.add(menu1);
JMenuItem calculer = new JMenuItem(new cEventManager( this, "mnCalculer", "Je calcule maintenant !"));
menu1.add(calculer);
JMenuItem quitter = new JMenuItem("Quitter");
menu1.add(quitter);
setJMenuBar(menuBar);
return panel;
}
public void EventManager(String Key, ActionEvent e){
System.out.println(Key + " clicked");
}
private void addListeners(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
Preferences prefs = Preferences.userNodeForPackage(_Form.class);
Rectangle bounds = getBounds();
prefs.putInt("x",(int)bounds.getX());
prefs.putInt("y",(int)bounds.getY());
prefs.putInt("w",(int)bounds.getWidth());
prefs.putInt("h",(int)bounds.getHeight());
}
});
}
} |
Partager