gpt4 book ai didi

java - Swing:GlassPane 防止鼠标指针改变

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:30:27 24 4
gpt4 key购买 nike

我有一个带有一些选项卡的 JTabbedPane,并且选项卡旁边有很多未使用的额外空间。所以我正在尝试使用它并在那里放置一些按钮(就像在 Eclipse 中一样)。我将按钮放在 GlassPane 上:

        JPanel glasspane = getPanelWithButtons();
// panel with FlowLayout.RIGHT
frame.setGlassPane(glasspane);
glasspane.setOpaque(false);
glasspane.setVisible(true);

这有效,我仍然可以点击我的 gui 的其他元素(我发现的大多数搜索结果都是关于如何防止这种情况的)。到目前为止唯一的问题是,当鼠标指针悬停在 JSplitPane 的栏上时,鼠标指针不会变为那个双头水平箭头。我怎样才能恢复这种行为?

编辑

我发现玻璃 Pane 下的任何 组件都没有显示鼠标更改事件。这些组件会将鼠标光标更改为手形光标、变焦镜头等。这些鼠标指针更改都不再起作用。我猜这是因为对于玻璃板,需要对玻璃板进行鼠标指针更改,但我不想手动更改所有鼠标指针。

最佳答案

嗯。我知道怎么做了。

虽然我花了5个多小时才明白后面所有的东西,但是解决方法很简单。

只需覆盖玻璃面板的'public boolean contains(int x, int y)'方法。

public static void main(String[] args)
{
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(800, 600);

final JSplitPane panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JPanel(), new JPanel());

frame.getContentPane().add(panel, BorderLayout.CENTER);

final JPanel glassPane = new JPanel(){
@Override
public boolean contains(int x, int y)
{
Component[] components = getComponents();
for(int i = 0; i < components.length; i++)
{
Component component = components[i];
Point containerPoint = SwingUtilities.convertPoint(
this,
x, y,
component);
if(component.contains(containerPoint))
{
return true;
}
}
return false;
}
};
glassPane.setOpaque(false);
JButton button = new JButton("haha");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("haha");
}
});
glassPane.add(button);
glassPane.setBorder(BorderFactory.createLineBorder(Color.red));
frame.setGlassPane(glassPane);

//try to comment out this line to see the difference.
glassPane.setVisible(true);

frame.setVisible(true);
}

关于java - Swing:GlassPane 防止鼠标指针改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12347320/

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