我有一个 JTabbedPane,其中有一些选项卡以及选项卡旁边有很多未使用的额外空间。所以我尝试使用它并在那里放置一些按钮(就像在 Eclipse 中一样)。我将按钮放在 GlassPane 上:
JPanel glasspane = getPanelWithButtons();
// panel with FlowLayout.RIGHT
frame.setGlassPane(glasspane);
glasspane.setOpaque(false);
glasspane.setVisible(true);
这有效,我仍然可以点击我的 gui 的其他元素(我发现的大多数搜索结果都是关于如何prevent这)。到目前为止唯一的问题是,当鼠标指针悬停在 JSplitPane 的栏上时,鼠标指针不会更改为双端水平箭头。我怎样才能恢复这种行为?
EDIT
我发现没有鼠标改变事件any显示玻璃板下方的组件。这些组件会将鼠标光标更改为手形光标、变焦镜头等。这些鼠标指针更改都不再产生影响。我想这是因为对于玻璃窗格,需要对玻璃窗格进行鼠标指针更改,但我不想手动更改所有鼠标指针。
出色地。我知道该怎么做了。
虽然我花了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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)