圆形 JButton 带图标;当不直接指向时被单击;将光标设置为 HAND_CURSOR 适用于图标外的区域



所以,我有JFrameJPanelJPanel有两个JToggleButtons。每个按钮都显示为圆形按钮的图标。当光标悬停在按钮上方时,它应该变成手形。

public class UserInterface extends JFrame  {
int width;
int height;
JPanel panel;
public static void main(String[] args)  {
run();
}
public UserInterface()  {
setup();
}
private void setup()  {
width=800;
height=600;
panel=new UserInterfacePanel();
add(panel);
setSize(width, height);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void run()  {
UserInterface gui=new UserInterface();
gui.setVisible(true);
}
}   

class UserInterfacePanel extends JPanel {
private JToggleButton startButton;
private JToggleButton stopButton;
public static void main(String[] args)  {
}
public UserInterfacePanel()  {
setup();
}
private void setup()  {
setLayout(new GridLayout(1,2));
setupButtons();
setupButtonsActions();
add(startButton);
add(stopButton);
}
private void setupButtons()  {
ImageIcon iconStartButton = new ImageIcon("C:\Users\parsecer\Desktop\imgs\greenReleased.png");
ImageIcon iconStopButton=new ImageIcon("C:\Users\parsecer\Desktop\imgs\redReleased.png");
startButton=new JToggleButton(iconStartButton);
stopButton=new JToggleButton(iconStopButton);
startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
stopButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
startButton.setHorizontalAlignment(JButton.CENTER);
stopButton.setHorizontalAlignment(JButton.CENTER);
startButton.setAlignmentX(CENTER_ALIGNMENT);
stopButton.setAlignmentX(CENTER_ALIGNMENT);
startButton.setAlignmentY(CENTER_ALIGNMENT);
stopButton.setAlignmentY(CENTER_ALIGNMENT);
setupButtonIcon(startButton);
setupButtonIcon(stopButton);
ImageIcon iconStartButtonPressed=new ImageIcon("C:\Users\parsecer\Desktop\imgs\greenPressed.png");
startButton.setDisabledIcon(iconStartButton);
startButton.setPressedIcon(iconStartButtonPressed);
startButton.setSelectedIcon(iconStartButtonPressed);
ImageIcon iconStopButtonPressed=new ImageIcon("C:\Users\parsecer\Desktop\imgs\redPressed.png");
stopButton.setDisabledIcon(iconStopButton);
stopButton.setPressedIcon(iconStopButtonPressed);
stopButton.setSelectedIcon(iconStopButtonPressed);
}

private void setupButtonIcon(JToggleButton button)  {
button.setBorder(BorderFactory.createEmptyBorder());
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusable(false);
button.setPreferredSize(new Dimension(80, 80));
button.setFocusPainted(false);
button.setOpaque(false);
}
}

问题是,光标变成了图标本身之外的手。单击按钮也是如此 - 无论我垂直单击何处,按钮都会被按下 - 因为它与按钮位于同一列中,或者当我水平相对接近它时。

我想第一个问题可能与按钮图像有关,但背景被删除了。第二个可以链接到GridLayout吗?

我会做这样的事情: 定义按钮的形状,大概是一个圆形/椭圆形。 在单击和运动侦听器中,检查事件是否发生在该形状内。如果是这样,那就做你的事吧。

放入代码中,它将(对于 motionlistener)如下所示:

Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
// create your shape here
Shape shape = new Ellipse2D.Float(0, 0, 30, 30);
MouseMotionAdapter motionListener = new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e); 
JToggleButton src = (JToggleButton) e.getSource();
if (shape.contains(e.getPoint())) {
src.setCursor(handCursor);
} else {
src.setCursor(normalCursor);
}
}
};
startButton.addMouseMotionListener(motionListener);
stopButton.addMouseMotionListener(motionListener);

最新更新