要求是: 在totle number 里输入数字,如果不在1---23之间或有异常,就清除掉之后重新输入,从左到右,JTextField 一个一个检查, 出现错误或异常就清除掉该区域 , 并且请你重新输入,如果输入全部正确就把student number和Score添加到JTextArea,按项目添加, 并且如果你改动该学生的分数时 JTextArea也可以相应变化,并能把相应的项目添加到Array中, 请教: 我应该怎么写?或是提供提供你们的思路,^_^
thank you!

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;import java.awt.event.*;;
    public class Gui {

    private JButton jb1,jb2;
    private JTextArea jtext;
    private JFrame jframe;
    private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8,jp9,jp10;
    private JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8,jl9,jl10,jl11,jl12,jl13;
    private JTextField jt1, jt2, jt3,jt4 ;
    private double[] arrayScore;
    private double pinkValue,orangeValue,blueValue;
    private boolean pinkTrue,orangeTrue,blueTrue;
    public Gui(){
    }public void creatGui(){
    jframe= new JFrame("Score Statistic tools");
    //creat menubar
        JMenuBar menubar=new JMenuBar();
        jframe.setJMenuBar(menubar);
        JMenu fileMenu=new JMenu("File");
    JMenu helpMenu=new JMenu("Help");
    //add items to menubar
    menubar.add(fileMenu);
    menubar.add(helpMenu);
        //add the items about the application tool in menu
    fileMenu.add("New");
    fileMenu.add("Open");
    fileMenu.add("Close");
    fileMenu.addSeparator(); //add a Sepatator line
    fileMenu.add("Save");
    fileMenu.add("Save as...");
    fileMenu.addSeparator();
    fileMenu.add("Print...");
        helpMenu.add("Help Contents");
        helpMenu.addSeparator();
        helpMenu.add(" About Score static tooles"); 

    Toolkit theKit=jframe.getToolkit();         //Get the windowtiilkit
    Dimension wndSize=theKit.getScreenSize();   //Get the screen size*/
        
        jframe.setBounds(wndSize.width/4,wndSize.height/4,wndSize.width/2,wndSize.height/2);  //position&size
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);               //Display the window
        Container container=new Container();
        container=jframe.getContentPane();
        container.setLayout(new BorderLayout());
        
     
        jp1= new JPanel();
        jp2= new JPanel();
        jp3= new JPanel();
        jp4= new JPanel();
        
        container.add(jp1,BorderLayout.NORTH);
        jp1.setLayout(new GridLayout(2,1));
        jp5=new JPanel();
        jp6=new JPanel();
        GridLayout lay1=new GridLayout(1,5);
        lay1.setHgap(15);
        jp1.add(jp5);jp5.setLayout(lay1);
        jp1.add(jp6);jp6.setLayout(lay1);
        container.add(jp2,BorderLayout.CENTER);
        jp2.setLayout(new GridLayout(1,2));
        jp2.add(jp4);
        jp2.add(jp3);
        
        jl1=new JLabel("Totle Number");  jl1.setForeground(new Color(255,175,175));        jl1.setHorizontalAlignment(JTextField.CENTER);
        jl2=new JLabel("Student Number");  jl2.setForeground(new Color(255,200,0));           jl2.setHorizontalAlignment(JTextField.CENTER);
        jl3=new JLabel("Studnet Score");     jl3.setForeground(new Color(0,255,255));  jl3.setHorizontalAlignment(JTextField.CENTER);
        jl4=new JLabel("Class Type");  jl4.setForeground(new Color(0,255,0));             jl4.setHorizontalAlignment(JTextField.CENTER);
        jb1=new JButton("Analyse");
        jb2=new JButton("Enter");    
        jt1=new JTextField();jt1.setBackground(new Color(255,175,175));
        jt2=new JTextField();jt2.setBackground(new Color(255,200,0));
        jt3=new JTextField();jt3.setBackground(new Color(0,255,255));
        jt4=new JTextField();jt4.setBackground(new Color(0,255,0));    
        
        jp5.add(jl1); jp5.add(jl2); jp5.add(jl3); jp5.add(jl4); jp5.add(jb1);    
        jp6.add(jt1); jp6.add(jt2); jp6.add(jt3); jp6.add(jt4); jp6.add(jb2);
        
        GridLayout lay3=new GridLayout(5,2);
        lay3.setHgap(-45);
        jp3.setLayout(lay3);
        jl4=new JLabel("Statistic Item");  jl4.setHorizontalAlignment(JTextField.CENTER);
        jl5=new JLabel("Statistic Result");jl5.setHorizontalAlignment(JTextField.CENTER);
        jl6=new JLabel("Highest Score");jl6.setHorizontalAlignment(JTextField.CENTER);
        jl7=new JLabel("Lowest Score");jl7.setHorizontalAlignment(JTextField.CENTER);
        jl8=new JLabel("Avarage Score");jl8.setHorizontalAlignment(JTextField.CENTER);
        jl9=new JLabel("Failure Rate");jl9.setHorizontalAlignment(JTextField.CENTER);
        jl10=new JLabel(""); jl10.setHorizontalAlignment(JTextField.CENTER);
        jl11=new JLabel(""); jl11.setHorizontalAlignment(JTextField.CENTER);
        jl12=new JLabel(""); jl12.setHorizontalAlignment(JTextField.CENTER);
        jl13=new JLabel(""); jl13.setHorizontalAlignment(JTextField.CENTER);
        
        jp7= new JPanel();    jp7.setBackground(new Color(255,175,175));   jp7.setLayout(new BorderLayout());
        jp8= new JPanel();    jp8.setBackground(new Color(255,200,0));     jp8.setLayout(new BorderLayout());
        jp9= new JPanel();    jp9.setBackground(new Color(0,255,255));     jp9.setLayout(new BorderLayout());
        jp10= new JPanel();   jp10.setBackground(new Color(0,255,0));      jp10.setLayout(new BorderLayout());
        
              
        jp3.add(jl4);
        jp3.add(jl5);
        jp3.add(jl6);
        jp3.add(jp7);   jp7.add(jl10,BorderLayout.CENTER);
        jp3.add(jl7); 
        jp3.add(jp8);   jp8.add(jl11,BorderLayout.CENTER);
        jp3.add(jl8);
        jp3.add(jp9);   jp9.add(jl12,BorderLayout.CENTER);
        jp3.add(jl9);   
        jp3.add(jp10);  jp10.add(jl13,BorderLayout.CENTER);
        
        jtext=new JTextArea(); 
        jp4.add(jtext);
        jp4.setBackground(new Color(255,255,255));
        
        
        ActionListener l1=new ActionListener(){
            public void actionPerformed(ActionEvent e){
            Gui gui=new Gui();
            while(!gui.checkPinkTrue()){
            jt1=new JTextField();jt1.setBackground(new Color(255,175,175));
            jp6.add(jt1);
            }
            if(gui.checkPinkTrue()){
            while(!gui.checkOrangeTrue()){
            jt2=new JTextField();jt2.setBackground(new Color(255,200,0)); 
            
            }
            if(gui.checkOrangeTrue()){
            while(!gui.checkBlueTrue()){
            jt3=new JTextField();jt3.setBackground(new Color(0,255,255));
            }
            if(gui.checkBlueTrue()){
            jtext.setText("aaaaaaaaa");
            }
            }
            }
            }
          };
          jb2.addActionListener(l1);
          
          
          ActionListener l2=new ActionListener(){
              public void actionPerformed(ActionEvent e1){
              
              }      
    };jb1.addActionListener(l2);
          }public boolean checkPinkTrue(){

        try{
       String pinktext= jt1.getText();
       pinkValue=Double.parseDouble(pinktext);
       if(pinkValue<=0||pinkValue>23){
         JOptionPane.showMessageDialog(null,"sorry, the class size is between 1 and 23");
         pinkTrue=false;        
       }
        }
       catch(Exception pink){
       JOptionPane.showMessageDialog(null,"sorry, please enter number in the totle number field");
       jt1.setText("");
       pinkTrue=false;             
       }
       return pinkTrue;
    }
    public boolean checkOrangeTrue(){
    try{
     String orangetext= jt2.getText();
     orangeValue=Double.parseDouble(orangetext);
     if(orangeValue<=0||orangeValue>pinkValue){
     JOptionPane.showMessageDialog(null,"sorry,please enter the number between 1 and class size");
     jt2.setText("");
     orangeTrue=false;
     }  
    }
    catch(Exception orange){
    JOptionPane.showMessageDialog(null,"you must enter a number in student number field");
    //jt2.setText("");
        orangeTrue=false;
    }
    return orangeTrue;
    }
     public boolean checkBlueTrue(){
     try{
     String bluetext=   jt3.getText();
     blueValue=Double.parseDouble(bluetext);
     if(blueValue<0||blueValue>100){
     JOptionPane.showMessageDialog(null,"sorry, please ensure the Score is between 0 and 100");
     jt3.setText("");
     blueTrue=false;
     }
     }
     catch(Exception blue){
     JOptionPane.showMessageDialog(null,"Sorry, you must enter a number in Score field");
     jt3.setText("");
     blueTrue=false;
     }
     return blueTrue;
     }public static void main(String[] args){
    Gui a=new Gui();
    a.creatGui();
    }
    }
    这是我还没有写完的