gpt4 book ai didi

java - 设置 JPanel 边框上的描边

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

我在设置边框厚度时遇到问题。我想要一个带有圆虚线边框的 JPanel 。我可以通过覆盖 paintComponent 来将其设置为圆形。但是,当我设置笔划以使边框变粗并虚线时,它不起作用。我使用 setStroke() 方法。我的代码如下

private void loadTopPane() {

JPanel topSection = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension arcs = new Dimension(15, 15); // Border corners arcs
// {width,height},
// change this to
// whatever you want
int width = getWidth();
int height = getHeight();
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

float dash1[] = { 10.0f };
final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
dash1, 0.0f);

// Draws the rounded panel with borders.
graphics.setColor(getBackground());
graphics.fillRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
// background
graphics.setColor(getForeground());
graphics.drawRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
// border
graphics.setStroke(dashed);

}
};

topSection.setLayout(null);
topSection.setSize(1150, 175);
topSection.setBackground(new Color(222, 225, 226));

topSection.setBounds(25, 13, topSection.getPreferredSize().width, topSection.getPreferredSize().height);

topSection.add(new JLabel("TESTING"));
topSection.setBounds(20, 10, 1180, 180);
frame.add(topSection);

}

所以输出显示了一个带有圆形边框的 JPanel ,但它没有给我一个虚线且更粗的边框。我该如何解决这个问题?

最佳答案

您是否考虑过在绘制边框之前调用setStroke...

Stroked

float dash1[] = {10.0f};
final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
dash1, 0.0f);
graphics.setStroke(dashed);

// Draws the rounded panel with borders.
graphics.setColor(getBackground());
graphics.fillRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
// background
graphics.setColor(getForeground());
graphics.drawRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint

关于java - 设置 JPanel 边框上的描边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37234754/

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