java - 如何在 jlabel 之间留空格?

标签 java swing jlabel layout-manager null-layout-manager

您好,我正在尝试制作 Java 桌面应用程序,其中我使用多个 jlabel 我想在每个标签之间留出小空间

我怎样才能做到这一点

这是我的代码

public class Second extends javax.swing.JFrame {
JLabel label=new JLabel();
    /**
     * Creates new form Second
     */
    public Second() {
          this.getContentPane().setBackground(new java.awt.Color(255, 140, 0));
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setUndecorated(true);
JButton print= new JButton();
print.setBackground(new java.awt.Color(153, 153, 0));
            print.setOpaque(true);
            print.setBounds(525,1282,130,85);
            print.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
          print.setForeground(new java.awt.Color(255,255,255));
            print.setText("Print");
            this.add(print);

          JButton home= new JButton();
home.setBackground(new java.awt.Color(153, 153, 0));
            home.setOpaque(true);
            home.setBounds(640,1282,130,85);
            home.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
          home.setForeground(new java.awt.Color(255, 255,255 ));
            home.setText("Home");
            this.add(home);

int Height = 134;
int a=100;
             ArrayList<JLabel> label = new ArrayList<JLabel>();

  for(int i=0;i<23;i++){
         JLabel j = new JLabel();
           j.setBackground(new java.awt.Color(255, 140, 255));
            j.setOpaque(true);
            j.setBounds(5,Height,378,50);
            j.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
          j.setForeground(new java.awt.Color(128, 128, 128));
            j.setText("Case                                  Item                         CourtNo   ");
             LineBorder line = new LineBorder(Color.blue, 2, true);

       j.setBorder(line);
            this.add(j);
            label.add(j);


               JLabel j1 = new JLabel();
            j1.setBackground(new java.awt.Color(255, 140, 0));
            j1.setOpaque(true);
            j1.setBounds(390,Height,768,50);
            j1.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
            j1.setForeground(new java.awt.Color(128, 128, 128));
            j1.setText("Case                                 Item                         CourtNo   ");
            this.add(j1);
            label.add(j1);


               Height = Height +50;
               a=a+10;
  }




        initComponents();


    }
  public void paint(Graphics g) {
        super.paint(g);

         Graphics2D g3 = (Graphics2D) g;
      BasicStroke bs = new BasicStroke(2);
    Line2D lin1 = new Line2D.Float(386, 100, 386, 1282);
      Line2D lin = new Line2D.Float(0, 1283, 768, 1283);
      Line2D line3=new Line2D.Float(400,1284,400,1364);
    g3.setStroke(bs);
     g3.setColor(Color.white);
     g3.draw(lin1);
     g3.draw(lin);
     g3.draw(line3);;

  }
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Second().setVisible(true);
            }
        });
    }}

我的新代码

public class Testing1 extends javax.swing.JFrame {


    public Testing1() {

             JFrame frame = new JFrame();
        JPanel panel = createPanel();
        panel.setLocation(100, 100);
        //panel.setLayout(null);
        this.add(panel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        initComponents();
    }


     private JPanel createPanel() {
    jPanel1 = new JPanel(new GridLayout(0, 1, 10, 5));
        EmptyBorder panelBorder = new EmptyBorder(10, 10, 10, 10);
        jPanel1.setBorder(panelBorder);
        EmptyBorder border = new EmptyBorder(5, 20, 5, 20);
        LineBorder line = new LineBorder(Color.blue, 2, true);
        CompoundBorder compound = new CompoundBorder(line, border);
        for (int i = 0; i <12; i++) {
            JLabel label = new JLabel("Label" + i);
            label.setBorder(compound);
           // label.setBounds(13, 100, 100, 50);
           jPanel1.add(label);
        }
        return jPanel1;
    }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 0, 153));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 306, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 243, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 94, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }                      


    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Testing1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}

更新了 提前致谢

最佳答案

"How to give spaces between jlabel?"

setBounds and null layouts: no; 使用 EmptyBorders: YES!;使用 LayoutManagers:是!

"how can i use this can u explain little bit "

使用可以使用默认的FlowLayout包含 JPanel 的(或在本例中设置 FlowLayout 的间隙)

FlowLayout 构造函数:

public FlowLayout(int align,
      int hgap,
      int vgap)

align - the alignment value

hgap - the horizontal gap between components and between the components and the borders of the Container

vgap - the vertical gap between components and between the components and the borders of the Container

例子

JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5);
for (int i = 0; i < 10; i++) {
    panel.add(new JLabel("Label" + i));
}

你可以使用 EmptyBorder

EmptyBorder 构造函数

public EmptyBorder(int top,
       int left,
       int bottom,
       int right)

top - the top inset of the border

left - the left inset of the border

bottom - the bottom inset of the border

right - the right inset of the border

例子

JPanel panel = new JPanel();
for (int i = 0; i < 10; i++) {
    JLabel label = new JLabel("Label" + i);
    EmptyBorder border = new EmptyBorder(5, 20, 5, 20);
    label.setBorder(border);
    panel.add(label);
}

如果您想使用 LineBorder EmptyBorder 作为边距,您可以使用 CompoundBorder

A composite Border class used to compose two Border objects into a single border by nesting an inside Border object within the insets of an outside Border object. For example, this class may be used to add blank margin space to a component with an existing decorative border:

复合边框示例

JPanel panel = new JPanel();
EmptyBorder border = new EmptyBorder(5, 20, 5, 20);
LineBorder line = new LineBorder(Color.blue, 2, true);
CompoundBorder compound = new CompoundBorder(line, border);
for (int i = 0; i < 10; i++) {
    JLabel label = new JLabel("Label" + i);
    label.setBorder(compound);
    panel.add(label);
}

有很多可能性。选择你的口味。重点是不是尝试为所有内容设置大小和位置,并利用布局管理器和边框和间隙来调整大小、间距等。

Laying out Components Within a Container 学习使用不同的布局管理器


使用 CompoundBorderGridLayout(int rows, int cols, int hgap, int vgap) 的完整示例

enter image description here

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class CompoundBorderDemo {

    public CompoundBorderDemo() {
        JFrame frame = new JFrame();
        JPanel panel = createPanel();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createPanel() {
        JPanel panel = new JPanel(new GridLayout(5, 5, 10, 10));
        EmptyBorder panelBorder = new EmptyBorder(10, 10, 10, 10);
        panel.setBorder(panelBorder);
        EmptyBorder border = new EmptyBorder(5, 20, 5, 20);
        LineBorder line = new LineBorder(Color.blue, 2, true);
        CompoundBorder compound = new CompoundBorder(line, border);
        for (int i = 0; i < 25; i++) {
            JLabel label = new JLabel("Label" + i);
            label.setBorder(compound);
            panel.add(label);
        }
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CompoundBorderDemo();
            }
        });
    }
}

关于java - 如何在 jlabel 之间留空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22653592/

相关文章:

java - java无法读取图像

java - 如何让我的代码找出在合理的时间内按下了哪个按钮?

java - JLabel图片错误

java - 如何在 MouseEnter 上为 JLabel 添加下划线

java - 使用LinkedList的迭代器打印值,在java中不断陷入无限循环

java - 如何告诉 jetty 将部分 jar 文件提取到其上下文临时位置

java - 有人有过使用 JODB 进行网络应用程序的经验吗?

java - 重新创建一个 Jpanel

java - 将JFrame ImageIcon发送到Frame的后面

java - 从每行中删除特定文本