编程语言
首页 > 编程语言> > java-如何获取彩色线条以区分挥杆中的文本字段数组

java-如何获取彩色线条以区分挥杆中的文本字段数组

作者:互联网

我开发了一个框架,在该框架上使用GridBagLayout来排列12X12的文本字段.即框架上共有144个文本字段.现在,我想用每三列三行之后的彩色线来区分这些文本字段,如下图所示.我尝试了很多方法,但是找不到解决方案.请提出建议.以下是我的代码的一部分.提前致谢.

1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12 
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
-------------------------------    
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
--------------------------------    |
1 2 3 | 4 5 6................
. .
. .
.

请在图表中将每个数字视为一个文本字段.

JTextField jt[][]=new JTextField[12][12];


for(int i=0;i<jt.length;i++)
        {
            for(int j=0;j<jt.length;j++)
            {

                jt[i][j] = new JTextField(1);


                constraints.gridx=j;
                consraints.gridy=i;
                gridbag.setConstraints(jt[i][j],cons);
                c.add(jt[i][j]);
                                jt[i][j].setHorizontalAlignment(JTextField.CENTER);
                jt[i][j].setFont(new Font("TimesNewRoman",Font.BOLD,14));
                jt[i][j].setDocument(new JTextFieldLimit(2));
            }
        }

解决方法:

您可以给我们一个JSeparator,或者将每组3×3字段分成单独的窗格,然后使用LineBorder.

只要正确设置了字段,您就应该能够使复合面板/ LineBorder正常工作

UPDATE

抱歉,应该是MatteBorder:P

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;

for (int row = 0; row < 4; row++) {

    gbc.gridx = 0;

    add(buildGroup(0, 0, 1, 1), gbc);
    gbc.gridx++;
    add(buildGroup(0, 0, 1, 1), gbc);
    gbc.gridx++;
    add(buildGroup(0, 0, 1, 1), gbc);
    gbc.gridx++;
    add(buildGroup(0, 0, 1, 0), gbc);
    gbc.gridy++;

}

public JPanel buildGroup(int top, int left, int bottom, int right) {

    JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(new MatteBorder(top, left, bottom, right, Color.RED));
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {

            JTextField field = new JTextField(8);
            gbc.gridx = col;
            gbc.gridy = row;

            panel.add(field, gbc);

        }
    }

    return panel;

}

现在,显然,您需要弄清楚如何对字段进行播种,但是基本上,我只是传递要使用的字段(例如2D数组).

或带分隔符:P

for (int row = 0; row < 9; row++) {

    gbc.gridwidth = 1;
    gbc.weightx = 0;

    int verSplit = 0;

    for (int col = 0; col < 12; col++) {

        gbc.gridx++;

        add(new JTextField(8), gbc);

        verSplit++;
        if (verSplit == 3) {

            verSplit = 0;

            gbc.gridx++;

            if (horSplit % 3 == 0) {

            gbc.gridheight = 3;
            gbc.fill = GridBagConstraints.VERTICAL;
            add(new JSeparator(JSeparator.VERTICAL), gbc);

            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridheight = 1;

            }

        }

    }

    horSplit++;

    gbc.gridx = 0;

    if (horSplit == 3) {

        horSplit = 0;
        gbc.gridy++;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1;
        add(new JSeparator(JSeparator.HORIZONTAL), gbc);

    }

    gbc.gridy++;

}

或同一主题的变体

现场管理更新

// Build the array of fields, I used a col by row matrix
JTextField fields[][] = new JTextField[12][12];
for (int col = 0; col < 12; col++) {

    for (int row = 0; row < 12; row++) {

        fields[col][row] = new JTextField(col + "x" + row, 8);

    }

}

// Build the groups...
for (int row = 0; row < 12; row++) {

    gbc.gridx = 0;

    int col = 0;

    add(buildGroup(fields, col, row, 0, 0, 1, 1), gbc);

    col += 3;
    gbc.gridx++;
    add(buildGroup(fields, col, row, 0, 0, 1, 1), gbc);

    col += 3;
    gbc.gridx++;
    add(buildGroup(fields, col, row, 0, 0, 1, 1), gbc);

    col += 3;
    gbc.gridx++;
    add(buildGroup(fields, col, row, 0, 0, 1, 0), gbc);

    gbc.gridy++;
    row += 2; // This is important, don't miss this ;)

}

public JPanel buildGroup(JTextField[][] fields, int startCol, int startRow, int top, int left, int bottom, int right) {

    JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(new MatteBorder(top, left, bottom, right, Color.RED));
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {

            // Get the field to use for this cell 
            JTextField field = fields[col + startCol][row + startRow];
            gbc.gridx = col;
            gbc.gridy = row;

            panel.add(field, gbc);

        }
    }

    return panel;

}

标签:java,swing,line,jtextfield,jseparator
来源: https://codeday.me/bug/20191011/1894795.html