On Windows's Start menu, an image is displayed on the menu. Many Windows-based software also have a similar menu. Can I make a similar effect of similar effects with Java Swing? The answer is of course affirmative, and it is very simple.
We first extends from Swing's JPopupMenu components, let it accept a picture, display on the left; or accept a string, dynamically generate a picture, displayed on the left. In order to avoid the trouble of preparing the picture, we use the dynamically generated memory picture as an example, write a JimageDpopupMenu class.
JimageDPopupMenu can accept a string when creating, generate a memory picture bufferedImage. Then, we need to override the GetInsets method of the JComponent, recalculate the INSET's LEFT value, add the width of the image on the basis of the original value, then return:
Public insets getInsets () {INSETS INSETS = (Insets) Super.getInsets (). Clone (); insets.left = imageicon.geticonwidth (); Return INSETS;}
Finally, override the PaintComponent method, add the drawing of the picture on the basis: Public void PaintComponent (GRAPHICS G) {Super.PaintComponent (G); if (imageicon! = Null) {INSETS INSETS = GetInsets (); g.drawImage (imageicon. GetImage (), INSETS.LEFT - ImageICON.Geticonwidth (), insets.top, null;}}
The full code is as follows:
Import java.awt. *; import java.awt.event. *; import java.awt.geom. *; import java.awt.Image. *; import javax.swing. *;
Public Class JimagedPopUpMenu Extends JpopupMenu {Private Font Font = New Font ("Dialog", Font.Bold, 13); Private Imageicon Imageicon = NULL;
Public JimagedPopupmenu (imageicon imageicon) {this.imageicon = imageicon;
Public JimagedPopupnupmenu (String text) {this.imageicon = createImage (text);
private ImageIcon createImage (String text) {BufferedImage bi = new BufferedImage (30, 1000, BufferedImage.TYPE_INT_ARGB); ImageIcon image = new ImageIcon (bi); Graphics2D g2d = bi.createGraphics ();
GradientPaint Paint = new gradientpaint (0, 0, color.yellow, 30, 10, color.red, true); g2d.setpaint (Paint); g2d.fillRect (0, 0, Bi.Getwidth (), bi.getHeight ( ));
AffineTransform at = new affinetransform (); at.ROTATE (-Math.pi / 2);
G2d.SetTransform (at); g2d.setcolor; g2d.setfont (font); g2d.drawstring (text, -180, bi.getwidth () / 2);
Return image;}
Public insets getInsets () {INSETS INSETS = (Insets) Super.getInsets (). Clone (); insets.left = imageicon.geticonwidth (); Return INSETS;}
Public void Paint (Graphics G) {Super.Paint (g); if (ImageICON! = null) {INSETS INSETS = GetInsets (); g.drawImage (), INSETS.DRAWIMAGE (), INSETS.DRAWIMAGE (), INSETS.DRAWIMAGE () INSETS.TOP, NULL);}}
public static void main (String [] args) {final JFrame frame = new JFrame (); frame.setSize (600, 500); frame.setTitle ( "ImageMenu"); final JImagedPopupMenu menu = new JImagedPopupMenu ( "Windows XP Perfessional" ); Menu.add (New Jmenuitem); menu.addseparator (); menu.add (New Jmenuitem ("Program")); Menu.Add (New Jmenuitem ("Document")); MENU. Add ("settings"); menu.add (New Jmenuitem ("Search")); menu.add ("Help and support"); menu.add (New Jmenuitem ("Run .. "); Menu.addseParator (); menu.add (New Jmenuitem (" Shut Down ... ")); JLabel Label = New Jlabel (" Right Click Me To Show Image Popup Menu. "); Label.addmouseristener (new java.awt.event.mouseadapter () {public void mousereleased (mouseEvent E) {if (E.Ispopuptrigger ()) {menu.show (frame, E.GETPOINT (). x, e.getPoint (). y }}} Frame.getContentPane (). Add (label, borderlayout.center); frame.show ();}} The operation is as follows:
Similarly, this method can also be extended the same effect.