如何在JTextField中排列2D数组的值

时间:2019-05-06 15:54:32

标签: java swing

我没有在档案中找到任何答案。
我在jtextfield上设置5x5数字序列时遇到问题。 这是我的代码

private void btnperformAction(java.awt.event.ActionEvent evt) {                                                           
        int [][] boardOne = defineBoard(); //initializes matrix   
        fillBoard(boardOne); //adds integers values to the matrix
        int i,j;

        for(i = 0; i < boardOne.length; i++){
            for(j = 0; j < boardOne[i].length; j++){
                txtField.setText(txtField.getText() + boardOne[i][j]); 
         }
        }

这就是我需要显示输出的方式。

    5   16  36  52  70  
    8   26  35  60  73  
    12  23  -1  51  74  
    3   27  34  59  68  
    14  30  47  64  80  

我在jtextfield上得到的是这个

5
16
36
52
70
8
26
35
60
73
12
23
-1
51
74
3
27
34
59
68
14
30
47
64
80

如何以5x5格式格式化? 任何帮助是极大的赞赏。

2 个答案:

答案 0 :(得分:1)

您需要使用JTextArea并在每行末尾添加换行符。如果使用的是JTextField,则它不支持多行,只能在一行上。 将代码粘贴到任何Java文件中,然后执行main方法。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Scratch {
    public static void main(String[] args) {
        JFrame frm = new JFrame();
        JTextArea txtArea = new JTextArea();
        txtArea.setPreferredSize(new Dimension(500,500));
        JButton btn = new JButton("DO ARRANGE");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int[][] boardOne = new int[][]{
                        {5,   16,  36,  52,  70},
                        {8,   26,  35,  60,  73},
                        {12,  23,  -1,  51,  74},
                        {3,   27,  34,  59,  68},
                        {14,  30,  47,  64,  80}
                };
                int i, j;
                StringBuilder sb = new StringBuilder();
                for (i = 0; i < boardOne.length; i++) {
                    for (j = 0; j < boardOne[i].length; j++) {
                        sb.append(boardOne[i][j]);
                        if (j < boardOne[i].length - 1) {
                            sb.append(" ");
                        }
                    }
                    sb.append("\r\n");
                }
                txtArea.setText(sb.toString());

            }
        });
        frm.getContentPane().setLayout(new BorderLayout());
        frm.getContentPane().add(btn, BorderLayout.NORTH);
        frm.getContentPane().add(txtArea, BorderLayout.CENTER);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
}

答案 1 :(得分:1)

如您在前面的4x4矩阵问题中所述,您可以按如下方式使用StringBuilder

StringBuilder builder = new StringBuilder();
for(int i = 0; i < boardOne.length; i++){
    for(int j = 0; j < boardOne[i].length; j++){
        builder.append(boardOne[i][j]);
        builder.append(" ");
    }
    builder.append("\n");
}

txtField.setText(builder.toString());

请注意,这只是一个用空格分隔的简单String。正如您所问的那样,它不会使间距均匀,因此看起来像一个完美的网格。对于这种事情,您将需要根据位数计算出确切的空格填充。因此,如果那对您很重要,请选择一个JTable。

相关问题