java 添加到ArrayList后如何访问textField

标签 java

我有一个按钮,可以从文本字段获取数字,然后创建该数量的文本字段。它将文本字段添加到 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());
   }
});

关于java 添加到ArrayList后如何访问textField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58374625/

相关文章:

java - 从 java 变量调用类方法

java - 找不到路径时如何返回状态码 400

java - 当 Object 是 List 时替换 List<Object> 中的对象? java

java - 如何将我的词典添加到斯坦福标记器中?

java - 如何为Spark Java提供动态数据库配置凭据?

java - 如何在 JavaScript 中读取 XML 服务器响应?

java - 如何从 Java 运行 Windows 命令

java - Singleton 中的 Volley requestQueue 返回 null

java - Java 中的中缀到前缀数学表达式转换器

java - 尽管有参数,仍需要 : no arguments, 吗?