从数组写入JTextField

时间:2018-02-01 23:07:03

标签: java arrays swing jtextfield

我有一个关于如何从数组写入JTextField的问题。我有几个版本的家庭作业程序。他们每个人的固有属性和改进功能。我的任务是将note字段从2限制为6,这需要是唯一有效的输入(2,3,4,5,6)。添加新学生时的注释字段只应接受2到6的数字。

import java.io.*;
class Student implements Serializable{
    String name; int note;
    Student(String name, int note){this.name = name;this.note = note;}
    public String toString(){
        return name+"   "+note;
    }
}


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Apl extends JFrame{
    protected JButton ad;
    protected JTextField tf[]= new JTextField[2];
    protected List lst;

    protected JPanel contr, plst;

    protected AdSt adSt;
    protected Student prs[]=new Student[0];

    Apl(int x, int y, int ln, int ht){
        this.setLayout(new BorderLayout());
        this.setBounds(x, y, ln, ht);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Student's directory - version 1");

        contr = new JPanel(new FlowLayout());
        plst = new JPanel(new FlowLayout());

        ad = new JButton("add");
        contr.add(ad);
        tf[0]= new JTextField("name?",10);
        tf[1]= new JTextField("note?",3);
        contr.add(tf[0]);
        contr.add(tf[1]);
        ad.addActionListener(adSt=new AdSt());
        add("North",contr);

        lst = new List(10);
        plst.add(lst);
        add("Center",plst);     
        revalidate();
    }
    class AdSt implements ActionListener{
        public void actionPerformed(ActionEvent e ){
            Student s;
            int nt;
            String n=tf[0].getText();
            try{
                nt=Integer.parseInt(tf[1].getText());
            }
            catch(NumberFormatException ex){
                tf[1].setText("note?");
                return;
            }
            Student help[]= new Student[prs.length+1];
            System.arraycopy(prs, 0, help, 0, prs.length);
            help[help.length-1]= s=new Student(n,nt);
            prs=help; tf[0].setText("name?");tf[1].setText("note?");
            lst.add(""+s);
            revalidate();
        }
    }
    public static void main(String [] arg){
        Apl apl=new Apl(20,20,400,300);
    }
}

import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Apl1 extends Apl{
    protected JButton load,save;  
    Apl1(){
        super(20,20,500,350);
        load = new JButton("load");
        load.addActionListener(new Load());
        contr.add(load);
        save = new JButton("save");
        save.addActionListener(new Save());
        contr.add(save);
        setTitle("Student's directory - version 2");
        revalidate();
    }
    public static void main(String arg[]){
        new Apl1();     
    }
    class Save implements ActionListener  {
        public void actionPerformed(ActionEvent e ){
            ObjectOutputStream oos = null;
            try{
                oos = new ObjectOutputStream (
                    new FileOutputStream ("save.ser"));
                oos.writeObject(prs);
            }
            catch (IOException ex){
                System.out.println(ex);
            }
            try{
                if(oos!=null)oos.close();
            }
            catch (IOException ex){}
        }
    }
    class Load implements ActionListener  {
        public void actionPerformed(ActionEvent e ){
            ObjectInputStream ios = null;
            try{
                ios = new ObjectInputStream (new FileInputStream ("save.ser"));
                prs= (Student[])ios.readObject();
            }
            catch (Exception ex){
                tf[0].setText("Error");
            }
            try{
                if(ios!=null)ios.close();
            }
            catch (IOException ex){}
            lst. removeAll();
            for(int i=0;i<prs.length;i++){
                lst.add(""+prs[i]);
            }           
        }       
    }
}

0 个答案:

没有答案