java按钮点击处于当前焦点的时候,能否边框线不在文字边沿,而是在按钮边沿呢?就像windows的按钮那样,焦点时的边框线都是在按钮边沿的。

解决方案 »

  1.   

    只用重写MetalButtonUI就可以了, 写了个简单的,给你参考import javax.swing.AbstractButton;
    import javax.swing.UIManager;
    import javax.swing.plaf.metal.MetalButtonUI;
    import java.awt.Graphics;
    import java.awt.Rectangle;public class MyButtonUI extends MetalButtonUI{

        int rectGapX;
        int rectGapY;
        int rectGapW;
        int rectGapH;

        public MyButtonUI() {
            rectGapX = UIManager.getInt("ButtonUI.dashedRectGapX");
            rectGapY = UIManager.getInt("ButtonUI.dashedRectGapY");
            rectGapW = UIManager.getInt("ButtonUI.dashedRectGapWidth");
            rectGapH = UIManager.getInt("ButtonUI.dashedRectGapHeight");
        }    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, 
      Rectangle textRect, Rectangle iconRect) {

            textRect = new Rectangle(rectGapX+3, rectGapY+3, b.getWidth()-rectGapW-6,    
                                     b.getHeight()-rectGapH-6);
            super.paintFocus(g, b, viewRect, textRect, iconRect);
        }
    }使用如下JButton button = new JButton("test Button");
    MyButtonUI bui = new MyButtonUI();
    button.setUI(bui);
      

  2.   

    这个有非常简单的方法,JDK本身就提供了这种支持JButton button = new JButton("测试");
    Insets inset = new Insets(1,1,1,1); //这个数值就是那个边框线离四个边框的距离
    button.setMargin(inset); OK了