如何使用JOptionPane将数据输入到数组中 [英] How to use JOptionPane to input data into an array

查看:96
本文介绍了如何使用JOptionPane将数据输入到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何创建一个数组,我希望一个数组中有8个值,但用户输入了它们?

how do i create an array where I want there to be 8 values in an array but the user inputs them?

这是我到目前为止所拥有的

this is what i have so far

                    import javax.swing.JOptionPane;

                    public class Southside Report {
                       public static void main(String[] args) {

                       int FINAL MIN_STAFF = 7;
                       int total_staff = 0;

                       double[] num_students = 8; 

推荐答案

看看

Take a look to JOptionPane. Option Panes are very customizable. Besides that your code not compile i think that you want the user to input 8 text only in one dialog and you can do it with optionPane with little customization like below example.

import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JOptionPaneTest {

    public static final int OPTIONS = 8;
    private List<JTextField> textfields = new ArrayList<>(OPTIONS);

    private JPanel panel;


    public JOptionPaneTest(){
        panel = new JPanel();

        for(int i =0;i< OPTIONS;i++){
            JTextField textfield = new JTextField(5);
            textfields.add(textfield);
            panel.add(new JLabel(Integer.toString(i+1)+": "));
            panel.add(textfield);
        }

    }


    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
          JOptionPaneTest example = new JOptionPaneTest();
          int result = JOptionPane.showConfirmDialog(null, example.panel, 
                   "Please Enter Values", JOptionPane.OK_CANCEL_OPTION);
          if (result == JOptionPane.OK_OPTION) {
             for(JTextField textfield : example.textfields){
                 System.out.println(textfield.getText()); 
             }

          }
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

并输出:

这篇关于如何使用JOptionPane将数据输入到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆