如何修改jbutton的边框颜色?我想得到这样的东西:但我可以´如果不修改颜色,则边框为黑色:如果我尝试添加线条边框或任何其他边框,我无法删除内边框:
v64noz0r1#
我试过下面的一个例子,一切似乎都很好,除非你正在做一些不同的事情:
import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Insets; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; public class TestApp { public TestApp() { createAndShowGui(); } public static void main(String[] args) { SwingUtilities.invokeLater(TestApp::new); } private void createAndShowGui() { JFrame frame = new JFrame("TestApp"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setBorder(new EmptyBorder(20, 20, 20, 20)); panel.setBackground(Color.WHITE); JButton button = new JButton("Primary"); button.setOpaque(false); button.setBackground(null); button.setFocusPainted(false); button.setForeground(new Color(69, 143, 253)); RoundedBorder blueLineBorder = new RoundedBorder(new Color(69, 143, 253), 10); Border emptyBorder = BorderFactory.createEmptyBorder(button.getBorder().getBorderInsets(button).top, button.getBorder().getBorderInsets(button).left, button.getBorder().getBorderInsets(button).bottom, button.getBorder().getBorderInsets(button).right); button.setBorder(BorderFactory.createCompoundBorder(blueLineBorder, emptyBorder)); panel.add(button); frame.add(panel); frame.pack(); frame.setVisible(true); } private static class RoundedBorder implements Border { private int radius = 10; private Color color; private RoundedBorder(Color color, int radius) { this.color = color; this.radius = radius; } @Override public Insets getBorderInsets(Component c) { return new Insets(this.radius + 1, this.radius + 1, this.radius + 1, this.radius + 1); } @Override public boolean isBorderOpaque() { return true; } @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { g.setColor(color); g.drawRoundRect(x, y, width - 1, height - 1, radius, radius); } } }
1条答案
按热度按时间v64noz0r1#
我试过下面的一个例子,一切似乎都很好,除非你正在做一些不同的事情: