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

莫奈沃肯斯

嘿,所以我有这个任务,要求我创建一个带有选项 1 和 2 的菜单系统的小程序。选项 1 要求我使用 JOptionPane 使用数组(不是数组列表)输入和存储人的姓名和工资。之后,选项 2 将显示人员姓名和工资列表。但是,我不确定如何将来自 JOptionPane 的用户输入存储到数组中并稍后显示它。任何帮助将非常感激!谢谢你!

    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);
        } 
    } 
Hummel Goodspeed Hummelgoodspe
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");


            }
        });
       }
     }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章