2011-07-04 76 views
2

我正在開發桌面應用程序在java.On啓動我的應用程序從系統托盤運行。但系統托盤的上下文菜單是根據OS有標準的外觀和感覺。我想自定義上下文菜單。我想給它漸變背景,想改變字體,邊框等。讓我知道這是可能的嗎? 如果有一些例子對於相同的是,請提供鏈接..具有系統托盤上下文菜單的漸變背景

在此先感謝..

+0

你是什麼意思「系統托盤運行」?那種說法對我來說有點困惑。是[this](http://stackoverflow.com/questions/6403821/how-to-add-an-image-to-a-jframe-title-bar/6403846#6403846)你在說什麼? – mre

+0

檢查thw下面的鏈接:http://download.oracle.com/javase/tutorial/uiswing/misc/systemtray.html – Krutika

+0

上面的鏈接將向您解釋系統托盤的功能。這裏的菜單顯示給我要給漸變background.I認爲這足以讓我的問題清楚地向你.. – Krutika

回答

2

嗯僅此blog通過SwingGuru的一個可以與

import java.awt.Dimension; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JPopupMenu; 
import javax.swing.JSeparator; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

public class FunnyMenuTest { 

    private JPopupMenu popupMenu; 
    private JMenuItem m_mniInsertRow; 
    private JMenuItem m_mniInsertScrip; 
    private JMenuItem m_mniDeleterRow; 
    private JMenuItem m_mniDeleteExpiredScrip; 
    private JMenuItem m_mniSetAlert; 

    public void makeUI() { 
     JFrame frame = new JFrame("Funny JMenu Test"); 
     popupMenu = new JPopupMenu(); 
     m_mniInsertRow = new JMenuItem("Insert a Row"); 
     m_mniInsertRow.setOpaque(false); 
     m_mniInsertScrip = new JMenuItem("Insert a Scrip"); 
     m_mniInsertScrip.setOpaque(false); 
     m_mniDeleterRow = new JMenuItem("Delete a Row"); 
     m_mniDeleterRow.setOpaque(false); 
     m_mniDeleteExpiredScrip = new JMenuItem("Delete a Expired Scrip"); 
     m_mniDeleteExpiredScrip.setOpaque(false); 
     m_mniSetAlert = new JMenuItem("Set Alert"); 
     m_mniSetAlert.setOpaque(false); 
     popupMenu.add(m_mniInsertRow); 
     popupMenu.add(m_mniInsertScrip); 
     popupMenu.addSeparator(); 
     popupMenu.add(m_mniDeleterRow); 
     popupMenu.add(m_mniDeleteExpiredScrip); 
     popupMenu.add(new JSeparator()); 
     popupMenu.add(m_mniSetAlert); 
     JMenuBar menuBar = new JMenuBar(); 
     for (int i = 0; i < 5; i++) { 
      JMenu menu = new JMenu("Menu " + i); 
      for (int j = 0; j < 5; j++) { 
       JMenuItem item = new JMenuItem("Item " + j); 
       item.setOpaque(false); 
       menu.add(item); 
      } 
      menuBar.add(menu); 
     } 
     JMenu menu = new JMenu("Menu 1"); 
     JMenuItem item = new JMenuItem("Item "); 
     item.setOpaque(false); 
     menu.add(item); 
     menuBar.add(menu); 
     JPanel jp = new JPanel(); 
     jp.setPreferredSize(new Dimension(400, 400)); 
     jp.addMouseListener(new MouseAdapter() { 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       if (e.isPopupTrigger()) { 
        int x = e.getX(); 
        int y = e.getY(); 
        popupMenu.show(e.getComponent(), x, y); 
       } 
      } 
     }); 

     frame.getContentPane().add(jp); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setPreferredSize(new Dimension(400, 400)); 
     frame.setLocationRelativeTo(null); 
     frame.setJMenuBar(menuBar); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new FunnyMenuTest().makeUI(); 
      } 
     }); 
     UIManager.put("PopupMenuUI", "JMenu.GradientPopupMenuUI"); 
    } 
} 

HEPL你

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.LinearGradientPaint; 
import java.awt.MultipleGradientPaint.CycleMethod; 
import java.awt.Paint; 
import java.awt.geom.Point2D; 
import javax.swing.JComponent; 
import javax.swing.plaf.ComponentUI; 
import javax.swing.plaf.basic.BasicPopupMenuUI; 

public class GradientPopupMenuUI extends BasicPopupMenuUI { 

    private Paint paint = new LinearGradientPaint(new Point2D.Double(5, -5), new Point2D.Double(), 
      new float[]{0.2F, 0.9F}, new Color[]{Color.WHITE, Color.GREEN}, CycleMethod.REFLECT); 

    public static ComponentUI createUI(JComponent c) { 
     return new GradientPopupMenuUI(); 
    } 

    @Override 
    public void paint(Graphics g, JComponent c) { 
     ((Graphics2D) g).setPaint(paint); 
     g.fillRect(0, 0, c.getWidth(), c.getHeight()); 
    } 
} 
+0

+1的梯度彈出菜單。唯一的疑問是UI是L&F的依賴。改變L&F可能會損壞它。 – StanislavL

+0

@mKorble:在博客的鏈接中,使用了JMenuItem。但是這個JMenuItem不能與SystemTray一起使用。所以你有什麼想法我怎麼能在系統托盤中使用jMenuItem(或JPopupMenu)? – Krutika

+0

@Krutika是你閱讀教程,鏈接你張貼?,系統托盤只是圖標與JPopupMenu實施功能,檢查http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/和http: //download.oracle.com/javase/6/docs/api/java/awt/TrayIcon.html – mKorbel