JOptionPane不存储String

时间:2017-05-01 21:19:28

标签: java swing

我遇到问题,我使用我的EnterGroup方法输入所有信息,但是当我使用groupSearch找到它时,它只显示正确的名称,并为其余值输入null。我怀疑它与JOPtionPane有关,而不是返回我输入的内容,但我不知道如何解决这个问题。

package assignment7;

import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;

public class homescreen extends JFrame {
    private JLabel windowTitle;
    private JTextField textBox;
    private JButton button;
    private JComboBox<String> comboBox;
    private JRadioButton rbutton;
    private JRadioButton rbutton2;
    private ButtonGroup radioGroup;
    private JPanel window;


private ArrayList<person> people = new ArrayList<person>();
    private ArrayList<group> groups = new ArrayList<group>();
    private ArrayList<String> peopleNames = new ArrayList<String>();
    private ArrayList<String> groupNames = new ArrayList<String>();

public homescreen() {
    super("Database");
    setLayout(new FlowLayout());

    windowTitle = new JLabel("Please enter a group or person");
    textBox = new JTextField("Enter name here");
    button = new JButton("enter");
    rbutton = new JRadioButton("Add", true);
    rbutton2 = new JRadioButton("Search", false);
    String[] choices = {"group", "person"};
    comboBox = new JComboBox<String>(choices);
    window = new JPanel();

    add(windowTitle); add(textBox); 
    window.add(comboBox);
    add(rbutton);
    add(rbutton2);
    radioGroup = new ButtonGroup();
    radioGroup.add(rbutton);
    radioGroup.add(rbutton2);
    add(button);
    add(window);

    //Handler h = new Handler();
    //ActionListener l = new ActionEvent();
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(rbutton.isSelected() && comboBox.getSelectedItem().equals(choices[0]))
                enterGroup(textBox.getText());
            if(rbutton.isSelected() && comboBox.getSelectedItem().equals(choices[1]))
                enterPerson();
            if(rbutton2.isSelected() && comboBox.getSelectedItem().equals(choices[0]))
                groupSearch(textBox.getText());
            if(rbutton2.isSelected() && comboBox.getSelectedItem().equals(choices[1]))
                personSearch();
        }
    });
    //button.addActionListener(h);
}

public void groupSearch(String name) {
    boolean found = false;
    for(int i = 0; i < groupNames.size(); i++) {
        if(name.equals(groupNames.get(i))) {
            JOptionPane.showMessageDialog(null, "Name: " + groupNames.get(i) + "\nYears Established: " + groups.get(i).yearsEstablished + "\nState: " + groups.get(i).state + "\nCity: " + groups.get(i).city + "\nMembers: " + groups.get(i).members, "Group Found",JOptionPane.INFORMATION_MESSAGE);
            i = groupNames.size();
        }
    }
    if(found == false)
        JOptionPane.showMessageDialog(null, "Group Not Found", "Not In Database",JOptionPane.INFORMATION_MESSAGE);
}

public void personSearch() {

}

public void enterGroup(String name) {
    //String name = JOptionPane.showInputDialog("Enter name of group");
    String yearsEstablished = null;
    String state = null;
    String city = null;
    String members = null;
    yearsEstablished = JOptionPane.showInputDialog("Enter number of years group has been established");

    if(yearsEstablished != null) {
        state = JOptionPane.showInputDialog("Enter state group is located in");
            if(state != null) {
                city = JOptionPane.showInputDialog("Enter city group is located in");
                    if(city != null) {
                        members = JOptionPane.showInputDialog("Enter number of members in group");
            }
        }
    }
    int years = 0;
    int num = 0;

    if(yearsEstablished != null) {
        StringTokenizer st = new StringTokenizer(yearsEstablished);
        years = Integer.parseInt(st.nextToken());
    }
    if(members != null) {
        StringTokenizer st2 = new StringTokenizer(members);
        num = Integer.parseInt(st2.nextToken());
    }
    if(name != null && yearsEstablished != null && state != null && city != null && members != null) {
        groups.add(new group(name, years, state, city, num));
        groupNames.add(name);
        JOptionPane.showMessageDialog(null, "group has been added to the database", "Complete", JOptionPane.INFORMATION_MESSAGE);
    }

}

public void enterPerson() {

}
}

如果你需要,这里是小组课

package assignment7;

public class group {
    public String name;
    public int yearsEstablished;
    public String state;
    public String city;
    public int members;

public group(String name, int yearsEstablished, String state, String city, int members) {
    name = this.name;
    yearsEstablished = this.yearsEstablished;
    state = this.state;
    city = this.city;
    members = this.members;
}

public String getName() {
    return name;
}

public int getYears() {
    return yearsEstablished;
}

public String getState() {
    return state;
}

public String getCity() {
    return city;
}

public int getMembers() {
    return members;
}
}

如果你需要,这里是跑步者/司机类

package assignment7;

import javax.swing.JFrame;

public class runner {
    public static void main(String[] args) {
        homescreen screen = new homescreen();

    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setSize(500,500);
    screen.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:3)

此...

public group(String name, int yearsEstablished, String state, String city, int members) {
    name = this.name;
    yearsEstablished = this.yearsEstablished;
    state = this.state;
    city = this.city;
    members = this.members;
}

是错误的。您正在将group类的实例字段分配给参数,分配应该是相反的方式

public group(String name, int yearsEstablished, String state, String city, int members) {
    this.name = name;
    this.yearsEstablished = yearsEstablished;
    this.state = state;
    this.city = city;
    this.members = members;
}

您可能还想查看Coding Conventions for the Java Language它会让您更轻松地阅读其他人的代码以及让其他人阅读您的代码