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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
package com.moi.observ;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
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.JTextField;
import javax.swing.SwingConstants;
public class MyFrame extends JFrame {
//public class MyFrame {
private JMenuBar menuBar;
public AbstractJPanel view_;
public AbstractJPanel view__;
private Controler controler;
public MyFrame(Controler controler) {
this.controler = controler;
initFrame();
initMenu();
initView(controler);
}
public void initFrame() {
this.setTitle("Un test");
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
menuBar = new JMenuBar();
this.setJMenuBar( menuBar );
this.setPreferredSize( new Dimension(300, 300) );
this.setResizable( false );
}
public void initView(Controler controler) {
view_ = new View_( controler );
view__ = new View__( controler );
//On initialise la frame avec le panel View_.
setContentPane(view_);
/* La vue observera le modèle */
controler.getModel().addObserver(view_);
controler.getModel().addObserver(view__);
}
public void display() {
this.pack();
this.setVisible(true);
}
public AbstractJPanel getView_() {
return view_;
}
public AbstractJPanel getView__() {
return view__;
}
public void initMenu() {
JMenuItem exit;
String items[] = { "Update ...", "Open ...", "Save ..." };
char itemMnemonics[] = { 'U', 'O', 'C', 'E' };
String iconFile[] = { "new.gif", "open.gif", "save.gif" };
Icon icons[];
JMenu fileMenu = new JMenu("File");
JMenuItem menuItems[] = new JMenuItem[3];
final JLabel display = new JLabel("Waiting for Event...", SwingConstants.CENTER); //Display JLabel in the Center
fileMenu.setMnemonic('F'); //Add mnemonic on the JMenu "File"
menuBar.add(fileMenu); //Adding the JMenu to JMenuBar
//Constructing 3 JMenuItem using "for loop"
for(int count=0; count<menuItems.length; count++) {
menuItems[count] = new JMenuItem(items[count],new ImageIcon(iconFile[count])); //Constructing JMenuItem with the Specified String menus and icons
menuItems[count].setMnemonic(itemMnemonics[count]); //Adding mnemonics on JMenuItem
fileMenu.add(menuItems[count]); //Add JMenuItem on JMenu
}
fileMenu.addSeparator(); //Creating a separator to separate exit from New, Open, and Close
exit = new JMenuItem("Exit ..."); //Constructing JMenuItem "Exit"
exit.setMnemonic('E'); //Set JMenuItem "Exit" Mnemonic
fileMenu.add(exit); //Adding JMenuItem "Exit" to JMenu
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
//Implementing Event Listener on JMenuItem[0] which is "New"
menuItems[0].addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
display.setText("New Item Menu is Selected");
if ( view_ == null) {
view_ = new View_(controler);
}
setContentPane(view_);
revalidate();
}
});
//Implementing Event Listener on JMenuItem[1] which is "Open"
menuItems[1].addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
display.setText("Open Item Menu is Selected");
if ( view__ == null) {
view__ = new View__(controler);
}
setContentPane(view__);
revalidate();
}
});
//Implementing Event Listener on JMenuItem[2] which is "Save"
menuItems[2].addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
display.setText("Save Item Menu is Selected");
}
});
//Implementing Event Listener on JMenuItem exit
exit.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0); //Exit the Application
}
});
//Add JLabel display in the container
//pane.add(display, BorderLayout.CENTER);
}
} |
Partager