各位达人:
您好!目前我用swing做界面,当按钮选中焦点的时候,焦点的显示非常不明显。目前我用的方法是在CSDN上的方法:
自己写一个:MyButtonUI
package ui; import javax.swing.AbstractButton;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalButtonUI; import java.awt.Color;
import java.awt.Font;
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); 这样的方法,可以将:焦点的边框 和 按钮的边框的距离近一些!
但小弟请教:
1,是否可以将 焦点的边框 加粗
2,是否可以将 焦点的边框 变色
反正:目的只有一个,就是将焦点搞得醒目一些,让用户能够一眼看清楚~!!
大人们看是否能在上面的 MyButtonUI 上改写,或者 用其他方法来 设置button的属性!
谢谢了!
您好!目前我用swing做界面,当按钮选中焦点的时候,焦点的显示非常不明显。目前我用的方法是在CSDN上的方法:
自己写一个:MyButtonUI
package ui; import javax.swing.AbstractButton;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalButtonUI; import java.awt.Color;
import java.awt.Font;
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); 这样的方法,可以将:焦点的边框 和 按钮的边框的距离近一些!
但小弟请教:
1,是否可以将 焦点的边框 加粗
2,是否可以将 焦点的边框 变色
反正:目的只有一个,就是将焦点搞得醒目一些,让用户能够一眼看清楚~!!
大人们看是否能在上面的 MyButtonUI 上改写,或者 用其他方法来 设置button的属性!
谢谢了!
paintFocus里面设置颜色和线条粗细不行吗?
如何设置 颜色,和线条啊?
这个如何使用!
没有方法提示啊!
package component;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;import com.sun.java.swing.plaf.windows.WindowsButtonUI;public class ButtonHighlighted extends JFrame
{
private static final long serialVersionUID = -5513217060123469552L; JButton okButton = new JButton("OK");
JButton hlButton = null;
JPanel centerpiece = new JPanel(); public ButtonHighlighted()
{
hlButton = new JButton("Highlighted");
hlButton.setUI(new HighlightedButtonUI()); centerpiece.setLayout(new FlowLayout());
centerpiece.add(okButton);
centerpiece.add(hlButton); getContentPane().setLayout(new BorderLayout());
getContentPane().add(centerpiece, BorderLayout.CENTER);
} public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex)
{
ex.printStackTrace();
} JFrame.setDefaultLookAndFeelDecorated(true); ButtonHighlighted frame = new ButtonHighlighted();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 100);
frame.setLocation(200, 200);
frame.setVisible(true);
} private class HighlightedButtonUI extends WindowsButtonUI
{
/*
int rectGapX;
int rectGapY;
int rectGapW;
int rectGapH; public HighlightedButtonUI()
{
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); Rectangle focusRect = new Rectangle();
String text = b.getText();
if (text != null)
{
focusRect.setBounds(textRect);
}
else
{
focusRect.setBounds(iconRect);
} g.setColor(Color.RED);
g.drawRect(focusRect.x - 1, focusRect.y - 1, focusRect.width + 1, focusRect.height + 1); super.paintFocus(g, b, viewRect, textRect, iconRect);
}
}
}
private class HighlightedButtonUI extends WindowsButtonUI
{
int rectGapX;
int rectGapY;
int rectGapWidth;
int rectGapHeight; public HighlightedButtonUI()
{
rectGapX = UIManager.getInt("ButtonUI.dashedRectGapX");
rectGapY = UIManager.getInt("ButtonUI.dashedRectGapY");
rectGapWidth = UIManager.getInt("ButtonUI.dashedRectGapWidth");
rectGapHeight = 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); g.setColor(Color.RED);
g.drawRect(rectGapX + 1, rectGapY + 1, b.getWidth() - rectGapWidth - 3, b.getHeight() - rectGapHeight - 3); super.paintFocus(g, b, viewRect, textRect, iconRect);
}
}