JTaskPane 內由 JTaskPaneGroup 組成,而 JTaskPaneGroup 則由 AbstractAction 所組成。很不幸的是 AbstractAction 卻沒有 addMouseListener 函數,也就等於無法加入 JPopupMenu 功能。

經由查看原始碼才發現,原來 AbstractAction 都會被轉換成 JLinkButton,那何不一開始就改成 JLinkButton 呢?原來 JLinkButton 是繼承自 JButton,預設會畫一個大外框,造成二個 JLinkButton 間距太大不好看。而 L2FProd 的做法是在 BasicTaskPaneGroupUI.java 內寫一個 createAction 新增一個 JLinkButton 並覆寫 updateUI 來重設 JButton 的外觀。

public class JLinkButton extends com.l2fprod.common.swing.JLinkButton implements MouseListener { private JPopupMenu popupMenu; public JLinkButton() { this("...."); } public JLinkButton(String name) { super(name); configure(); addMouseListener(this); } protected void configure() { setOpaque(false); setBorder(null); setBorderPainted(false); setFocusPainted(true); setForeground(UIManager.getColor("TaskPaneGroup.titleForeground")); } public void updateUI() { super.updateUI(); configure(); } public void add(JPopupMenu menu) { popupMenu = menu; } protected void showPopupMenu(MouseEvent arg0) { if ((popupMenu != null) && popupMenu.isPopupTrigger(arg0)) { popupMenu.show( arg0.getComponent(), arg0.getX(), arg0.getY()); } } public void mousePressed(MouseEvent arg0) { showPopupMenu(arg0); } public void mouseReleased(MouseEvent arg0) { showPopupMenu(arg0); } public void mouseClicked(MouseEvent arg0) { // nothing to do } public void mouseEntered(MouseEvent arg0) { // nothing to do } public void mouseExited(MouseEvent arg0) { // nothing to do } }

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()