我想在鼠标悬停时将JButton添加到另一个JButton。这是我的实际代码:
public class JFrameButton extends JFrame
{
public static void main(String[] args)
{
main();
}
public static void main()
{
new JFrameButton();
}
public JFrameButton()
{
boutonExterne = new JButton("Externe");
boutonInterne = new JButton("Interne");
boutonExterne.setLayout(new BorderLayout());
setSize(200, 200);
setLocation(100, 100);
setVisible(true);
boutonExterne.addMouseListener(new MouseAdapter()
{
@Override
public void mouseExited(MouseEvent e)
{
boutonExterne.remove(boutonInterne);
JFrameButton.this.revalidate();
JFrameButton.this.repaint();
}
@Override
public void mouseEntered(MouseEvent e)
{
JFrameButton.this.boutonExterne.add(boutonInterne, BorderLayout.SOUTH);
JFrameButton.this.revalidate();
JFrameButton.this.repaint();
}
});
this.add(boutonExterne);
}
private JButton boutonExterne,boutonInterne;
}
答案 0 :(得分:1)
完全。从内部按钮上的外部按钮内容移动鼠标后,调用内部按钮的mouseExit,使内部按钮隐藏。
快速包看起来像:
boutonExterne.addMouseListener(new MouseAdapter() {
boolean ignoreExit=false;
@Override
public void mouseExited(final MouseEvent e) {
if (!ignoreExit) {
boutonExterne.remove(boutonInterne);
JFrameButton.this.revalidate();
JFrameButton.this.repaint();
}
ignoreExit=true;
}
@Override
public void mouseEntered(final MouseEvent e) {
JFrameButton.this.boutonExterne.add(boutonInterne, BorderLayout.SOUTH);
JFrameButton.this.revalidate();
JFrameButton.this.repaint();
ignoreExit=true;
}
});