【问题标题】:Making a button write to one JTextField in an Array使按钮写入数组中的一个 JTextField
【发布时间】:2014-10-16 15:48:40
【问题描述】:

我正在开发 TextTwist java 实现,目前正在开发 GUI。我想知道你们中的一些 Swing 天才是否可以帮助我。我正在尝试做一个 GUI,每当单击按钮时,该按钮的文本都会写入其下方的第一个空 textField 中。我很难思考如何去做。到目前为止,我已经尝试过使第一个按钮单击填充所有 TextFields。

任何正确方向的帮助或指示都会很棒。

        private void makeButtonLayout() {
    this.charArray = new char[6];
    ArrayList<Character> charArrayList = new ArrayList<Character>();

        this.charArray = this.randomString.toCharArray();
        for(char tempCharacter : this.charArray){
            charArrayList.add(tempCharacter);
        }

        for (int i = 0; i< 6; i++){

            JButton letterButton = new JButton();
            Character buttonCharacter = charArrayList.get(i);
            charArrayList.remove(i);

            String letterString = buttonCharacter.toString();
            letterButton.setText(letterString);
            this.letterButtonsArray.add(letterButton);
        }

        for (final JButton currentButton : this.letterButtonsArray){
            this.buttonPanel.add(currentButton);    
            currentButton.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    int i = 0;
                    currentButton.setVisible(false);
                    JTextField temporaryTextField = new JTextField();
                    String temporaryString = currentButton.getText();

                    temporaryTextField.setText(temporaryString);

                    if(textFieldArray.get(i).getText().isEmpty()){
                        textFieldArray.get(i).setText(temporaryString);
                        return;
                    }else{
                        i++;
                        return;
                    }

                }

            });
        }
    }

【问题讨论】:

    标签: java swing jbutton jtextfield


    【解决方案1】:

    我觉得你的方法真的很奇怪。您混合了初始化代码和执行代码。这样做:

            for (final JButton currentButton : this.letterButtonsArray){
                this.buttonPanel.add(currentButton);    
                currentButton.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        currentButton.setVisible(false);
    
                        String temporaryString = currentButton.getText();
    
                        for(int i = 0; i < textFieldArray.size(); i++)
                          JTextField elem = textFieldArray.get(i);
                          if(elem.getText().equals("")){ // or if you don't want spaces do: elem.getText().trim().equals("");
                              elem.setText(temporaryString);
                              break;
                          }
                        }
    
                    }
    
                });
            }
    

    【讨论】:

    • 我更改了格式,但仍然遇到同样的问题。当用户单击第一个按钮时,该方法将该按钮的文本放入所有空文本字段中,而不仅仅是第一个空文本字段。
    • 我围绕该方法调整了一些其他代码;并且通过使用“.trim()”变体能够找到解决方案。非常感谢!
    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多