public class DefaultButton
{
public DefaultButton()
{
JFrame f = new JFrame("Button Test");
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
JButton b1 = new JButton("Button Test");
b1.setMnemonic('T');
f.getRootPane().setDefaultButton(b1);
b1.addMouseListener(new MouseAdapter()
{
/**
* 鼠标进入按钮区域
*/
@Override
public void mouseEntered(MouseEvent e)
{
JButton eventBtn = (JButton) e.getSource();
eventBtn.setText("鼠标进入");
super.mouseEntered(e);
}
/**
* 鼠标移除按钮区域
*/
@Override
public void mouseExited(MouseEvent e)
{
JButton eventBtn = (JButton) e.getSource();
eventBtn.setText("鼠标移除");
super.mouseExited(e);
}
});
contentPane.add(b1);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new DefaultButton();
}
}