使用 JTextFields 或 JTextAreas 创建矩阵

发布于 2024-10-05 12:07:04 字数 171 浏览 2 评论 0原文

我必须制作一个程序,该程序必须能够以 Jtextfields 或 Jtextareas 的形式显示矩阵,以便用户可以在其中写入(每个 Jtextfield 的多个矩阵)。 问题是我不知道如何使用用户指定的 Jtextfields 的大小和数量(每次都不同)创建自定义 JPanel。 我已经用谷歌搜索了这个问题,但没有结果。

I have to do make a program that has to be able to show a matrix in form of Jtextfields or Jtextareas, so that the user can write in them(a number of the matrix for each Jtextfield).
The problem is that I have no idea how to create a custom JPanel with the size and quantity of Jtextfields that the user specifies(a different each time).
I have already googled the question, to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧话新听 2024-10-12 12:07:04

看一下 GridLayout。将网格与几个参数(行数和列数)放在一起非常简单。从 JavaDoc 中解释:

   public static void main (String[] args) {


         JPanel panel = new JPanel();     
         panel.setLayout(new GridLayout(3,2));
         panel.add(new JTextField("1"));
         panel.add(new JTextField("2"));
         panel.add(new JTextField("3"));
         panel.add(new JTextField("4"));
         panel.add(new JTextField("5"));
         panel.add(new JTextField("6"));

将导致 JTextFields 的 3 行 x 2 列网格

编辑:

这里还有更多内容,在名为 Demo 的类中:

 public class Demo {

public static void main(final String[] args) {
    if (args.length < 2) {
        System.out.print("please enter row and col on commandline");
    }
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            new Demo(Integer.parseInt(args[0]), Integer.parseInt(args[1]));

        }
    });

}


public Demo(int colCnt, int rowCnt) {
    JFrame frame = new JFrame("Demo");
    frame.setSize(600, 600);
    JPanel panel = new JPanel();     
    panel.setLayout(new GridLayout(colCnt,rowCnt));

    for (int i =0; i < rowCnt*colCnt; i++) {
       panel.add(new JTextField(""+i));
    }

    frame.setContentPane(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

Take a look at GridLayout. It is pretty simple to put together a grid together with a couple params (row and col count). To paraphrase from the JavaDoc:

   public static void main (String[] args) {


         JPanel panel = new JPanel();     
         panel.setLayout(new GridLayout(3,2));
         panel.add(new JTextField("1"));
         panel.add(new JTextField("2"));
         panel.add(new JTextField("3"));
         panel.add(new JTextField("4"));
         panel.add(new JTextField("5"));
         panel.add(new JTextField("6"));

would result in a 3 row by 2 col grid of JTextFields

EDIT:

here is some more, in a class named Demo:

 public class Demo {

public static void main(final String[] args) {
    if (args.length < 2) {
        System.out.print("please enter row and col on commandline");
    }
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            new Demo(Integer.parseInt(args[0]), Integer.parseInt(args[1]));

        }
    });

}


public Demo(int colCnt, int rowCnt) {
    JFrame frame = new JFrame("Demo");
    frame.setSize(600, 600);
    JPanel panel = new JPanel();     
    panel.setLayout(new GridLayout(colCnt,rowCnt));

    for (int i =0; i < rowCnt*colCnt; i++) {
       panel.add(new JTextField(""+i));
    }

    frame.setContentPane(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}
时光无声 2024-10-12 12:07:04

首先,我将构建一个文本字段\区域的二维数组。当您获得用户输入时,您可以初始化数组并“新建”所有小部件。将它们全部添加到父面板\框架后,您可能需要根据已用尺寸进行一些计算并调整顶层窗口的大小。除此之外,正如已经建议的那样,GridLayout 对于直接父组件来说是一个不错的选择。

希望这有帮助。

First of all, I would build a 2d array of the text fields\areas. When you get the user input you can then initialize the arrays and "new up" all of the widgets. After adding all of them to the parent panel\frame you may have to do some calculation based on the used up size and resize your top level window. Aside from that, as already suggested, GridLayout will be a good choice for the direct parent component.

Hope this helps.

深海里的那抹蓝 2024-10-12 12:07:04

要在运行时更改外观,您所需要做的就是用新组件替换内容。
因此,根据您获取输入的方式,您可以用输入替换 akf 答案中给出的数字。

panel.setLayout(new GridLayout( rows, columns));

然后为了显示新面板,您可以将其添加到 JFrame 中并进行

add( panel );

分配,但是可能类似于子类化 JPanel 以显示矩阵。我从你的措辞中得到了这种印象。那么它是一个完全不同的解决方案。

如果您的唯一目标是实现一个可以编辑值的矩阵,那么 JTable 是迄今为止最简单的。将以下内容放入 JFrame

    DefaultTableModel data = new DefaultTableModel(3 , 3);// rows, cols
    JTable table = new JTable(data);
    add(table);
    pack();
    setVisible(true);

这也将简化使用 data.setRowCount( newValue ) 修改矩阵的高度和宽度。

To change the appearance at runtime, all you need to do is replace the content with new components.
So depending on how you get your input, you replace the numbers given in akf's answer with the input.

panel.setLayout(new GridLayout( rows, columns));

and then to show the new panel you add it in the JFrame with

add( panel );

Your assignment however might be something along the lines of subclassing a JPanel to show a matrix. I get that impression in your wording. Then its a whole different solution.

If your only goal is to implement a matrix where you can edit values a JTable is by far the simplest. Put the following in a JFrame

    DefaultTableModel data = new DefaultTableModel(3 , 3);// rows, cols
    JTable table = new JTable(data);
    add(table);
    pack();
    setVisible(true);

This would also simplify modifying the height and width of the matrix by using data.setRowCount( newValue ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文