Appel d'une méthode d'une autre classe à partir d'une action sur JMenuItem
Bonsoir
dans mon programme, j'ai créé un interface contient une barre de menu qui constitue de menu "file" . en fait dans le menu file il y a 2 JMenuItem.
Code:
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
|
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.ImageIcon;
public class interfacejava {
public static void main(final String[] args)throws IOException
{
final JFrame frame = new JFrame("Interface");
JMenuBar menubar=new JMenuBar();
JMenu menu = new JMenu(" File ");
JMenuItem ouvrir = new JMenuItem(" Ouvrir ",'O');
final JMenuItem genere = new JMenuItem(" Génerer les 10 meilleure posts ",'G');
menubar.add(menu);
menu.add(ouvrir);
menu.add(genere);
frame.setJMenuBar(menubar);
frame.setSize(500,300);
ouvrir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String photo;
Statement stmt;
Connection conn;
JFileChooser dialogue = new JFileChooser(new File("C:/Users/gmi/Desktop/Photos"));
File fichier;
if (dialogue.showOpenDialog(null)==
JFileChooser.APPROVE_OPTION) {
fichier = dialogue.getSelectedFile();
String namefile=dialogue.getName(fichier);
System.out.println(namefile);
});
genere.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
menu2 menugenere=new menu2(frame);}
});
frame.show();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}} |
menu2.java est une classe qui permet d’insérer plusieurs images
Code:
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
|
public class menu2 {
static JFrame frame;
public menu2 (JFrame frame)
{this.frame=frame;}
public static void main(String[] args) throws IOException {
frame.setSize(500,300);
try
{
frame.setLayout(null);
File file = new File("C:/Users/gmi/Desktop/Photos/im1.jpg");
BufferedImage im = ImageIO.read(file);
//affichage de l'image
ImageIcon icon = new ImageIcon(im);
Image zoom = scaleImage(icon.getImage(), 50, 50);
Icon iconScaled = new ImageIcon(zoom);
JLabel monImage = new JLabel(iconScaled);
monImage.setBounds(30, 30, 50, 100);
frame.add(monImage);
frame.show();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static Image scaleImage(Image source, int width, int height) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source, 0, 0, width, height, null);
g.dispose();
return img;
}
} |
Dans mon interface graphique, j'ai créé un actionListener sur l'item "generer" pour lancer la classe main de "menu2" or je ne sais pas comment écrire ou"exécute la méthode "main" .
merci d'avance pour votre aide :)