简体   繁体   中英

java How to access textField after added to ArrayList

I have a button that gets an number from a text field then creates that number of textFields. It adds the text Fields to an ArrayList. I then create another button. This button needs to get the new value for each test field and add it to an ArrayList. How can I access the new values in each text field. Any help is appreciated. My code is bellow.

public class BallonBustSetupFrame {
    static int numberOfBallons;
    JFrame setup;
    JLayeredPane lp;
    int setBoundsX = 120;
    int setBoundsY = 220;
    ArrayList<String> prizeList = new ArrayList();
    String prizeString;
    JTextField prizeTextBox;


    public BallonBustSetupFrame (){
        setup = new JFrame("Setup");
        lp = new JLayeredPane();
        setup.getContentPane().add(lp);

        setup.setDefaultCloseOperation(EXIT_ON_CLOSE);

        ImageIcon splashPic = new ImageIcon("splash3.jpg");
        JLabel label = new JLabel(splashPic);
        label.setVerticalAlignment(SwingConstants.TOP);
        label.setOpaque(true);
        label.setBounds(0,0,600,600);

        JLabel numberOfBallonsLabel = new JLabel("Number of Ballons:");
        numberOfBallonsLabel.setFont(new Font("Arial", Font.BOLD, 14));
        numberOfBallonsLabel.setForeground(Color.white);
        numberOfBallonsLabel.setBounds(120, 150, 140, 50);

        JTextField numberOfBallonsTextBox = new JTextField(50);
        numberOfBallonsTextBox.setBounds(260, 160, 50, 30);
        numberOfBallonsTextBox.setFont(new Font("Arial", Font.BOLD, 14));
        numberOfBallonsTextBox.setText("10");

        JLabel numberOfBallonsNoteLable = new JLabel("(Number of Ballons must be between 2 and 15)");
        numberOfBallonsNoteLable.setFont(new Font("Arial", Font.ITALIC, 10));
        numberOfBallonsNoteLable.setForeground(Color.white);
        numberOfBallonsNoteLable.setBounds(120, 180, 250, 50);

        JButton okButton = new JButton("Quit");
        okButton.setBounds(400,550,95, 0x1e);
        okButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });

        JButton startGameButton = new JButton("Start");
        startGameButton.setBounds(120,550,95, 0x1e);
        startGameButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){ 
                //Need to access the textFields values


//                BallonBustFrame startGame = new BallonBustFrame();
//                closeFrame();
            }
        });

        JButton numberOfBallonsButton = new JButton("Set");
        numberOfBallonsButton.setBounds(360,160,95, 0x1e);
        numberOfBallonsButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){ 
                numberOfBallons = Integer.parseInt(numberOfBallonsTextBox.getText());
                System.out.println(numberOfBallons);
                lp.remove(numberOfBallonsButton);
                //creates the textFields
                for(int i = 0; i < numberOfBallons; i++ ){
                    createPrizePanels();
                    setBoundsX = setBoundsX +125;
                    if(setBoundsX > 450){
                        setBoundsX = 120;
                        setBoundsY = setBoundsY + 65;
                    }
                }
                lp.add(startGameButton);
            }
        });

        lp.add(okButton);
        lp.add(numberOfBallonsButton);
        lp.add(numberOfBallonsLabel);
        lp.add(numberOfBallonsTextBox);
        lp.add(numberOfBallonsNoteLable);
        lp.add(label);
        setup.setSize(620, 650);
        setup.setVisible(true);
    }   

    public int getNumberOfBallons() {
        return numberOfBallons;
    }

    public void createPrizePanels(){
        JLabel prizePanel = new JLabel("Enter Prize Here", SwingConstants.CENTER);
        prizePanel.setVerticalAlignment(SwingConstants.TOP);
        prizePanel.setFont(new Font("Arial", Font.BOLD, 14));
        prizePanel.setForeground(Color.BLUE);
        Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
        prizePanel.setBorder(border);
        prizePanel.setOpaque(true);
        prizePanel.setBackground(Color.LIGHT_GRAY);
        prizePanel.setBounds(setBoundsX, setBoundsY, 120, 60);

        prizeTextBox = new JTextField(50);
        prizeTextBox.setBounds(setBoundsX + 5, setBoundsY + 20, 110, 30);
        prizeTextBox.setFont(new Font("Arial", Font.BOLD, 12));
        prizeTextBox.setOpaque(true);
        prizeTextBox.setBackground(Color.WHITE);
        prizeTextBox.setForeground(Color.BLACK);
        prizeTextBox.setText("No Prize");
        prizeTextBox.setHorizontalAlignment(JTextField.CENTER);

        lp.add(prizePanel);
        lp.add(prizeTextBox); 

    }

    public void closeFrame(){
        setup.dispose();
    }

}

You might need to get all components to identify which is of type JTextField and iterate:

Arrays.asList(lp.getComponents()).forEach(component -> {
   if (component instanceof JTextField) {
      // System.out.println(((JTextField) component).getText());
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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