如何使用循环从帧中的数组显示多个面板

时间:2014-02-09 09:06:38

标签: java arrays layout jframe jpanel

我从未擅长使用java图形界面。我正在创建一个程序,它将创建一个“联系人”数组并将它们显示到一个框架。我可以正确添加,删除和排序数组。我只是无法让它显示在屏幕上。我遇到了不正确显示的几种变体。无论我做什么,我似乎无法让“联系”显示正确。我的意思是,当运行这个程序时,应该出现一个jframe,其中包含4个带图片和文本信息的联系人。然后用户可以删除其中一个联系人,程序应显示一个新的按字母顺序排序的列表,而不删除已删除的联系人。

我遇到的问题是让我的联系人在jframe中显示。当我运行该程序时,联系人会出现,但只有在主方法中,我每次添加新联系人时都会调用我的show函数。否则jframe只显示添加的最后一个联系人,直到程序被告知要删除哪个联系人。然后jframe显示太多的联系人。

请帮我弄清楚我做错了什么。我为我糟糕的格式道歉。这是我第一次使用这个网站。

/*
blake yacavone
advanced programming
project 1
*/

import java.util.Scanner;

public class project1Tester
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);

      contactArrayList list = new contactArrayList();

      contact beatle1 = new contact("Paul", 1284, "Beverly Hills", "California", 90209, "paul.jpg", "vocals");
      list.addContact(beatle1);
      list.showContacts();

      contact beatle2 = new contact("Pete", 1284, "Beverly Hills", "California", 90209, "pete.jpg", "drummer");
      list.addContact(beatle2);
      list.showContacts();

      contact beatle3 = new contact("John",  1284, "Beverly Hills", "California", 90209, "john.jpg", "vocals");
      list.addContact(beatle3);
      list.showContacts();

      contact beatle4 = new contact("George", 1284, "Beverly Hills", "California", 90209, "george.jpg", "vocals");
      list.addContact(beatle4);
      list.showContacts();

      System.out.println("please enter the identification number of the contact you would like deleted ");
      list.delete(input.nextInt());
      list.showContacts();

      contact beatle5 = new contact("Ringo", 1284, "Beverly Hills", "California", 90209, "ringo.jpg", "drummer");
      list.addContact(beatle5);
      list.showContacts();

      list.sort();
      list.showContacts();
   }
}



/*
blake yacavone
advanced programming
project 1
*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.ImageIcon;

public class contactArrayList extends JFrame
{
   public contact[] contactList;                                           //declares an array of contacts
   JFrame contactViewer;                                                   //creates a frame to hold my panel
   JPanel contactGrid;                                                     //creates a panel to hold my contact information
   int contactCount = 1;                                                   //keeps track of / assigns ID numbers to each contact that is created

   contactArrayList()                                                      //constructor for a contact array list
   {
      this.contactList = new contact[0];                                   //creates an array of contacts of inital size 0

      this.contactViewer = new contactListFrame();
      contactViewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      this.contactGrid = new JPanel();
        contactGrid.setBackground(Color.WHITE);

      contactViewer.add(contactGrid);
      contactViewer.setVisible(true);
   }

   int checkList(String name)                                              //walks through the array checking for a contact of a specified name
   {
      if(contactList.length == 1)                                          //if the length of the contact list is one than its empty and an element should be added
      {
         return 1;
      }

      for(int i = 0; i < contactList.length; i++)
      {
         if(name.equals(contactList[i].getName()))
         {
            return 0;
         }
      }
      return 1;
   }

   void addContact(contact person)
   {                                                                       //calls check and if check returns true it adds the contact to the contactList
      if ( (checkList(person.getName())) > 0 )
      {
         resize(1);                                                        //calls resize method to increase the size of the array
         person.setID(contactCount++);                                     //gives the contact their id number
         contactList[contactList.length-1] = person;                       //adds the contact to the end of the array
      }
      else
      {
         System.out.println("ERROR, the requested contact is already in the list");
      }

   }

   void delete(int ID)                                                     //deletes a contact specified by name
   {
      for(int i = 0; i < contactList.length-1; i++)                        //runs through the array searching for the specified name
      {
         if(ID == contactList[i].getID())                                  //if the ID i was given matches the one im looking at in the 
         {                                                                 //contact list AND i+1 does not equal the end of the array, 
            contact temp = contactList[i];                                 //swap the entry marked for deletion with the next entry
            contactList[i] = contactList[i+1];                             //in the array until entry marked for deletion is at the end
            contactList[i+1] = temp;
         }
      }
      resize(-1);
   }

   void resize(int direction)
   {
      if(direction > 0)                                                    //if direction is a positive number increase the array size
      {
         contact[] tempList = new contact[contactList.length + 1];         //creates a temporary contact list array

         for(int i = 0; i < contactList.length; i++)                       //runs through the contact list array and copies its data into the temp list
         {
            tempList[i] = contactList[i];
         }

         contactList = new contact[contactList.length + 1];                //re initalizes contact list with a new size

         for(int i = 0; i < contactList.length; i++)                       //runs through the temp array and copies its data into the new bigger contact list
         {
            contactList[i] = tempList[i];
         }
      }
      else if(direction < 0)                                               //if direction is a negative number decrease the array size
      {
         contact[] tempList = new contact[contactList.length];             //creates a temporary contact list array

         for(int i = 0; i < contactList.length; i++)                       //runs through the contact list array and copies its data into the temp list
         {
            tempList[i] = contactList[i];
         }

         contactList = new contact[contactList.length - 1];                //re initalizes contact list with a new size

         for(int i = 0; i < contactList.length; i++)                       //runs through the contact list array and copies its data into the temp list
         {
            contactList[i] = tempList[i];
         }
      }
   }

   void sort()
   {

      for(int i = 0; i < contactList.length-1; i++)
      {
         for(int j = 1; j < contactList.length; j++)
         {
            if( (contactList[i].getName().charAt(0)) < (contactList[j].getName().charAt(0)) );
            {
               contact temp = contactList[i];
               contactList[i] = contactList[j];
               contactList[j] = temp;
            }
         }
      }
   }



   void showContacts()
   {
      JLabel contactInfo = new JLabel();


      for(int i = 0; i < contactList.length; i ++)
      {
         contactInfo.setText( getText(contactList[i]) );

         ImageIcon photo = new ImageIcon(getPicture(contactList[i]));
         contactInfo.setIcon(photo);

         contactGrid.add(contactInfo);
         contactGrid.setVisible(true);
      }

      contactViewer.add(contactGrid);
      //contactViewer.pack();
      contactViewer.setVisible(true);
   }









   String getPicture(contact person)
   {
      return (person.getPicture());
   }

   int getID(contact person)
   {
      return (person.getID());
   }

   String getName(contact person)
   {
      return (person.getName());
   }

   int getAddress(contact person)
   {
      return (person.getAddress());
   }

   String getCity(contact person)
   {
      return (person.getCity());
   }

   String getState(contact person)
   {
      return (person.getState());
   }

   int getZipCode(contact person)
   {
      return (person.getZipCode());
   }

   String getComment(contact person)
   {
      return (person.getComment());
   }

   String getText(contact person)
   {
      String text = Integer.toString(getID(person)) + " \n " + getName(person) + " \n " + Integer.toString(getAddress(person)) + " \n " + getCity(person) + " \n " + getState(person) + " \n " + Integer.toString(getZipCode(person)) + " \n " + getComment(person);
      return(text);
   }
}










    /* 
   Blake yacavone
*/
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class contactListFrame extends JFrame
{
    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 500;
    private static final int FRAME_OFFSET = 15;


    public contactListFrame()
    {
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

}







    /*
blake yacavone
advanced programming
project 1
*/

public class contact
{
   private int ID = 0;
   private String name;
   private int address;
   private String city;
   private String state;
   private int zipCode;
   private String picture;//picture is a string with the file name of the picture to be displayed with the Contact.
   private String comment;

   public contact(String name, int address, String city, String state, int zipCode, String picture,String comment)
   {
       this.name = name;
       this.address = address;
       this.city = city;
       this.state = state;
       this.zipCode = zipCode;
       this.picture = picture;
       this.comment = comment;
   }

   public contact()
   {
      this.name = "problem name";
      this.address = 1134;
      this.city = "problem city";
      this.state = "problem state";
      this.zipCode = 1134;
      this.picture = "problem picture";
      this.comment = "problem comment";
   }

   public void setID(int ID)
   {
      this.ID = ID;
   }
   public String getName()
   {
      return this.name;
   }

   public int getAddress()
   {
      return this.address;
   }

   public String getCity()
   {
      return this.city;
   }

   public String getState()
   {
      return this.state;
   }

   public int getZipCode()
   {
      return this.zipCode;
   }

   public String getPicture()
   {
      return this.picture;
   }

   public String getComment()
   {
      return this.comment;
   }

   public int getID()
   {
      return this.ID;
   }
}

1 个答案:

答案 0 :(得分:0)

仅在屏幕上显示?

我已将所有类放在runnable类(具有main方法)上,并将转换后的类转换为静态类以访问该静态方法

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.util.Scanner;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class project1Tester
{
 public static class contactListFrame extends JFrame
{
    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 500;
    private static final int FRAME_OFFSET = 15;


    public contactListFrame()
    {
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

}








  public static class contact
{
   private int ID = 0;
   private String name;
   private int address;
   private String city;
   private String state;
   private int zipCode;
   private String picture;//picture is a string with the file name of the picture to be displayed with the Contact.
   private String comment;

   public contact(String name, int address, String city, String state, int zipCode, String picture,String comment)
   {
       this.name = name;
       this.address = address;
       this.city = city;
       this.state = state;
       this.zipCode = zipCode;
       this.picture = picture;
       this.comment = comment;
   }

   public  contact()
   {
      this.name = "problem name";
      this.address = 1134;
      this.city = "problem city";
      this.state = "problem state";
      this.zipCode = 1134;
      this.picture = "problem picture";
      this.comment = "problem comment";
   }

   public void setID(int ID)
   {
      this.ID = ID;
   }
   public String getName()
   {
      return this.name;
   }

   public int getAddress()
   {
      return this.address;
   }

   public String getCity()
   {
      return this.city;
   }

   public String getState()
   {
      return this.state;
   }

   public int getZipCode()
   {
      return this.zipCode;
   }

   public String getPicture()
   {
      return this.picture;
   }

   public String getComment()
   {
      return this.comment;
   }

   public int getID()
   {
      return this.ID;
   }
}
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);

      contactArrayList list = new contactArrayList();

      contact beatle1 = new contact("Paul", 1284, "Beverly Hills", "California", 90209, "paul.jpg", "vocals");
      list.addContact(beatle1);
      list.showContacts();

      contact beatle2 = new contact("Pete", 1284, "Beverly Hills", "California", 90209, "pete.jpg", "drummer");
      list.addContact(beatle2);
      list.showContacts();

      contact beatle3 = new contact("John",  1284, "Beverly Hills", "California", 90209, "john.jpg", "vocals");
      list.addContact(beatle3);
      list.showContacts();

      contact beatle4 = new contact("George", 1284, "Beverly Hills", "California", 90209, "george.jpg", "vocals");
      list.addContact(beatle4);
      list.showContacts();

      System.out.println("please enter the identification number of the contact you would like deleted ");
      list.delete(input.nextInt());
      list.showContacts();

      contact beatle5 = new contact("Ringo", 1284, "Beverly Hills", "California", 90209, "ringo.jpg", "drummer");
      list.addContact(beatle5);
      list.showContacts();

      list.sort();
      list.showContacts();
   }
   public static class contactArrayList extends JFrame
{
   public contact[] contactList;                                           //declares an array of contacts
   JFrame contactViewer;                                                   //creates a frame to hold my panel
   JPanel contactGrid;                                                     //creates a panel to hold my contact information
   int contactCount = 1;                                                   //keeps track of / assigns ID numbers to each contact that is created

   contactArrayList()                                                      //constructor for a contact array list
   {
      this.contactList = new contact[0];                                   //creates an array of contacts of inital size 0

      this.contactViewer = new contactListFrame();
      contactViewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      this.contactGrid = new JPanel();
        contactGrid.setBackground(Color.WHITE);

      contactViewer.add(contactGrid);
      contactViewer.setVisible(true);
   }

   int checkList(String name)                                              //walks through the array checking for a contact of a specified name
   {
      if(contactList.length == 1)                                          //if the length of the contact list is one than its empty and an element should be added
      {
         return 1;
      }

      for(int i = 0; i < contactList.length; i++)
      {
         if(name.equals(contactList[i].getName()))
         {
            return 0;
         }
      }
      return 1;
   }

   void addContact(contact person)
   {                                                                       //calls check and if check returns true it adds the contact to the contactList
      if ( (checkList(person.getName())) > 0 )
      {
         resize(1);                                                        //calls resize method to increase the size of the array
         person.setID(contactCount++);                                     //gives the contact their id number
         contactList[contactList.length-1] = person;                       //adds the contact to the end of the array
      }
      else
      {
         System.out.println("ERROR, the requested contact is already in the list");
      }

   }

   void delete(int ID)                                                     //deletes a contact specified by name
   {
      for(int i = 0; i < contactList.length-1; i++)                        //runs through the array searching for the specified name
      {
         if(ID == contactList[i].getID())                                  //if the ID i was given matches the one im looking at in the
         {                                                                 //contact list AND i+1 does not equal the end of the array,
            contact temp = contactList[i];                                 //swap the entry marked for deletion with the next entry
            contactList[i] = contactList[i+1];                             //in the array until entry marked for deletion is at the end
            contactList[i+1] = temp;
         }
      }
      resize(-1);
   }

   void resize(int direction)
   {
      if(direction > 0)                                                    //if direction is a positive number increase the array size
      {
         contact[] tempList = new contact[contactList.length + 1];         //creates a temporary contact list array

         for(int i = 0; i < contactList.length; i++)                       //runs through the contact list array and copies its data into the temp list
         {
            tempList[i] = contactList[i];
         }

         contactList = new contact[contactList.length + 1];                //re initalizes contact list with a new size

         for(int i = 0; i < contactList.length; i++)                       //runs through the temp array and copies its data into the new bigger contact list
         {
            contactList[i] = tempList[i];
         }
      }
      else if(direction < 0)                                               //if direction is a negative number decrease the array size
      {
         contact[] tempList = new contact[contactList.length];             //creates a temporary contact list array

         for(int i = 0; i < contactList.length; i++)                       //runs through the contact list array and copies its data into the temp list
         {
            tempList[i] = contactList[i];
         }

         contactList = new contact[contactList.length - 1];                //re initalizes contact list with a new size

         for(int i = 0; i < contactList.length; i++)                       //runs through the contact list array and copies its data into the temp list
         {
            contactList[i] = tempList[i];
         }
      }
   }

   void sort()
   {

      for(int i = 0; i < contactList.length-1; i++)
      {
         for(int j = 1; j < contactList.length; j++)
         {
            if( (contactList[i].getName().charAt(0)) < (contactList[j].getName().charAt(0)) );
            {
               contact temp = contactList[i];
               contactList[i] = contactList[j];
               contactList[j] = temp;
            }
         }
      }
   }



   void showContacts()
   {
      JLabel contactInfo = new JLabel();


      for(int i = 0; i < contactList.length; i ++)
      {
         contactInfo.setText( getText(contactList[i]) );

         ImageIcon photo = new ImageIcon(getPicture(contactList[i]));
         contactInfo.setIcon(photo);

         contactGrid.add(contactInfo);
         contactGrid.setVisible(true);
      }

      contactViewer.add(contactGrid);
      //contactViewer.pack();
      contactViewer.setVisible(true);
   }









   String getPicture(contact person)
   {
      return (person.getPicture());
   }

   int getID(contact person)
   {
      return (person.getID());
   }

   String getName(contact person)
   {
      return (person.getName());
   }

   int getAddress(contact person)
   {
      return (person.getAddress());
   }

   String getCity(contact person)
   {
      return (person.getCity());
   }

   String getState(contact person)
   {
      return (person.getState());
   }

   int getZipCode(contact person)
   {
      return (person.getZipCode());
   }

   String getComment(contact person)
   {
      return (person.getComment());
   }

   String getText(contact person)
   {
      String text = Integer.toString(getID(person)) + " \n " + getName(person) + " \n " + Integer.toString(getAddress(person)) + " \n " + getCity(person) + " \n " + getState(person) + " \n " + Integer.toString(getZipCode(person)) + " \n " + getComment(person);
      return(text);
   }
}

}