我有一个JTabbedPane和一些制表符和许多未使用的额外空间旁边的选项卡。因此,我尝试使用它,并在那里放置一些按钮(如在Eclipse中)。我把按钮放在一个GlassPane上:
JPanel glasspane = getPanelWithButtons();
// panel with FlowLayout.RIGHT
frame.setGlassPane(glasspane);
glasspane.setOpaque(false);
glasspane.setVisible(true);这是可行的,我仍然可以点击我的gui的其他元素(我发现的大多数搜索结果都是关于如何防止这种情况的)。到目前为止,唯一的问题是,当鼠标指针在JSplitPane的条形上盘旋时,它不会改变为双端水平箭头。我怎么才能找回这种行为呢?
编辑
我发现,在玻璃窗格下没有显示任何组件的鼠标更改事件。这些组件会将鼠标光标更改为手动光标、变焦镜头等。这些鼠标指针的更改都没有效果了。我想这是因为使用玻璃窗格时,需要对玻璃窗格进行鼠标指针更改,但我不想手动更改所有鼠标指针。
发布于 2012-09-26 01:20:48
井。我想出了怎么做。
虽然我花了5个多小时去了解背后的一切,但是解决办法很简单。
只需覆盖玻璃面板的“公共布尔包含( int,int)”方法。
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);
}发布于 2012-09-14 06:05:45
井。我找到了一个解决问题的方法“把按钮放在标签旁边”。我不再使用玻璃面板,而是直接放置按钮:
buttonpanel.setBounds(...);
frame.getLayeredPane().add(buttonpanel, 1);这能解决我的问题。它有点复杂,涉及到手工布局和监听帧调整大小的事件。
因为我仍然想知道如何用玻璃来完成这个任务,所以我不会接受这个答案。也许有人想出了玻璃玻璃的解决方案。
https://stackoverflow.com/questions/12347320
复制相似问题