Вопрос не дублирует другой вопрос, потому что этот вопрос относится к линии границы для одной стороны
Да, это дубликат, потому что API показывает, как использовать MatteBorder
с Icon
с одной стороны. Итак, все, что вам нужно сделать, это создать значок «Пунктирная линия». См. Пример, опубликованный в моем коде ниже.
Или второй вариант, не указан в проводке, является использование CompoundBorder
, чтобы создать эффект, который вы хотите:
Border empty = BorderFactory.createEmptyBorder(0, -1, -1, -1);
Border dashed = BorderFactory.createDashedBorder(null, 5, 5);
Border compound = new CompoundBorder(empty, dashed);
Устанавливая EmptyBorder
измерения -1 установить комбинированное измерение- , так что DashedBorder
только окрашивается с одной стороны.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class DashedBorder extends JPanel
{
public DashedBorder()
{
// Use an Icon in the MatteBorder
JLabel label1 = new JLabel("Label Using a Matte Border");
add(label1);
Icon icon = new DashedLineIcon(Color.BLACK, 5, 1, 5, 0);
Border matte = BorderFactory.createMatteBorder(1, 0, 0, 0, icon);
label1.setBorder(matte);
System.out.println(matte.getBorderInsets(label1));
add(Box.createHorizontalStrut(100));
// Create a CompoundBorder using the DashedBorder
JLabel label2 = new JLabel("Label Using a Dashed Border");
add(label2);
Border dashed = BorderFactory.createDashedBorder(null, 5, 5);
Border empty = BorderFactory.createEmptyBorder(1, -1, -1, -1);
Border compound = new CompoundBorder(empty, dashed);
label2.setBorder(compound);
System.out.println(compound.getBorderInsets(label2));
}
static public class DashedLineIcon implements Icon
{
private Color color;
private int dashWidth;
private int dashHeight;
private int emptyWidth;
private int emptyHeight;
public DashedLineIcon(Color color, int dashWidth, int dashHeight, int emptyWidth, int emptyHeight)
{
this.color = color;
this.dashWidth = dashWidth;
this.dashHeight = dashHeight;
this.emptyWidth = emptyWidth;
this.emptyHeight = emptyHeight;
}
public int getIconWidth()
{
return dashWidth + emptyWidth;
}
public int getIconHeight()
{
return dashHeight + emptyHeight;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, dashWidth, dashHeight);
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Dashed Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DashedBorder());
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
[This] (http://stackoverflow.com/a/2174361/3377857) следует помочь! –
Возможный дубликат [Возможно ли иметь границу качания java только на верхней стороне?] (Http://stackoverflow.com/questions/2174319/is-it-possible-to-have-a-java-swing- border-only-on-the-top-side) –
Проводили ли вы какие-либо исследования для этого? Просмотрите документы класса BorderFacotry и посмотрите, получаете ли вы что-нибудь. http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html – CaffeineToCode