简体   繁体   English

如何在Java中动态设置对象?

[英]How to dynamically set an object in java?

I am creating an GUI application in java that uses input from the user and dynamically sets up textfield in my jFrame with default names like jTextField1,jTextField2 etc 我正在用Java创建一个GUI应用程序,该应用程序使用来自用户的输入并使用默认名称(如jTextField1,jTextField2等)动态设置jFrame中的文本字段。

My projects demo is as follows:- 我的项目演示如下:

在此处输入图片说明

1) It accepts number of rows and columns from combo box. 1)它接受组合框中的行数和列数。

2)It should set the number of columns and rows as per the selection of user. 2)应根据用户选择设置列数和行数。 (ie it should dynamically create Jtextfield objects on the JFrame) (即它应该在JFrame上动态创建Jtextfield对象)

Can i dynamically setup these commands inside a looping statement and create the number of textfields i need :- 我可以在循环语句中动态设置这些命令并创建所需的文本字段数量吗?

     private void initComponents() {
    jTextField1 = new javax.swing.JTextField();
            jTextField2 = new javax.swing.JTextField();
}


private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;

Or is there any other methods to do the same. 还是有其他方法可以做到这一点。

Is this Possible in any other languages like javascript or html. 是否可以使用其他任何语言(例如javascript或html)。

Any help is appreciated ! 任何帮助表示赞赏!

Your approach is right. 您的方法是正确的。 You loop through the indexes and create the new components (Java GUI objects) and add them to the container. 您遍历索引并创建新组件(Java GUI对象)并将其添加到容器中。

Remember to either check existing components or remove()/removeAll() them. 记住要检查现有组件或remove()/removeAll()它们。

UPDATE: When you create a Container you usually define a LayoutManager (I am more familiar with GridBagLayout , but XYLayout could be useful for you). 更新:创建容器时,通常会定义一个LayoutManager(我对GridBagLayout更为熟悉,但是XYLayout对您很有用)。 When adding a component to a Container, you have a method that allows an extra object constraints ; 将组件添加到容器时,您可以使用一种方法,该方法允许额外的对象constraints that object is passed to the layoutManager to know where / how to draw the component added. 该对象将传递给layoutManager,以了解在何处/如何绘制添加的组件。

Of course, each different layoutManager uses its own different constraints, so for the GridBagLayout I pass a GridBagConstraints object. 当然,每个不同的layoutManager使用其自己的不同约束,因此对于GridBagLayout我传递了GridBagConstraints对象。 In your example, to build a row, all the fields except the ones would be added by a default GridBagConstraints; 在您的示例中,要构建一行,将使用默认的GridBagConstraints添加除字段之外的所有其他字段; the GridBagConstraints in the last row would have 最后一行中的GridBagConstraints将具有

 GridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;

to notify the layout manager that it is the last item in the row. 通知布局管理器它是该行的最后一项。

You should read about Layout managers. 您应该阅读有关布局管理器的信息。 In your case you should start from using GridLayout. 在您的情况下,您应该从使用GridLayout开始。 Read number of rows and columns, create GridLayout with appropriate parameters. 读取行数和列数,使用适当的参数创建GridLayout。 Then create text fields into the for loop (or 2 nested for loops) and add them to the panel. 然后在for循环中创建文本字段(或2个嵌套的for循环)并将其添加到面板中。

You will see that the number of text fields and their positions are almost OK. 您将看到文本字段的数量及其位置几乎可以。 Now see the problem: the text field sizes will depend on your frame size. 现在看到问题:文本字段的大小将取决于您的框架大小。 To solve this problem you can replace your GridLayout by BoxLayout or GridBagLayout or use nested panel with flow layout or use third party tool like MigLayout . 要解决此问题,您可以将BoxLayoutGridBagLayout替换为GridLayout ,或者将嵌套面板与流布局一起使用,或者使用第三方工具(例如MigLayout

First of all: You can use arrays for your text fields: 首先:您可以将数组用于文本字段:

JTextField[][] matrix1TextFields, matrix2TextFields, matrix3TextFields;
private void refreshMatrixComponents(int r, int c) {
  matrix1TextFields = new JTextField[r][c];
  matrix2TextFields = new JTextField[r][c];
  matrix3TextFields = new JTextField[r][c];
  for (int row = 0; row < r; row++) {
    for (int col = 0; col < c; col++) {
      matrix1TextFields[row][col] = new JTextField();
      matrix2TextFields[row][col] = new JTextField();
      matrix3TextFields[row][col] = new JTextField();
    }
  }
  placeMatrixTextField();  
}

For the layout manager, there are a lot of options. 对于布局管理器,有很多选择。 I would either recommend GridLayout (very easy to use) or GridBagLayout (more difficult to use, but more flexible) or something 3rd party like MigLayout (you will have to dive into the syntax a bit, but I think it's totally worth the effort). 我会推荐GridLayout (非常易于使用)或GridBagLayout (更难使用,但更灵活),或者像MigLayout这样的MigLayout (您将不得不深入语法,但我认为这完全值得付出努力) 。 I also recommend creating a JPanel for every matrix and put all the text fields in there. 我还建议为每个矩阵创建一个JPanel并将所有文本字段放在其中。 Then place those JPanels + the rest of the components: 然后放置那些JPanels +其余组件:

JPanel matrix1Panel, matrix2Panel, matrix3Panel;
private void placeMatrixTextField() {
  int r = matrix1TextFields.length, c = matrixTextFields[0].length;
  matrix1Panel = new JPanel();
  matrix2Panel = new JPanel();
  matrix3Panel = new JPanel();
  matrix1Panel.setLayout(new GridLayout(r, c));
  matrix2Panel.setLayout(new GridLayout(r, c));
  matrix3Panel.setLayout(new GridLayout(r, c));
  for (int row = 0; row < r; row++) {
    for (int col = 0; col < c; col++) {
      matrix1Panel.add(matrix1TextFields[row][col]);
      matrix2Panel.add(matrix2TextFields[row][col]);
      matrix3Panel.add(matrix3TextFields[row][col]);
    }
  }
  // now place your panels
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM