Java 如何使用循环在帧中显示阵列中的多个面板
我从来都不擅长使用java图形界面。我正在创建一个程序,该程序将创建一个“联系人”数组,并将其显示到一个框架中。我可以正确添加、删除和排序数组。我就是不能把它显示在屏幕上。我遇到了几种不正确显示的变体。无论我做什么,我似乎都无法正确显示“联系人”。我的意思是,当这个程序运行时,应该会出现一个jframe,包含4个带有图片和文本信息的联系人。然后用户可以删除其中一个联系人,程序将显示一个新的按字母顺序排序的列表,而不显示已删除的联系人 我遇到的问题是让我的联系人显示在jframe中。当我运行程序时,只有在每次添加新联系人时,我在主方法中调用show函数时,才会显示联系人。否则,jframe只显示添加的最后一个联系人,直到程序被告知要删除哪个联系人。然后jframe会显示过多的联系人 请帮我找出我做错了什么。我为我的格式错误道歉。这是我第一次使用这个网站Java 如何使用循环在帧中显示阵列中的多个面板,java,arrays,layout,jframe,jpanel,Java,Arrays,Layout,Jframe,Jpanel,我从来都不擅长使用java图形界面。我正在创建一个程序,该程序将创建一个“联系人”数组,并将其显示到一个框架中。我可以正确添加、删除和排序数组。我就是不能把它显示在屏幕上。我遇到了几种不正确显示的变体。无论我做什么,我似乎都无法正确显示“联系人”。我的意思是,当这个程序运行时,应该会出现一个jframe,包含4个带有图片和文本信息的联系人。然后用户可以删除其中一个联系人,程序将显示一个新的按字母顺序排序的列表,而不显示已删除的联系人 我遇到的问题是让我的联系人显示在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
*/
导入java.util.Scanner;
公共类项目1测试
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
contactArrayList=新建contactArrayList();
联系人beatle1=新联系人(“Paul”,1284,“贝弗利山庄”,“加利福尼亚州”,90209,“Paul.jpg”,“人声”);
列表。添加联系人(beatle1);
list.showContacts();
联系人beatle2=新联系人(“Pete”,1284,“贝弗利山庄”,“加利福尼亚州”,90209,“Pete.jpg”,“鼓手”);
列表。添加联系人(beatle2);
list.showContacts();
联系人beatle3=新联系人(“约翰”,1284,“贝弗利山庄”,“加利福尼亚州”,90209,“John.jpg”,“人声”);
列表。添加联系人(beatle3);
list.showContacts();
联系人beatle4=新联系人(“乔治”,1284,“贝弗利山”,“加利福尼亚”,90209,“乔治.jpg”,“人声”);
列表。添加联系人(beatle4);
list.showContacts();
System.out.println(“请输入您要删除的联系人的标识号”);
list.delete(input.nextInt());
list.showContacts();
联系人beatle5=新联系人(“Ringo”,1284,“贝弗利山庄”,“加利福尼亚州”,90209,“Ringo.jpg”,“鼓手”);
列表。添加联系人(beatle5);
list.showContacts();
list.sort();
list.showContacts();
}
}
/*
布莱克·亚卡沃内
高级编程
项目1
*/
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入java.awt.*;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入javax.swing.JComponent;
导入java.awt.Toolkit;
导入java.awt.Image;
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入javax.swing.JPanel;
导入javax.swing.ImageIcon;
公共类contactArrayList扩展了JFrame
{
public contact[]contactList;//声明联系人数组
JFrame contactViewer;//创建一个框架来容纳我的面板
JPanel contactGrid;//创建一个面板来保存我的联系信息
int contactCount=1;//跟踪/为创建的每个联系人分配ID号
contactArrayList()//联系人数组列表的构造函数
{
this.contactList=新联系人[0];//创建初始大小为0的联系人数组
this.contactViewer=新的contactListFrame();
contactViewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.contactGrid=new JPanel();
contactGrid.setBackground(颜色:白色);
contactViewer.add(contactGrid);
contactViewer.setVisible(true);
}
int checkList(String name)//遍历数组,检查指定名称的联系人
{
if(contactList.length==1)//如果联系人列表的长度比其空长度大一,则应添加一个元素
{
返回1;
}
对于(int i=0;i0)
{
resize(1);//调用resize方法以增加数组的大小
person.setID(contactCount++);//提供联系人的id号
contactList[contactList.length-1]=person;//将联系人添加到数组的末尾
}
其他的
{
System.out.println(“错误,请求的联系人已在列表中”);
}
}
void delete(int ID)//删除由名称指定的联系人
{
for(int i=0;iimport 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);
}
}
}