使用super和toString更新JTextArea

时间:2016-04-20 15:44:14

标签: java jtextarea super

我试图在Person类中使用toString将JTextArea附加到EmployeesApplet类中。最终输出看起来像final output

对于Person类,它需要私有的实例变量,一个构造函数和一个toString方法,现在我只是想让它显示名称,这样我就可以弄清楚如何做其余的 我将如何完成我的Person课程?

EmployeesApplet位于

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

public class EmployeesApplet extends JApplet implements ActionListener 
{
  public JButton              sd   = new JButton ("Salaried");
  public JButton              hr   = new JButton ("Hourly");
  public JButton              cm   = new JButton ("Commissioned");
  public JButton              cl   = new JButton ("Clear"); 

  private final int    FIELDS      =  8,   
                       FIELD_WIDTH = 20;   

  private String[]     strings     = new String[FIELDS];
  private TextFieldWithLabel[] tf  = new TextFieldWithLabel[FIELDS];
  private JTextArea    ta          = new JTextArea(5,25); 

  String[]  s = {"First Name", "Last Name", "Employee ID", "(a) Salaried: Weekly Salary", "(b1) Hourly 1: Rate Per Hour", 
                   "(b2) Hourly 2: Hours Worked" , "(c1) Commissioned: Rate", "(c2) Commissioned: Gross Sales" };

     public void init()
     {
      this.setSize(420, 310);

       //----------------------
       //  Set up the Structure
       //----------------------

       Container c = getContentPane();
       JPanel f   = new JPanel(new FlowLayout());
       JPanel b   = new JPanel(new BorderLayout(2,0));

       JPanel glb = new JPanel(new GridLayout(8,1,0,2));
       JPanel gtf = new JPanel(new GridLayout(8,1,0,2));
       JPanel flb = new JPanel(new FlowLayout());


       // Add FlowLayout to the container
       c.add(f);
       // Add BorderLayout to the FlowLayout
       f.add(b);

       //---------------------------------------
       //Add JPanels to the BorderLayout regions
       //---------------------------------------

       // Add JLables to GridLayout in West
       b.add(glb, BorderLayout.WEST);
       for (int i = 0; i < tf.length; i++)
       {
        tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH);
        glb.add(tf[i].getLabel());
       }

       // Add JTextFeilds to GridLayout in East
       b.add(gtf, BorderLayout.EAST);
       for (int i = 0; i < tf.length; i++)
       {
        tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH);
        tf[i].getTextField();
        gtf.add(tf[i].getTextField());
       }

       // Add JButtons to FlowLayout in South
       b.add(flb, BorderLayout.SOUTH);

       flb.add(sd);
       flb.add(hr);
       flb.add(cm);
       flb.add(cl);

       sd.addActionListener(this);
       hr.addActionListener(this);
       cm.addActionListener(this);
       cl.addActionListener(this);

       // Add JTextArea and make it not editable   
       f.add(ta);
       ta.setEditable(false);

     }

     //---------------------------------------
     //  Read all the JTextFields and 
     //  save the contents in a parallel array
     //---------------------------------------
     private void readFields()
     {
       for (int i = 0; i < tf.length; i++)  // or FIELDS
         strings[i] = tf[i].getText();
     }

     private boolean fieldsExist(int i, int i2)
     {
       if(i == 0 && i2 == 3)  // Checks Salaried worker
       {
         if(tf[0].getText()  == null || tf[0].getText().length() == 0)
         {
           showStatus("First Name field is empty");  //  Diplays error message in status area
           tf[0].getTextField().requestFocus();  //  Places focus in JTextField
           return false;
         }
         else if(tf[1].getText()  == null || tf[1].getText().length() == 0)
         {
           showStatus("Last Name field is empty");  
           tf[1].getTextField().requestFocus();  
           return false;
         }
         else if(tf[2].getText()  == null || tf[2].getText().length() == 0)
         {
           showStatus("Employee ID field is empty"); 
           tf[2].getTextField().requestFocus(); 
           return false;
         }
         else if(tf[3].getText()  == null || tf[3].getText().length() == 0)
         {
           showStatus("(a)Salried: Weekly Salary field is empty"); 
           tf[3].getTextField().requestFocus(); 
           return false;
         }       
         else        
         return true;
       }

       if(i == 0 && i2 == 2)  // Checks Hourly worker
       {
         if(tf[0].getText()  == null || tf[0].getText().length() == 0)
         {
           showStatus("First Name field is empty");  
           tf[0].getTextField().requestFocus();  
           return false;
         }
         else if(tf[1].getText()  == null || tf[1].getText().length() == 0)
         {
           showStatus("Last Name field is empty");  
           tf[1].getTextField().requestFocus();  
           return false;
         }
         else if(tf[2].getText()  == null || tf[2].getText().length() == 0)
         {
           showStatus("Employee ID field is empty"); 
           tf[2].getTextField().requestFocus(); 
           return false;
         }
         else        
         return true;
       }

       if(i == 4 && i2 == 5)  // Checks Hourly worker the second time
       {
         if(tf[4].getText()  == null || tf[4].getText().length() == 0)
         {
           showStatus("(b1) Hourly 1: Rate Per Hour field is empty");  
           tf[5].getTextField().requestFocus(); 
           return false;
         }
         else if(tf[5].getText()  == null || tf[5].getText().length() == 0)
         {
           showStatus("(b2) Hourly 2: Hours Worked field is empty");  
           tf[5].getTextField().requestFocus();  
           return false;
         }
         else        
         return true;
       }

       if(i == 0 && i2 == 2)  // Checks Commissioned worker
       {
         if(tf[0].getText()  == null || tf[0].getText().length() == 0)
         {
           showStatus("First Name field is empty"); 
           tf[0].getTextField().requestFocus();
           return false;
         }
         else if(tf[1].getText()  == null || tf[1].getText().length() == 0)
         {
           showStatus("Last Name field is empty");  
           tf[1].getTextField().requestFocus();  
           return false;
         }
         else if(tf[2].getText()  == null || tf[2].getText().length() == 0)
         {
           showStatus("Employee ID field is empty"); 
           tf[2].getTextField().requestFocus(); 
           return false;
         }       
         else      
         return true;
       }

       if(i == 6 && i2 == 7)  // Checks Commissioned second time
       {
         if(tf[6].getText()  == null || tf[6].getText().length() == 0)
         {
           showStatus("(c1)Commissioned: Rate field is empty"); 
           tf[0].getTextField().requestFocus();
           return false;
         }
         else if(tf[7].getText()  == null || tf[7].getText().length() == 0)
         {
           showStatus("(c2)Commissioned: Ratefield is empty");  
           tf[1].getTextField().requestFocus();  
           return false;
         }     
         else       
         return true;
       } 
       return false;

      }


     private boolean fieldsEmpty(int i, int i2, String[] a)
     {
       if(i == 4 && i2 == 7) // checks salaried
       { 
         for (int index = 4; index <= 7; index++)
         {
           if(tf[index].getText().length() != 0)
           {
            showStatus( a[index] + " should be empty");  //  Diplays error message in status area
            tf[index].getTextField().requestFocus();  //  Places focus in JTextField
            return true;
           }
           else return false;
         } // end for
       }  // end if

       if (i == 3 && i2 == 3) // checks hourly first time
       {
         if(tf[3].getText().length() != 0)
         {
          showStatus(a[3] + " field should be empty");  
          tf[3].getTextField().requestFocus();  
          return true;
         }
       } // end if

       if(i == 6 && i2 == 7) // checks hourly second time
       {      
         for (int index = 6; index <= 7; index++)
         {
           if(tf[index].getText().length() != 0)
           {
            showStatus(a[index] + " field should be empty"); 
            tf[index].getTextField().requestFocus();  
            return true;
           }

         } // end for
       }  // end if

       if(i == 3 && i2 == 5) // checks commissioned  
       {      
         for (int index = 3; index <= 5; index++)
         {
           if(tf[index].getText().length() != 0)
           {
            showStatus(a[index] + " field should be empty"); 
            tf[index].getTextField().requestFocus();  
            return true;
           }

         } // end for
       }  // end if

       return false;
     }

     public void actionPerformed(ActionEvent e)
     {
       showStatus("");

       if (e.getSource() == cl)  //  Executes clear button is clicked
       {
         for (int i = 0; i < FIELDS; i++)
         {
           tf[i].getTextField().setText("");
           tf[0].getTextField().requestFocus(); 
         }
        }  //  End clear if    

       if (e.getSource() != cl)  
       {

         if(e.getSource() == sd)  // checks for salaried employee
         {
           showStatus("Salaried");
           fieldsExist(0,3);  
           fieldsEmpty(4,7, s);
           ta.append("lol");  
         }  // end salaried

         if(e.getSource() == hr)  // checks for hourly employee
         {
           showStatus("Hourly");
           fieldsExist(0,2);  
           fieldsExist(4,5);  
           fieldsEmpty(3,3, s);  
           fieldsEmpty(6,7, s);  
         }  // end hourly

         if(e.getSource() == cm)  // checks for commissioned employee
         {
           showStatus("Commissioned");
           fieldsExist(0,2);  
           fieldsExist(6,7);  
           fieldsEmpty(3,5, s);  
         }  // end commisssioned   
       } // end if
     }  // End of actionPerformed

}

这是Person类

public abstract class Person  
{
  private int i;  // private instance variables

  public Person(int i)  // one constructor
  {


  }




  public String toString() {return ??? ;}  // toString method

}

1 个答案:

答案 0 :(得分:0)

看起来你的Person类需要像firstName和lastName这样的String变量。这就是我用于我的人类课程的内容:

public class Person extends EmployeesApplet {
    private int i;  // private instance variables
    private String firstName;
    private String lastName;

    public Person(int i)  // one constructor
    {
        firstName = "";
        lastName = "";
    }

    public String getFirstName() 
    {
         return firstName;
    }

    public void setFirstName(String firstName) 
    {
         this.firstName = firstName;
    }

    public String getLastName() 
    {
         return lastName;
    }

    public void setLastName(String lastName) 
    {
         this.lastName = lastName;
    }

    public String toString() 
    {
        return getfirstName() + " " + getLastName();
    }  // toString method

}