日期:2014-05-20 浏览次数:20803 次
package org.gui; import java.awt.Frame; import java.awt.Image; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestMenu extends Frame{ /** * @param args */ MenuBar mb = new MenuBar(); Menu m = new Menu("File"); Menu m1 = new Menu("Start"); MenuItem mi1 = new MenuItem("图片1"); MenuItem mi2 = new MenuItem("图片2"); public TestMenu(){ this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ setVisible(false); System.exit(0); } }); this.setSize(400,400); mb.add(m); m.add(m1); m1.add(mi1); m1.add(mi2); MenuListener ml = new MenuListener(this); mi1.addActionListener(ml); mi2.addActionListener(ml); this.setMenuBar(mb); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new TestMenu(); } } class MenuListener implements ActionListener{ TestMenu tm = null; Image im = null; public MenuListener(TestMenu tm){ this.tm = tm; } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(arg0.getActionCommand().equals("图片1")){ System.out.println("1111"); im = tm.getToolkit().getImage("d:/001.jsp"); System.out.println(im==null); while(!tm.getGraphics().drawImage(im, 20, 20, tm)){ tm.getGraphics().drawImage(im, 20, 20, tm); } }else if(arg0.getActionCommand().equals("图片2")){ System.out.println("22222"); im = tm.getToolkit().getImage("d:/002.jsp"); System.out.println(im==null); while(!tm.getGraphics().drawImage(im, 20, 20, tm)){ tm.getGraphics().drawImage(im, 20, 20, tm); } } } }
/** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2009</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */ import java.awt.Frame; import java.awt.Image; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; import javax.swing.JFrame; import java.awt.Graphics2D; public class TestMenu extends JFrame{ public static void main(String[] args) { // TODO Auto-generated method stub new TestMenu(); } /** * @param args */ String filename ; MenuBar mb = new MenuBar(); Menu m = new Menu("File"); Menu m1 = new Menu("Start"); MenuItem mi1 = new MenuItem("图片1"); MenuItem mi2 = new MenuItem("图片2"); public TestMenu(){ /*this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ setVisible(false); System.exit(0); } });*/ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400,400); this.setContentPane(new PicPanel()); mb.add(m); m.add(m1); m1.add(mi1); m1.add(mi2); MenuListener ml = new MenuListener(this); mi1.addActionListener(ml); mi2.addActionListener(ml); this.setMenuBar(mb); this.setVisible(true); } public void updateImg(String filename){ this.filename = filename; repaint(); } class PicPanel extends JPanel{ public void paintComponent(Graphics g){ if(filename==null || filename.length()==0){ super.paintComponent(g); return; } try{ BufferedImage img = ImageIO.read(new File(filename)); Graphics2D g2d = (Graphics2D)g; g2d.drawImage(img,0,0,img.getWidth(),img.getHeight(),null); }catch(IOException e){} } } } class MenuListener implements ActionListener{ TestMenu tm = null; Image im = null; public MenuListener(TestMenu tm){ this.tm = tm; } public void actionPerformed(ActionEvent arg0) { if(arg0.getActionCommand().equals("图片1")){ tm.updateImg("d:/001.jsp"); }else if(arg0.getActionCommand().equals("图片2")){ tm.updateImg("d:/002.jsp"); } } }