我试图让我的第一个正确定制GUI
你应该开始通过阅读Swing指南。我不确定你想要做什么,但你的方法肯定是错误的。
您可以从How to Use Menus开始,它显示了如何使用ActionListener来处理鼠标单击。鼠标点击通常在菜单项上处理,而不是在菜单上处理。您通常会拥有一个类似于“文件”菜单的菜单,该菜单包含一个“退出”菜单项。
然后,我还会查看各种方法的JMenu API,以便您在鼠标悬停或选择菜单时更改图标。也许setRolloverEnabled(),setRolloverIcon()是你正在寻找的。
如果您仍然有问题,然后发布SSCCE,说明问题。
更新:
正如气垫船提到,侧翻的支持不会为菜单或菜单项工作。有两个问题。首先,这些组件使用不同的MouseListener。侦听器不侦听mouseEntered和mouseExited事件。第二个问题是这两个组件的用户界面已经被自定义,并且自定义的图标绘制代码没有考虑按钮的滚动状态。
添加MouseListener很容易。定制用户界面(这是正确的解决方案)以更好地支持滚动更为复杂。
对于一个简单的黑客来说,我只是更新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);
}
}
-1,侧翻支持内置到AbstractButton组件中。不要重新发明轮子。 – camickr
好点,我已经摆脱了摇摆的世界了一下。 – Bryan
@camickr:但它不能在JMenu对象上工作,即使它们从AbstractButton继承。为了证明这一点,在JMenu对象或其模型上放置ChangeListener,您将看不到鼠标悬停的响应。 –