0

В текстовом поле записаны числа в таком виде: 4, 6, 10, 30, 8. Как получить эти значения и записать их в ArrayList ? Вот пример моего кода:

public class MainFrame extends JFrame {
    private String title;
    private Dimension d;
    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JTextField  setATextField = new JTextField(10);
    private JTextField setBTextField = new JTextField(10);
    private JTextField resultTextField = new JTextField(10);
    private JLabel setALabel = new JLabel("Множина А: ");
    private JLabel setBLabel = new JLabel("Множина B: ");
    private JLabel resultLabel = new JLabel("Результат: ");
    private JButton unionButton = new JButton("Об'єднання");
    private JButton intersectionButton = new JButton("Перетин");
    private JButton differenceButton = new JButton("Різниця");
    private JButton sDifferenceButton = new JButton("Різниця");
    private JButton equelsButton = new JButton("Рівність");
    private JButton isAInBButton = new JButton("Включення");

    public MainFrame(String title, Dimension d) {
        this.title = title;
        this.d = d;
    }

    public void init(){
        setTitle(title);
        setSize(d);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(2,0));

        unionButton.addActionListener(new UnionActionListener());

        add(panel1);
        add(panel2);
        panel1.setLayout(new GridLayout(3,2));
        panel2.setLayout(new GridLayout(2,3));
        panel1.add(setALabel);
        panel1.add(setATextField);
        panel1.add(setBLabel);
        panel1.add(setBTextField);
        panel1.add(resultLabel);
        panel1.add(resultTextField);
        panel2.add(unionButton);
        panel2.add(differenceButton);
        panel2.add(sDifferenceButton);
        panel2.add(intersectionButton);
        panel2.add(isAInBButton);
        panel2.add(equelsButton);

        setVisible(true);
        pack();
    }

    //Action Listener
    public class UnionActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            ArrayList<Integer> setA = new ArrayList<>();
           // тут нужно добавить значения с setATextField в setA.
            ArrayList<Integer> setB = new ArrayList<>();
        }
    }
}
2
  • 1
    setATextField.getText() вернет строку, вписанную в текстовое поле.
    – zRrr
    13 мар 2016 в 12:55
  • Если вам дан исчерпывающий ответ, отметьте его как верный (галка напротив выбранного ответа). 14 мар 2016 в 6:59

1 ответ 1

3
String string = setATextField.getText(); //получаем строку с числами
String[] numbers = string.split(","); //разбиваем на массив строк по запятым
for (String s:numbers) {
  //добавляем в список, убирая все не-буквы и преобразовывая в Integer
  setA.add(Integer.parseInt(s.replaceAll("\\D",""))); 
}

И конечно, нужно не забыть про то, что Integer.parseInt() может выбросить исключение.

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.