Java GUI-选择ComboBox中的项目时,在文本字段中设置特定文本

Java GUI-选择ComboBox中的项目时,在文本字段中设置特定文本,java,eclipse,combobox,interface,Java,Eclipse,Combobox,Interface,我正在尝试创建一个程序(使用JavaGUI),要求用户添加自己的姓名和年龄。当按下“添加”按钮时,用户名会进入一个组合框,以便以后选择 当在组合框中选择一个项目时,年龄值应该出现在组合框下方的文本字段中,但我不知道如何完成最后一部分。这是 [这是它的样子][1] JButton btnAdd = new JButton("Add"); //Makes user's name goes to JComboBox btnAdd.addActionListener(new ActionList

我正在尝试创建一个程序(使用JavaGUI),要求用户添加自己的姓名和年龄。当按下“添加”按钮时,用户名会进入一个组合框,以便以后选择

当在组合框中选择一个项目时,年龄值应该出现在组合框下方的文本字段中,但我不知道如何完成最后一部分。这是 [这是它的样子][1]

JButton btnAdd = new JButton("Add"); //Makes user's name goes to JComboBox
    btnAdd.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent arg0) {
            comboB.addItem(name.getText());
            }
    });
我想我必须将actionListener与ComboBox结合使用才能实现这一点。所以每次选择时间和项目时,它都会做我想做的事情

我想我必须将文本以数组的形式存储在“age”字段中,并以某种方式将其与ComboBox项关联起来,但我不知道如何做


谢谢你的帮助。全部代码:

我接受了您提供的代码并对其进行了编辑以满足您的需要。我对我所做的每一项更改都进行了评论,以便您能够尝试了解我所做的:

    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;
    import java.awt.Color;
    import javax.swing.JComboBox;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.awt.event.ActionEvent;

    public class comboTest {

        private JFrame frame;
        private JTextField name;
        private JTextField age;
        private JTextField ageField;
        private JTextField txtName;
        private JTextField txtAge;
        private JTextField txtAgeFromPerson;
        private ArrayList<Person> persons = new ArrayList<Person>();

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        comboTest window = new comboTest();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the application.
         */
        public comboTest() {
            initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 473, 323);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);


            //Panel that contains name field and age field
            JPanel panel = new JPanel();
            panel.setBorder(new LineBorder(new Color(0, 0, 0)));
            panel.setBounds(144, 94, 175, 119);
            frame.getContentPane().add(panel);
            panel.setLayout(null);

            //TextField name where the user will write his/her name
            name = new JTextField();
            name.setBounds(79, 26, 86, 20);
            panel.add(name);
            name.setColumns(10);

            //TextField age where the user will write his/her age
            age = new JTextField();
            age.setBounds(79, 57, 86, 20);
            panel.add(age);
            age.setColumns(10);

            //Simple text - it does not interfere with the code
            txtName = new JTextField();
            txtName.setEditable(false);
            txtName.setText("Name");
            txtName.setBounds(5, 26, 69, 20);
            panel.add(txtName);
            txtName.setColumns(10);

            //Simple text - it does not interfere with the code
            txtAge = new JTextField();
            txtAge.setText("Age");
            txtAge.setEditable(false);
            txtAge.setBounds(5, 57, 69, 20);
            panel.add(txtAge);
            txtAge.setColumns(10);

            //Creating ComoboBox
            JComboBox comboB = new JComboBox();
            comboB.setBounds(10, 32, 105, 20);
            frame.getContentPane().add(comboB);

            //This is where the text (age) from the Jpanel will go to
            ageField = new JTextField();
            ageField.setEditable(false);
            ageField.setBounds(108, 111, 33, 20);
            frame.getContentPane().add(ageField);
            ageField.setColumns(10);

            //Adding name to ComboBow
            JButton btnAdd = new JButton("Add");
            btnAdd.setBounds(179, 224, 89, 23);
            frame.getContentPane().add(btnAdd);
            btnAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                        //the problem you had was, that you didnt save the age of the person you added, so i created the class Person(it is at the bottom of the file), which saves a name and an age
                        Person person = new Person(name.getText(),age.getText());//we create a person with the given name and age
                        comboB.addItem(person);//and add this person to the combo.
                        //we can add ANY Object to the combo, the combo will write the return of the toString method of the given object, so i have overridden the toString Method of the Person class to return the Persons name.
                    }
            });

            //this is the comboBox actionListener, but I don't know what to write here
            comboB.addActionListener(new ActionListener (){
                public void actionPerformed (ActionEvent e) {
                    Person person = (Person)comboB.getSelectedItem();//here we get the selected object from the combo and cast it to a Person Object since we know it is a person and we have to get the persons age
                    ageField.setText(person.getAge());//then we get the Persons age, and put it into the Textfield that is supposed to show the selected Persons age
                }
            });

            //Simple text - it does not interfere with the code
            txtAgeFromPerson = new JTextField();
            txtAgeFromPerson.setEditable(false);
            txtAgeFromPerson.setText("Age from person:");
            txtAgeFromPerson.setBounds(4, 111, 101, 20);
            frame.getContentPane().add(txtAgeFromPerson);
            txtAgeFromPerson.setColumns(10);
        }
    }

    class Person{// a simple class that stores a name and an age
        private String name, age;

        public Person(String name,String age){// usually the age would be stored in an int, but i used a string so we dont have to convert it when we read it from the textfield and when we put it into a textfield. So it is just for simplicitys sake
            this.name = name;
            this.age = age;
        }

        public String getAge(){
            return age;
        }

        public String getName(){
            return name;
        }

        public String toString(){
            return this.name;
        }
    }
导入java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JButton;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.border.LineBorder;
导入java.awt.Color;
导入javax.swing.JComboBox;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.awt.event.ActionEvent;
公共类组合测试{
私有JFrame;
私有JTextField名称;
私人领域年龄;
私有JTextField-ageField;
私有JTextField txtName;
私有JTextField-txtAge;
私有JTextField txtAgeFromPerson;
private ArrayList persons=新ArrayList();
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
comboTest窗口=新建comboTest();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共测试(){
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
frame=新的JFrame();
框架.立根(10010047323);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//包含名称字段和年龄字段的面板
JPanel面板=新的JPanel();
面板.设置顺序(新线条边框(新颜色(0,0,0));
小组.挫折(144、94、175、119);
frame.getContentPane().add(面板);
panel.setLayout(空);
//text用户将在其中写入其姓名的字段名称
name=新的JTextField();
名称.立根(79,26,86,20);
面板。添加(名称);
名称.设置列(10);
//text用户将在其中写入其年龄的字段年龄
年龄=新的JTextField();
年龄.挫折(79,57,86,20);
加上(年龄);
年龄.设置栏(10);
//简单文本-它不会干扰代码
txtName=newjtextfield();
txtName.setEditable(false);
txtName.setText(“名称”);
txtName.setBounds(5,26,69,20);
panel.add(txtName);
txtName.setColumns(10);
//简单文本-它不会干扰代码
txtAge=newjtextfield();
txtAge.setText(“年龄”);
txtAge.setEditable(false);
立根(5,57,69,20);
面板。添加(txtAge);
txtAge.setColumns(10);
//创建combobox
JComboBox comboB=新的JComboBox();
组合b.挫折(10,32,105,20);
frame.getContentPane().add(comboB);
//这就是Jpanel中的文本(年龄)的目的地
ageField=新的JTextField();
ageField.setEditable(false);
ageField.setBounds(108,111,33,20);
frame.getContentPane().add(ageField);
ageField.setColumns(10);
//向ComboBow添加名称
JButton btnAdd=新JButton(“添加”);
b.挫折(179、224、89、23);
frame.getContentPane().add(btnAdd);
添加ActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件arg0){
//你遇到的问题是,你没有保存你添加的人的年龄,所以我创建了类person(它在文件的底部),它保存了一个名字和年龄
Person-Person=newperson(name.getText(),age.getText());//我们创建一个具有给定姓名和年龄的人
comboB.addItem(person);//并将此人添加到组合中。
//我们可以将任何对象添加到组合中,组合将写入给定对象的toString方法的返回,因此我已重写Person类的toString方法以返回Person名称。
}
});
//这是comboBox actionListener,但我不知道在这里写什么
comboB.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
Person Person=(Person)comboB.getSelectedItem();//这里我们从组合中获取所选对象并将其转换为Person对象,因为我们知道它是Person,并且必须获取Person的年龄
ageField.setText(person.getAge());//然后我们得到Persons age,并将其放入应该显示所选人员年龄的Textfield中
}
});
//简单特克斯