문제

I am having problems with this thing: can I, in some way, add a dashed (or dotted, no matter) border to a JPanel?

I searched SO questions but seems that no one asked this before.

I'm wondering if is there any class to use. actually I am using:

myPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

Obviously this is a standard class that give only few standard borders, no one is useful for me.

도움이 되었습니까?

해결책

다른 팁

Starting from Java 7, you can use BorderFactory.createDashedBorder(Paint).

Prior to Java 7, you have to define this border yourself. Then you can use this self-written border:

private class DashedBorder extends AbstractBorder {
    @Override
    public void paintBorder(Component comp, Graphics g, int x, int y, int w, int h) {
        Graphics2D gg = (Graphics2D) g;
        gg.setColor(Color.GRAY);
        gg.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{1}, 0));
        gg.drawRect(x, y, w - 1, h - 1);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top