java - 无法通过 jtextField() 输入将对象添加到 ArrayList

标签 java swing arraylist jtextfield

当您按下 JButton Ny 时,用户输入应该作为对象(class Deltagare)添加到 中ArrayList,当用户稍后按下 JButton Visa 时,它应该显示在 JTextArea 中。

问题是 textArea 只显示一个空的 Arraylist,输入没有添加到 Arraylist。

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

class Tävling extends JFrame
{   
    public static ArrayList<Deltagare> list = new ArrayList<Deltagare>();

    JTextArea text = new JTextArea();

    JRadioButton rb1 = new JRadioButton("Startnr", true);
    JRadioButton rb2 = new JRadioButton("Namn", false);
    JRadioButton rb3 = new JRadioButton("Ålder", false);
    JRadioButton rb4 = new JRadioButton("Tid", false);

    Tävling()
    {
        super ("DSV Kista Marathon");
        setLayout(new BorderLayout());

        //NORTH
        JPanel north = new JPanel();
        north.add(new JLabel("DSV Kista Marathon"));
        add(north, BorderLayout.NORTH);

        //CENTER
        add(new JScrollPane(text), BorderLayout.CENTER);
        text.setEditable(false);

        //EAST      
        JPanel east = new JPanel();
        east.setLayout(new BoxLayout(east, BoxLayout.Y_AXIS));
        east.add(new JLabel("Sortering"));

        east.add(rb1);
        east.add(rb2);
        east.add(rb3);
        east.add(rb4);

        ButtonGroup group = new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);

        add(east, BorderLayout.EAST);

        //SOUTH
        JPanel south = new JPanel();

        JButton b1 = new JButton("Ny");
        b1.addActionListener(new B1());
        south.add(b1);  

        JButton b2 = new JButton("Visa");
        b2.addActionListener(new B2());
        south.add(b2);

        JButton b3 = new JButton("Tid");
        b3.addActionListener(new B3());
        south.add(b3);

        add(south, BorderLayout.SOUTH);

        //Set
        setLocation(500,200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,350);
        setVisible(true);

    }

    //Metod för att skapa startnr
    public int getStartnr()
    {
        int startnr = list.size()+1;
        return startnr;
    }

    public static void main(String[] args) 
    {
        new Tävling();
    }

    //Huvudklass slut ------------------------------------------------------    


    //b1 - Ny
    class B1 implements ActionListener
    {
        public void actionPerformed(ActionEvent ave) 
        {
            new F1();
        }   
    }

    //b2 - Visa
    class B2 implements ActionListener
    {
        public void actionPerformed(ActionEvent ave) 
        {
            if (rb1.isSelected())
                text.append(list.toString() + "\n");
            else if (rb2.isSelected())
                text.setText("Namn");
            else if (rb3.isSelected())
                text.setText("Ålder");
            else if (rb4.isSelected())
                text.setText("Tid");
        }   
    }

    //b3 - Tid
    class B3 implements ActionListener
    {
        public void actionPerformed(ActionEvent ave) 
        {
            new F2();
        }   
    }


    //JOptionPane - Ny (Deltagare)
    class F1 implements ActionListener
    {       
        JTextField nfield = new JTextField(12);
        JTextField cfield = new JTextField(12);
        JTextField afield = new JTextField(3);

        F1()
        {
            JPanel form = new JPanel();
            form.add(new JLabel("Startnr "+ getStartnr()));
            form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));

            JPanel r1 = new JPanel();
            r1.add(new JLabel ("Namn:"));
            r1.add(nfield);
            form.add(r1);
            nfield.addActionListener(this);

            JPanel r2 = new JPanel();
            r2.add(new JLabel ("Land:"));
            r2.add(cfield);
            form.add(r2);
            cfield.addActionListener(this);

            JPanel r3 = new JPanel();
            r3.add(new JLabel ("Ålder:"));
            r3.add(afield);
            form.add(r3);
            afield.addActionListener(this);

            JOptionPane.showConfirmDialog(null, form, "Ny Tävlande"
                                          , JOptionPane.OK_CANCEL_OPTION);
        }

        public void actionPerformed(ActionEvent ave) 
        {
            String name = nfield.getText();
            String country = cfield.getText();
            int age = Integer.parseInt(afield.getText());
            int startnr = getStartnr();

            list.add(new Deltagare(name, country, age, startnr));       
        }   
    }

    //JOptionPane - Tid (Registrera ny)
    class F2
    {       
        JTextField field1 = new JTextField(6);
        JTextField field2 = new JTextField(6);

        F2()
        {
            JPanel form = new JPanel();
            form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));

            JPanel r1 = new JPanel();
            r1.add(new JLabel ("Startnr:"));
            r1.add(field1);
            form.add(r1);

            JPanel r2 = new JPanel();
            r2.add(new JLabel ("Tid:"));
            r2.add(field2);
            form.add(r2);

            JOptionPane.showConfirmDialog(null, form, "Registrera Tid"
                                           , JOptionPane.OK_CANCEL_OPTION);

        }
    }   
}

Deltagare 类:

public class Deltagare
{
    public String name,country;
    public int age,startnr;

    public Deltagare(String n, String c, int a, int b)
    {
        this.name = "n";
        this.country = "c";
        this.age = a;   
        this.startnr = b;
    }

    public void setStartnr(int b)
    {
        startnr = b;
    }

    public int getStartnr()
    {
        return startnr; 
    }

    public String getName()
    {
        return name;    
    }

    public String getCountry()
    {
        return country; 
    }

    public int getAge()
    {
        return age; 
    }

    public String toString()
    {
        return startnr + "" + name + " " + country + " " + age;
    }

}

最佳答案

首先你的 Deltagare 类的构造函数做错了,替换

public Deltagare(String n, String c, int a, int b)
{
    this.name = "n";
    this.country = "c";
    this.age = a;   
    this.startnr = b;
}

public Deltagare(String n, String c, int a, int b)
{
    // removed quotes to assign value of n to name and same for country.
    this.name = n;
    this.country = c;
    this.age = a;   
    this.startnr = b;
}

然后,您已将 actionListener 添加到 F1 类中的所有 JTextField,但是如果用户单击 < kbd>确定 按钮或取消 按钮。为此,请执行以下操作,在 F1 的构造函数中获取输入:

F1()
{
    JPanel form = new JPanel();
    form.add(new JLabel("Startnr "+ getStartnr()));
    form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));

    JPanel r1 = new JPanel();
    r1.add(new JLabel ("Namn:"));
    r1.add(nfield);
    form.add(r1);
    nfield.addActionListener(this);

    JPanel r2 = new JPanel();
    r2.add(new JLabel ("Land:"));
    r2.add(cfield);
    form.add(r2);
    cfield.addActionListener(this);

    JPanel r3 = new JPanel();
    r3.add(new JLabel ("Ålder:"));
    r3.add(afield);
    form.add(r3);
    afield.addActionListener(this);

    int choice = JOptionPane.showConfirmDialog(null, form, "Ny Tävlande"
                                       , JOptionPane.OK_CANCEL_OPTION);
    // If the value of the user is OK, then do this, else do nothing.                             
    if (choice == JOptionPane.OK_OPTION)
    {
        String name = nfield.getText();
        String country = cfield.getText();
        int age = Integer.parseInt(afield.getText());
        int startnr = getStartnr();

        list.add(new Deltagare(name, country, age, startnr));
    }
    else if (choice == JOptionPane.CANCEL_OPTION)
    {
        System.out.println("CANCEL OPTION SELECTED, DO SOMETHING NOW :-)");
    }
}

关于java - 无法通过 jtextField() 输入将对象添加到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9957439/

相关文章:

java - 为什么我无法将嵌入的 ArrayList<Integer> 分配给本地声明的 ArrayList<Integer>?

java - JTabbedPane 自定义选项卡外观

java - 使用 StyledDocument.insertString 将富文本附加到 JTextPane 不会保留字体

java - 令人沮丧的外观和感觉

java - 无法读取响应输出中的 application/json 消息

java - java中的重叠图像

java - 我可以比较数组与数组的 ArrayList 的相等性吗?

java - arraylist 的保存和读取类

java - 如何检查代码是在 GWT 的服务器端还是在客户端执行?

java - Android studio 删除了我的mapsactivity.java 如何恢复?