0

What I am trying to do is add two X by 2 grid panels in the first row of a 2x2 grid content panel, leaving the bottom row of the content panel blank.

To populate the cells on the top row I want to use a function which uses a loop to generate a text field and a slider. the text field calling it's input from textList[n].

So this breaks down into two primary questions.

If I have a function:

public static void makeTop(String textName) {
    JTextField textBox = new JTextField(textName);
    textBox.setPreferredSize(new Dimension(100,50));
    textBox.setHorizontalAlignment(JTextField.CENTER);
    textBox.setEditable(false);

    SpinnerNumberModel numSpinner = new SpinnerNumberModel(10,0,100,1);
    JSpinner spinner = new JSpinner(numSpinner);
    spinner.setPreferredSize(new Dimension(100,50));
}

And a frame w/ panel:

public static void main(String[] args) {
    JFrame frame = new JFrame("Frame");
    frame.getContentPane().setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel cPane = new JPanel((new GridLayout(2,2)));

    frame.add(cPane, BorderLayout.CENTER);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setVisible(true);
}

How could I add the text field and spinner created in makeTop to cPane?

cPane.add() doesn't like function calls, and making cPane public didn't seem to help when trying to add the content in makeTop().

Secondly, let's say makeTop is called as follows, with N being arbitrary and textList[] being populated with Strings:

for(i=N;i>0;i--){
    makeTop(textList[i]);
}

How could I get the text fields and sliders to be unique instances when creating them in such a way?

4

1 に答える 1