如何将文本字段中的东西放入java中的整数数组中

时间:2013-11-23 20:17:56

标签: java swing jtextfield

我有一个程序,我试图将其放入GUI中,我不知道如何将用户放入文本字​​段的内容放入数组中     到目前为止,这是我的代码:

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUIMain extends JFrame{
    @SuppressWarnings("unused")
    private static JLabel label3;
    @SuppressWarnings("unused")
    private static JLabel label2;
    @SuppressWarnings("unused")
    private static JLabel label4;
    @SuppressWarnings("unused")
    private static JLabel label;
    private static JTextField text;
    private static final long serialVersionUID = 1L;
    private static int [] array={98,99,100};
    @SuppressWarnings("static-access")
    public GUIMain(){
        super("Math Converter");
        //this.array = array;
        Arrays.sort(array);
        //System.out.println("The median is : "+median(array));//prints Median
        //System.out.println("The mode is : "+mode(array));//prints Mode
        //System.out.println("The mean is : "+mean(array));//prints the mean
        //pack();
        setSize(600,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(true);
        setVisible(true);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(10,10,10,10));//
        JTextField text = new JTextField(10);
        JLabel label = new JLabel(toStringMedian(array));
        JLabel label2 = new JLabel(toStringMode(array));
        JLabel label3 = new JLabel(toStringMean(array));

        JLabel label4 = new JLabel("How many numbers do you wan't in your array?");
        Container content = getContentPane();
        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.CENTER);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        add(panel1);
        content.setLayout(layout);
        content.add(panel);
        content.add(panel1);
        content.add(panel2);
        panel.setLayout(new GridLayout(10,2,10,10));//10,2,10,10
        panel1.setLayout(new GridLayout(1,2,1,10));//1,2,1,10
        panel2.setLayout(new GridLayout(1,4,10,10));//1,4,10,10
        panel.add(label4);
        panel.add(text);
        panel.add(label);
        panel.add(label2);
        panel.add(label3);
        text.addActionListener(new Action());
        JMenuBar menu = new JMenuBar();
        setJMenuBar(menu);
        JMenu file = new JMenu("File");
        menu.add(file);
        JMenu Help = new JMenu("Help");
        menu.add(Help);
        JMenuItem About = new JMenuItem("About");
        Help.add(About);
        JMenuItem Exit = new JMenuItem("Exit");
        file.add(Exit);
        Exit.addActionListener(new Exit());
        this.label = label;
        this.label2 = label2;
        this.label3 = label3;
        this.label4 = label4;
        this.text = text;
    }
    public static void main(String[] args) throws Exception {
        new GUIMain();
    }

    public static int mode(int[] a) {
        int previous = a[0];//sets previous to array at 0
        int popular = a[0];//Initializes popular
        int count = 1;//initializes count
        int maxCount = 1;//initializes max count

        for (int i = 1; i < a.length; i++) {//goes through the array
            if (a[i] == previous)//tests to see if array at i is equal to previous 
                count++;//adds one every time this is true

            else {//if not then it will go through the next statement
                if (count > maxCount) {//tests to see if count is greater than max count
                    popular = a[i-1];//sets popular to a at i-1
                    maxCount = count;//sets max count to count
                }
                previous = a[i];//sets previous to a at i
                count = 1;//sets count to one when finished then it goes through the loop again
            }
        }
        return count > maxCount ? a[a.length-1] : popular;//returns only if count is greater than max count the integer popular
    }
    public static int median(int[] a){
        int median= 0 ;//declares median
        if(a.length%2==0)//tests to see if the length of the array is even 
            median = (a[(int)a.length/2] + (int)a[a.length/2+1])/2;//if it is even then it takes the averages of the two numbers in the middle
        else{
            median = a[a.length/2];//finds the number in the middle of the array
        }
        return median;//returns the value of median
    }
    public static int mean(int[]a){
        int mean = 0;
        for(int i = 0; i<a.length;i++){
            mean =(int)(a[i]+a [i]-1)/a.length;//finds the average of the elements of the array
        }
        return mean;//returns the value of mean
    }
    public static String toStringMedian(int[]a){
        return"This is the Median : "+median(a);
    }
    public static String toStringMean(int[]a){
        return"This is the Mean : "+mean(a);
    }
    public static String toStringMode(int[]a){
        return"This is the Mode : "+median(a);
    }

    static class Exit implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }

    }
    static class Action implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            int x = 0;
            int y = 0;
            int z = 0;
            if(e.getSource()==text){

            }
        }
    }
 }

我之前尝试使用类似的东西来改变它int Input = Integer.parseInt(text.getText());但这需要太长时间还有另一种方式吗?

1 个答案:

答案 0 :(得分:1)

可能需要很长时间,但基本上没有其他方法可以将用户的文本字段输入转换为可以执行计算的整数。

int input = Integer.parseInt(text.getText());

此代码将从用户输入的文本字符串中为您提供单个整数。