如何获得彩色线来区分文本字段数组 [英] how to get colored line to differentiate text field array in swing

查看:115
本文介绍了如何获得彩色线来区分文本字段数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have developed one frame on which I used GridBagLayout to arrange textfields of 12X12. i.e., total 144 textfields on frame. Now I want to differentiate these text fields with colored line after each 3 columns and three rows as shown in the following diagram. I tried in many ways, but I couldn't find the solution. Please suggest. Below is the some part of my code. Thanks in advance.

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................
. .
. .
.

请将每个数字视为图表中的一个文本字段。

Please consider the each number as one textfield in diagram.

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 或打破每个一组3x3字段进入单独的窗格并使用 LineBorder

You could us a JSeparator or, break each group of 3x3 fields into there own separate panes and use a LineBorder.

只要您正确设置了字段,就可以获得复合面板/ LineBorder 上班

So long as you've setup you fields properly, you should be able to get the compound panels/LineBorder to work

更新

抱歉,它应该是 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;

}

现在,显然,你需要弄清楚你是怎么做的d播种你的字段,但基本上,我只是传入你想要使用的字段(例如2D数组)。

Now, obviously, you need to figure out how you'd seed your fields, but basically, I'd just pass in the fields you want to be used (such as 2D array for example).

或者使用分隔符: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;

}

这篇关于如何获得彩色线来区分文本字段数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆