繁体   English   中英

java 添加到 ArrayList 后如何访问 textField

[英]java How to access textField after added to ArrayList

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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