为了操纵鼠标事件,如悬停,你”你必须对待这些事件如果要做的一个方法是创建你自己的按钮。
编辑
添加鼠标监听到你的按钮,由Sergiy Medvynskyy作为回答,会是一个更好的做法,因为没有必要与按钮类上做文章。
您也可以与拉芳玩(UIManager的外观),因为它可以增加不需要的效果,UI组件。
请注意,这可能会影响所有接口组件。
以下是一个基本示例。
按钮:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.border.Border;
public class UWPButton extends JButton{
private final Border regularBorder;
private final Border hoverBorder;
private final Color bgColor = new Color(204, 204, 204);
private final Color transparent;
private final Font metroFont = new Font("Segoe UI", Font.PLAIN, 20);
public UWPButton() {
super();
// thickness of the button's borders
int thickness = 4;
// Create a transparent color, to use for the regular border
// could also be the bgColor (204 204 204)
Color c = new Color(1f, 0f, 0f, 0f);
transparent = c;
// Creates a compound border to be present on the button majority of the time.
// uses an invisible line border on the outside in order to change border smoothly
// the inside border is just an empty border for insets.
regularBorder = BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(transparent, thickness), //outside
BorderFactory.createEmptyBorder(10, 14, 10, 14)); //inside
// Creates a compound border which will be used when the mouse hovers the button.
// the outside border has a darker colour than the background
// the inside border is just an empty border for insets.
hoverBorder = BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(bgColor.darker(), thickness), //outside
BorderFactory.createEmptyBorder(10, 14, 10, 14)); //inside
// configures the button
initButton();
}
// Here is where the mouse events are treated.
@Override
protected void processMouseEvent(MouseEvent e) {
// Gets the ID of the event,
// if it is a mouse entering the button, sets the new border
// if it is the mouse exiting the button, resets the border to the default.
switch(e.getID()){
case MouseEvent.MOUSE_ENTERED:
this.setBorder(hoverBorder);
break;
case MouseEvent.MOUSE_EXITED:
this.setBorder(regularBorder);
break;
}
// the parent then does all the other handling.
super.processMouseEvent(e);
}
// Configures the button.
private void initButton(){
setFont(metroFont);
setBorder(regularBorder);
setBackground(bgColor);
setFocusPainted(false);
}
}
改变拉芳:如果您通过IDE拖放创建了框架,你应该有这样的事情里面你的主(这是从NetBeans中):
CHANGE “Nimbus”改为“Metro”(或删除LaF设置),以便界面管理器不会渲染所有Nimbus内容。 注意:没有“Metro”LaF,这只是为了禁用应用的LaF。
// other stuff above
//
// change "Nimbus" for something else or just remove this try-catch block
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// other stuff bellow
更多关于阿姆斯特朗基金会(外观): https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html