我有一个使用本机LAF的Java应用程序,如下所示:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
这很好用,但是,我试图让按钮有一个红色背景,但最终会像这样:
正如您所看到的,我在按钮上设置了背景和前景,但结果并不令人满意。 有没有办法让按钮在没有继承JButton的情况下绘制红色背景?
答案 0 :(得分:4)
你必须明白,在Swing的Look&感觉结构,它是JButton的UI委托来完成它的绘图,而不是JButton本身,因此setBackground(...)
在这种情况下不能很好地工作。你可能最好不要在按钮上添加一个图标。
答案 1 :(得分:2)
对于任何遇到问题的人来说,这是我采用的解决方案:
我使用ImageIcon切换为使用添加为按钮子项的图像:
BufferedImage stopPicture = null;
try {
stopPicture = ImageIO.read(new File("stop.png"));
} catch (IOException ex) { }
JLabel picLabel = new JLabel(new ImageIcon( stopPicture ));
JButton btnStop = new JButton("");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SerialTest.getInstance().stopMoving();
}
});
btnStop.add(picLabel);
答案 2 :(得分:2)
我在点击和鼠标悬停上创建了我自己的 CustomColorButton 漂亮的渐变结束效果/ p>
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
您可以像这样创建一个新按钮:
CustomColorButton button = new CustomColorButton(Color.RED, Color.WHITE); // Background and font color
button.setText("Color button!");
CustomColorButton 类:
public class CustomColorButton extends JButton implements ActionListener, MouseListener
{
private boolean hovered = false;
private boolean clicked = false;
private Color normalColor = null;
private Color lightColor = null;
private Color darkColor = null;
public CustomColorButton(Color normalRedColor, Color fontColor)
{
setForeground(fontColor);
this.normalColor = normalRedColor;
this.lightColor = normalRedColor.brighter();
this.darkColor = normalRedColor.darker();
addActionListener(this);
addMouseListener(this);
setContentAreaFilled(false);
}
/**
* Overpainting component, so it can have different colors
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp = null;
if (clicked)
gp = new GradientPaint(0, 0, darkColor, 0, getHeight(), darkColor.darker());
else if (hovered)
gp = new GradientPaint(0, 0, lightColor, 0, getHeight(), lightColor.darker());
else
gp = new GradientPaint(0, 0, normalColor, 0, getHeight(), normalColor.darker());
g2d.setPaint(gp);
// Draws the rounded opaque panel with borders
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // For High quality
g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 7, 7);
g2d.setColor(darkColor.darker().darker());
g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 7, 7);
super.paintComponent(g);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
System.out.println("Button clicked!");
}
@Override
public void mouseClicked(MouseEvent arg0)
{
}
@Override
public void mouseEntered(MouseEvent arg0)
{
hovered = true;
clicked = false;
repaint();
}
@Override
public void mouseExited(MouseEvent arg0)
{
hovered = false;
clicked = false;
repaint();
}
@Override
public void mousePressed(MouseEvent arg0)
{
hovered = true;
clicked = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent arg0)
{
hovered = true;
clicked = false;
repaint();
}
}
答案 3 :(得分:0)
如果“没有子类化”意味着不必自己扩展它,那么你可以选择使用SwingX JXButton,它扩展了JButton以使用画家(等等):
JButton button = new JButton();
button.setBackground(bg);
变为
JXButton button = new JXButton();
button.setBackgroundPainter(new MattePainter(bg));
如果你必须坚持基础JButton,我认为没有一个解决方案,因为你和L& F的工作方式。