Creating arrays of swing components or specifically JTextFields and giving each of them different properties in java

試著忘記壹切 提交于 2019-12-25 18:48:11

问题


So I'm trying to create a large number of JTextFields and use them as nodes in a binary tree program I'm trying to make. Is there a way to create an array of JTextFields, something like:

JTextField nodes[]=new JTextField[30];

If so, how do I define or give every one of them different properties or the property I want them to have?

Thanks!


回答1:


JTextField[] fields = new JTextField[30];
for(int i = 0; i<fields.length; i++){
    fields = new JTextField();
}

//then you can access them and modify them as normal.
fields[3].setColumns(5);
fields[3].setText("apples");



回答2:


You can do this:

JTextField[] jtfs = new JTextFields[] {
    // Define as many textfields as you want
    new JTextField(), // no args
    new JTextField("Initial text"), // text
    new JTextField(20), // 20 columns
    new JTextField("Initial text", 20), // text + columns number
    ...
}



回答3:


You can loop through all your JTextFields, taking this as an example you could add a switch inside the for loop to put inside each case the properties of each JTextField.

From your comments on other answers I saw you wanted field[3] (or any name you want) to change the columns number and have "Apple" written inside it.

Next time you should provide a Runnable Example as the one I'm providing so we can copy-paste it and give you a better approach. And (optionally) an image of what you want.

Here I used only 5 JTextFields to make an example, you can have the 30 you want, try it :)

The code below produces this output:

import java.awt.*;
import javax.swing.*;
public class ArrayOfJTextField {
    JFrame frame;
    JPanel pane;
    JTextField fields[];
    ArrayOfJTextField () {
        frame = new JFrame("Frame Test");
        fields = new JTextField[5];
        Container pane2 = frame.getContentPane();
        pane2.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));

        for(int i = 0; i < fields.length; i++) {
            pane = new JPanel(new FlowLayout());
            fields[i] = new JTextField ("This is field " + (i + 1)); //You missed to do this and that's why you were getting a NPE
            if (i == 3) {
                fields[i].setColumns(10);
                fields[i].setText("Apple");
            } else {
                fields[i].setColumns(5);
            }
            pane.add(new JLabel("Label " + (i +1)));
            pane.add(fields[i]);
            frame.add(pane);
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main (String args[]) {
        new ArrayOfJTextField();
    }
}


来源:https://stackoverflow.com/questions/34611167/creating-arrays-of-swing-components-or-specifically-jtextfields-and-giving-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!