0
我已经通过扩展的JButton创造了我的应用程序的自定义按钮,我知道了画我想要的方式,但由于某些原因,即使我打电话setMargin()
在构造函数中,按钮有0保证金,就像这样:保证金定制的JButton - Java的
有什么,我在我的代码做错了什么?标准的JButtons有多余,但我的自定义按钮没有?
我的按钮的Java代码:
public class CToolbarButton extends JButton
{
private static final Dimension SIZE = new Dimension(48, 48);
private static final int MARGIN_VAL = 50;
private static final Insets MARGIN = new Insets(MARGIN_VAL, MARGIN_VAL, MARGIN_VAL, MARGIN_VAL);
private static final Color FILL_NORM = Color.GRAY;
private static final Color FILL_ACTIVE = new Color(FILL_NORM.getRed()-25, FILL_NORM.getGreen()-25, FILL_NORM.getBlue()-25);
private static final Color BORDER_NORM = Color.BLACK;
private static final Color BORDER_ACTIVE = Color.YELLOW;
public CToolbarButton()
{
super();
setContentAreaFilled(false);
setFocusable(false);
setMargin(MARGIN);
}
@Override
public void paintComponent(Graphics g)
{
if (getModel().isArmed())
{
g.setColor(FILL_ACTIVE);
}
else
{
g.setColor(FILL_NORM);
}
g.fillRect(0, 0, getWidth(), getHeight());
}
@Override
public void paintBorder(Graphics g)
{
if (getModel().isArmed())
{
g.setColor(BORDER_ACTIVE);
}
else
{
g.setColor(BORDER_NORM);
}
g.drawRect(0, 0, getWidth(), getHeight());
}
@Override
public boolean contains(int x, int y)
{
return (x >= 0 &&
x <= getWidth() &&
y >= 0 &&
y <= getHeight());
}
@Override
public Dimension getPreferredSize()
{
return SIZE;
}
@Override
public Dimension getMinimumSize()
{
return SIZE;
}
@Override
public Dimension getMaximumSize()
{
return SIZE;
}
}
*“我已经通过扩展的JButton创造了我的应用程序自定义按钮,” *为什么呢?它具有什么特性,超出了Swing JButton的可能性? *“我已经把它画成了我想要的样子,但是......”*颜色我没有惊讶有一个'但'。 –
因为我想自定义绘制它,并且我不知道如何在不创建子类的情况下做到这一点... – rcplusplus
什么样的自定义绘画?具体.. –