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

时间:2012-03-31 16:00:01

标签: java swing arraylist jtextfield

当您按下JButton Ny 时,用户输入应该作为对象(类Deltagare)添加到ArrayList中,当用户稍后按JButton 签证,它应显示在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);

        }
    }   
}

Class 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;
    }

}

2 个答案:

答案 0 :(得分:4)

首先,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添加到JTextField课程中的所有F1,但如果用户点击确定按钮或 CANCEL 按钮。为此,请执行以下操作:在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 :-)");
    }
}

答案 1 :(得分:2)

在你的班级F1中,你在JOptionPane中显示一个表格,但是你没有对用户的回答做任何事情(确定或取消)。你有一个方法actionPerformed但是它适用于ActionListener,但是你永远不会在任何地方注册你的ActionListener。我认为你应该简单地处理JOptionPane的结果。如果正常,则调用actionPerformed中的代码(可以重命名)。

注意:showConfirmDialog是一个阻止调用。