JPanel

时间:2015-04-22 13:34:19

标签: java swing jpanel border

是否有方法只将虚线边框从一侧添加到Java SwingJPanelJLabel组件?

问题不是another question的重复,因为该问题涉及一方的边界线。但我需要虚线边框。

2 个答案:

答案 0 :(得分:3)

  

问题不是另一个问题的重复,因为该问题涉及一方的行边界

是的,它是重复的,因为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,您可以将组合维度设置为0,因此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();
            }
        });
    }
}

答案 1 :(得分:-1)

我也提到了这个解决方案,虽然它有点复杂:

var bulk = Person.collection.initializeOrderedBulkOp();
    bulk.find({'_id': id}).update({$set: {status: 'inactive'}});
    bulk.execute(function (error) {
         callback();                   
    });

在这里,我使用private void drawBorder(Graphics g){ int x = this.getWidth(); float dash[] = {1.0f}; Graphics2D g2; BasicStroke stroke; g2 = (Graphics2D)g; stroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dash, 0.0f); g2.setStroke(stroke); g2.setColor((Color) new Color(120, 120, 120)); g2.draw(new Line2D.Double(0, 0, x, 0)); } panel = new JPanel(){ @Override public void paintComponent(Graphics g){ drawBorder(g); } }; 方法绘制了顶行,并使用Graphics2D类创建了实际为点的短划线,因为密度数组BasicStroke告诉它有{{1}的空格破折号之间的短划线长度。