【发布时间】:2014-02-09 09:06:38
【问题描述】:
我从来都不擅长使用 java 图形界面。我正在创建一个程序,它将创建一个“联系人”数组并将它们显示到一个框架中。我可以正确地添加、删除和排序数组。我只是无法让它正确显示在屏幕上。我遇到了几种不正确显示的变体。无论我做什么,我似乎都无法正确显示“联系人”。我的意思是,当这个程序运行时,应该会出现一个 jframe,其中包含 4 个带有图片和文本信息的联系人。然后用户可以删除其中一个联系人,程序应显示一个新的按字母排序的列表,其中不包含已删除的联系人。
我遇到的问题是让我的联系人显示在 jframe 中。当我运行程序时,联系人会出现,但只有在每次添加新联系人时我都会在主方法中调用我的显示函数。否则 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;
}
}
【问题讨论】:
-
首先你没有一个主方法写你的主方法并在里面从你的类中创建一个实例
-
“显示正确”是什么意思?
-
并使用 Java 命名约定。类名以大写字母开头
-
@ElhadiMamoun:在“project1Tester”类中
-
并且不要将 GUI 与控制台混合使用。
标签: java arrays layout jframe jpanel