【问题标题】:Adding a string to an ArrayList of strings in the JFrame after the button is pressed按下按钮后,将字符串添加到 JFrame 中的字符串 ArrayList
【发布时间】:2014-01-30 16:06:38
【问题描述】:

我的 JFrame 存在编程问题。我希望我的 GUI 程序在用户按下“接受”按钮后向 ArrayList 添加单个字符串值。我已经写了几行代码,但现在我已经到了“死胡同”,不知道如何完成它。目前我遇到了“first_names.add(firstName);”行的问题。

public class Card extends JFrame {

    private JTextField firstName;
    private JTextField lastName;
    private JTextField amount;
    private JButton accept;

    public Card() {
        super("Cash Machine");
        setLayout(new FlowLayout());

        firstName = new JTextField("Enter your first name here");
        firstName.setEditable(true);
        add(firstName);

        lastName = new JTextField("Enter your last name here");
        add(lastName);

        amount = new JTextField("Enter amount here");
        add(amount);

        accept = new JButton("Accept");
        add(accept);

        newHandler handler = new newHandler();
        firstName.addActionListener(handler);
        lastName.addActionListener(handler);
        amount.addActionListener(handler);
        accept.addActionListener(handler);

    }
}

private class newHandler implements ActionListener {

    ArrayList<String> first_names = new ArrayList<String>();
    ArrayList<String> last_names = new ArrayList<String>();
    public void actionPerformed(ActionEvent event) {

        if(event.getSource()==firstName) {
            first_names.add(firstName);
        }
    }
}

感谢您的建议。

【问题讨论】:

  • 错误是什么?可以分享一下日志吗?

标签: java arrays swing button jframe


【解决方案1】:

试试 firstName.getText()

first_names.add(firstName.getText())

【讨论】:

    【解决方案2】:

    粗略查看您的代码后,似乎有一个问题。您已将 ArrayList 参数化为 String 类型。这一行:

     first_names.add(firstName);
    

    添加这个对象:

    private JTextField firstName;
    

    其实不是String,而是JTextField!

    改用firstName.getText(),应该可以解决您的问题。

    【讨论】:

      【解决方案3】:

      这行好像错了:

      first_names.add(firstName);
      

      first_namesArrayListStrings,所以在做add() 时必须给它一个StringgetText() 方法确实为firstName 返回String

      first_names.add(firstName.getText());
      

      【讨论】:

        【解决方案4】:

        您只需添加一个动作监听器。这是一个可以帮助你的代码。

        private JTextField firstName; 
        private JTextField lastName;
        private JTextField amount;
        private JButton accept;
        private ArrayList<String> first_names = new ArrayList<String>();
        private ArrayList<String> last_names = new ArrayList<String>();
        
        public Card() {
            super("Cash Machine");
            setLayout(new FlowLayout());
        
            firstName = new JTextField("Enter your first name here");
            firstName.setEditable(true);
            add(firstName);
        
            lastName = new JTextField("Enter your last name here");
            add(lastName);
        
            amount = new JTextField("Enter amount here");
            add(amount);
        
            accept = new JButton("Accept");
            add(accept);
        
            //ONE LISTENER
            accept.addActionListener(handler);
        
        }
        
        private class newHandler implements ActionListener {
        
        
            public void actionPerformed(ActionEvent event) {
        
            //Adding textField values to your lists
                first_names.add(firstName.getText());
                last_names.add(lastName.getText())
        
        
            }
        
        
        }
        

        【讨论】:

        • 他似乎在他的所有组件中添加了一个 ActionListner,这是错误的。
        猜你喜欢
        • 1970-01-01
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        相关资源
        最近更新 更多