gpt4 book ai didi

java - 使用 setBackground() 设置 JButton 边框?

转载 作者:行者123 更新时间:2023-12-02 03:54:08 26 4
gpt4 key购买 nike

所以我有一些代码来更改按钮的背景颜色,但是当我使用这些代码时,它会设置背景颜色和边框颜色。

Picture

有没有办法避免这种情况发生?

谢谢!

代码:

public void highlight(ArrayList<JButton> buttons){
for (JButton j : buttons) {
j.setBorder(new JButton().getBorder());
j.setBackground(Color.GREEN);
j.setForeground(Color.WHITE);
j.setOpaque(true);
j.setBorderPainted(false);
j.setFocusPainted(false);
j.setBorderPainted(false);
}
}

最佳答案

好吧,这是一个黑客版本,基于这样的想法:您想要保持按钮当前的“外观和感觉”,但想要使用不同的填充颜色

这只是在按钮上应用“突出显示”颜色...

Highlighted buttons

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.kaizen.core.ui.ImageUtilities;

public class Main {

public static void main(String[] args) {
new Main();
}

public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridBagLayout());
HighlightButton btn = new HighlightButton("Help");
btn.setMargin(new Insets(20, 20, 20, 20));
btn.setHighlight(new Color(255, 0, 0, 64));
add(btn);

btn = new HighlightButton("Help");
btn.setMargin(new Insets(20, 20, 20, 20));
btn.setHighlight(new Color(0, 255, 0, 64));
add(btn);

btn = new HighlightButton("Help");
btn.setMargin(new Insets(20, 20, 20, 20));
btn.setHighlight(new Color(0, 0, 255, 64));
add(btn);

btn = new HighlightButton("Help");
btn.setMargin(new Insets(20, 20, 20, 20));
add(btn);
}

}

public class HighlightButton extends JButton {

private Color highlight;

public HighlightButton() {
setOpaque(false);
}

public HighlightButton(String text) {
super(text);
setOpaque(false);
}

public void setHighlight(Color color) {
if (color != highlight) {
Color old = highlight;
this.highlight = color;
firePropertyChange("highlight", old, highlight);
repaint();
}
}

public Color getHighlight() {
return highlight;
}

@Override
protected void paintComponent(Graphics g) {
Color highlight = getHighlight();
if (highlight != null) {
BufferedImage img = createCompatibleImage(getWidth(), getHeight(), Transparency.TRANSLUCENT);
Graphics2D g2d = img.createGraphics();
super.paintComponent(g2d);
g2d.dispose();

BufferedImage mask = generateMask(img, getHighlight(), 1f);
g.drawImage(img, 0, 0, this);
g.drawImage(mask, 0, 0, this);
} else {
super.paintComponent(g);
}
}

}

public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency);
image.coerceData(true);
return image;
}

public static GraphicsConfiguration getGraphicsConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}

public static BufferedImage generateMask(BufferedImage imgSource, Color color, float alpha) {

int imgWidth = imgSource.getWidth();
int imgHeight = imgSource.getHeight();

BufferedImage imgMask = createCompatibleImage(imgWidth, imgHeight, Transparency.TRANSLUCENT);
Graphics2D g2 = imgMask.createGraphics();
applyQualityRenderingHints(g2);

g2.drawImage(imgSource, 0, 0, null);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, alpha));
g2.setColor(color);

g2.fillRect(0, 0, imgSource.getWidth(), imgSource.getHeight());
g2.dispose();

return imgMask;

}

public static void applyQualityRenderingHints(Graphics2D g2d) {
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
}

}

基本上,它的作用是绘制一个“蒙版”图像,在按钮顶部以突出显示的颜色着色。记住这一点很重要。颜色的 Alpha 值越高,您看到文本的可能性就越小。

我没有在 Windows 上测试过这个,所以我不能保证结果。

内容填充是由外观委托(delegate)执行的,通常会忽略类的颜色属性(是的,我知道,太棒了),所以如果你想尝试做一些更健壮的事情,你'需要定义自己的外观委托(delegate)并接管绘画过程,这不是简单的任务。

当然,您可以省去外观委托(delegate)使用的内容填充和边框并绘制您自己的(覆盖 paintComponent 方法),但这不会利用外观和感觉设置,所以这是您需要决定的平衡行为

关于java - 使用 setBackground() 设置 JButton 边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com