简体   繁体   English

Java如何将JOptionPane值存储在数组中

[英]Java how to store JOptionPane value in array

hey so I have this assignment that requires me to create a mini program with a menu system that has options 1 and 2. Option 1 requires me to use JOptionPane to input and store people's names and their salaries using arrays (not arraylist). 嘿,所以我要完成这个任务,要求我创建一个带有选项1和2的菜单系统的小程序。选项1需要我使用JOptionPane使用数组(而不是arraylist)输入和存储人员的姓名和薪水。 After that, option 2 will display the list of peoples names and salaries. 此后,选项2将显示人员姓名和工资列表。 However, I am unsure specifically on how to store user's input from JOptionPane into an array and later display it. 但是,我不确定如何将用户从JOptionPane的输入存储到数组中,然后再显示它。 Any help would be much appreciated! 任何帮助将非常感激! thank you! 谢谢!

    public class HR{  
    public static void getName(){
        String[] name= new String[20];
        for (int count=0;count<1;count++){
            name[count]= JOptionPane.showInputDialog(null,"Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
        } 
    } 
    public static void getSalary(){
        String[] salary= new String[20];
        for (int count=0;count<1;count++){
            salary[count]= JOptionPane.showInputDialog(null,"Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);
        } 
    } 

Using a single String array here is probably not ideal, as it holds just one String in each position, so you'd have to do something like this*: 在这里使用单个String array可能并不理想,因为它在每个位置仅持有一个String ,因此您必须执行以下操作*:

String arr[] = new String[20];
arr[0] = JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
arr[1] = JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);

JOptionPane.showMessageDialog(null, "Name is: " + arr[0] + " and salary is " + arr[1]);

If 20 is your size, you can use a loop and add 10 entries of name and salary and print them after. 如果您的尺寸为20,则可以使用循环,并添加10个姓名和工资条目,然后在其上打印。

for (int i = 0; i < arr.length; ++i) {
    arr[i] = JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
    arr[++i] = JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);
}

for (int i = 0; i < arr.length; ++i) {
    JOptionPane.showMessageDialog(null, "Name is: " + arr[i] + " and salary is " + arr[++i]);
}

However, again you'd need to make sure the length of the array is an even number, and note the ++i instead of i++ here. 但是,再次需要确保数组的长度为偶数,并在此注意++i而不是i++ Also, I'm still recommending using a different approach than a single String array for this problem. 另外,对于此问题,我仍然建议使用与单个String数组不同的方法。 Even an array of your own custom class would work better. 甚至您自己的自定义类的数组也可以更好地工作。

So add your own class ie Person : 因此,添加您自己的类,即Person

private String name;
private String salary;
//getters and setters

Define your array as: 将数组定义为:

Person arr[] = new Person[20];

Loop and set your values: 循环并设置您的值:

for (int i = 0; i < arr.length; ++i) {
    Person p = new Person();
    p.setName(JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE));
    p.setSalary(JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE));
    arr[i] = p;
}

And print them: 并打印它们:

for (int i = 0; i < arr.length; i++) {
    JOptionPane.showMessageDialog(null, "Name is: " + arr[i].getName() + " and salary: " + arr[i].getSalary());
}

*Based on your example code *根据您的示例代码

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.sun.accessibility.internal.resources.accessibility;

import javax.swing.JButton;

public class stas {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    public static String salary_1="";
    public static String name_1="";

    public static String []name=new String[20];
    public static String []salary=new String[20];
    public static int counter=0;
    public static void get_name(String a)
{
    name[counter]=a;


}
    public static void get_salary(String a)
{
    salary[counter]=a;


}


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

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

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

        JLabel lblNewLabel = new JLabel("Enter Employe Name");
        lblNewLabel.setBounds(10, 47, 143, 20);
        frame.getContentPane().add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(198, 44, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        textField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
        name_1=textField.getText();


            }
        });

        JLabel lblNewLabel_1 = new JLabel("Enter Employe Salary");
        lblNewLabel_1.setBounds(10, 119, 114, 17);
        frame.getContentPane().add(lblNewLabel_1);

        textField_1 = new JTextField();
        textField_1.setBounds(198, 116, 86, 20);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);
        textField_1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                 salary_1=textField_1.getText();



            }
        });

        JButton btnNewButton = new JButton("Add Employe");
        btnNewButton.setBounds(168, 175, 116, 23);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                get_salary(salary_1);
                get_name(name_1);
                textField_1.setText("");
                textField.setText("");
                JOptionPane.showMessageDialog(null,"employe addedd");


            }
        });
       }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM