问题描述
我有一个带有一些选项卡的JTabbedPane,并且这些选项卡旁边有许多未使用的额外空间.因此,我尝试使用它并在其中放置一些按钮(例如在Eclipse中).我将按钮放在GlassPane上:
I have a JTabbedPane with some tabs and a lot of unused extra space next to the tabs. So I'm trying to use it and place some buttons there (like in Eclipse). I put the buttons on a GlassPane:
JPanel glasspane = getPanelWithButtons();
// panel with FlowLayout.RIGHT
frame.setGlassPane(glasspane);
glasspane.setOpaque(false);
glasspane.setVisible(true);
这有效,并且我仍然可以单击gui的其他元素(我发现的大多数搜索结果都是关于如何防止的信息).到目前为止,唯一的问题是,当鼠标指针悬停在JSplitPane的条形上方时,它不会变为该双向水平箭头.我该如何恢复这种行为?
This works, and I still can click through on the other elements of my gui (most search results I found are about how to prevent this). The only problem so far is that the mouse pointer doesn't change to that double-ended horizontal arrow when it hovers over the bar of a JSplitPane. How can I get this behaviour back?
编辑
我发现在玻璃窗格下没有显示来自 any 组件的鼠标更改事件.这些组件会将鼠标光标更改为手形光标,变焦镜头等.这些鼠标指针更改均不再起作用.我猜这是因为在玻璃窗格中,需要对玻璃窗格进行鼠标指针更改,但是我不想手动更改所有鼠标指针.
I found that no mouse changing events from any component under the glass pane are shown. Those components would change the mouse cursor to a hand cursor, zoom lenses and others. None of these mouse pointer changes have an effect any more. I guess this is because with the glass pane, the mouse pointer change needs to be made to the glass pane, but I don't want to do all the mouse pointer changing manually.
推荐答案
好.我想出了办法.
尽管我花了5个多小时来了解所有背后的内容,但是解决方案却非常简单.
Although I spend more than 5 hours to understand all things behind, but the solution is very simple.
只需覆盖玻璃面板的'public boolean contains(int x,int y)'方法即可.
Just overwrite 'public boolean contains(int x, int y)' method of glass panel.
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);
}
这篇关于摇摆:GlassPane防止更改鼠标指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!