我有一个按钮,可从文本字段获取数字,然后创建该数量的textFields。它将文本字段添加到ArrayList。然后,我创建另一个按钮。该按钮需要获取每个测试字段的新值,并将其添加到ArrayList中。如何在每个文本字段中访问新值。任何帮助表示赞赏。我的代码在下面。

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();
    }

}

最佳答案

您可能需要获取所有组件以标识哪个类型为JTextField并进行迭代:

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

07-24 09:23