我正在尝试制作我的第一个正确定制的 GUI
您应该从阅读 Swing 教程开始。我不太确定您要做什么,但您的方法肯定是错误的。
您可以从How to Use Menus 开始,它展示了如何使用 ActionListener 来处理鼠标点击。鼠标点击通常在菜单项上处理,而不是在菜单上。您通常会有类似“文件”菜单的内容,其中包含“退出”菜单项。
然后,我还将查看 JMenu API 的各种方法,这些方法允许您在鼠标悬停或选择菜单时更改图标。也许 setRolloverEnabled(), setRolloverIcon() 就是你要找的。p>
如果您仍有问题,请发布 SSCCE 来说明问题。
更新:
正如 Hovercraft 所述,翻转支持不适用于菜单或菜单项。有两个问题。首先,这些组件使用不同的 MouseListener。侦听器不侦听 mouseEntered 和 mouseExited 事件。第二个问题是两个组件的UI都已经自定义了,自定义的Icon绘制代码没有考虑到按钮的滚动状态。
添加 MouseListener 很简单。自定义 UI(这是正确的解决方案)以正确支持翻转更复杂。
对于一个似乎可行的简单技巧,我只是更新 MouseListener 中的图标,而不是让 UI 确定要绘制哪个图标。我建议您忘记此要求并使用不会更改菜单和菜单项图标的普通 UI。使用以下内容需您自担风险:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonRollover extends JFrame
{
Icon normal;
Icon rollover;
Icon selected;
public ButtonRollover()
{
MouseListener ml = new RolloverButtonListener();
normal = new ColorIcon(Color.GREEN, 10, 10);
rollover = new ColorIcon(Color.RED, 10, 10);
selected = new ColorIcon(Color.BLUE, 10, 10);
setLayout( new FlowLayout() );
JMenuBar menuBar = new JMenuBar();
setJMenuBar( menuBar );
JMenu menu = (JMenu)createButton(new JMenu(), "Menu");
menu.addMouseListener( ml );
menuBar.add( menu );
JMenuItem menuItem = (JMenuItem)createButton(new JMenuItem(), "MenuItem");
menuItem.addMouseListener( ml );
menu.add( menuItem );
JButton button = (JButton)createButton(new JButton(), "Button");
add( button );
JCheckBox checkBox = (JCheckBox)createButton(new JCheckBox(), "CheckBox");
add( checkBox );
JRadioButton radioButton = (JRadioButton)createButton(new JRadioButton(), "RadioButton");
add( radioButton );
}
public AbstractButton createButton(AbstractButton button, String text)
{
button.setText( text );
button.setIcon( normal );
button.setSelectedIcon( selected );
button.setRolloverIcon( rollover );
button.setRolloverSelectedIcon( rollover );
System.out.println( text );
MouseListener[] mls = button.getMouseListeners();
for (MouseListener ml: mls)
{
System.out.println( "\t" + ml);
}
return button;
}
class RolloverButtonListener extends MouseAdapter
{
private Icon normal;
public void mouseEntered(MouseEvent e)
{
AbstractButton b = (AbstractButton) e.getSource();
ButtonModel model = b.getModel();
if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e))
{
normal = b.getIcon();
b.setIcon(b.getRolloverIcon());
model.setRollover(true);
}
}
public void mouseExited(MouseEvent e)
{
AbstractButton b = (AbstractButton) e.getSource();
ButtonModel model = b.getModel();
if(b.isRolloverEnabled())
{
b.setIcon( normal );
model.setRollover(false);
}
};
}
public class ColorIcon implements Icon
{
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
public static void main(String[] args)
{
try
{
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
ButtonRollover frame = new ButtonRollover();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setSize(400, 200);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}