gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:41:02 25 4
gpt4 key购买 nike

所以,我有 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 吗?

最佳答案

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

将其放入代码中(对于运动监听器)如下所示:

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);

关于java - 带图标的圆形 JButton;不直接指向时会被点击;将光标设置为 HAND_CURSOR 适用于图标之外的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44936145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com