public class Inventory {
private static final class InventoryWindowExtension extends InventoryWindow {
JFrame frame = new JFrame();
}
String name;
ArrayList<Item> items;
ArrayList<Person> people;
public Inventory(String name) {
this.name = name;
items = new ArrayList<Item>();
people = new ArrayList<Person>();
}
public String getName() {
return name;
}
public ArrayList<Item> getItems() {
return items;
}
public ArrayList<Person> getPeople() {
return people;
}
public void addItem(Item i1) {
this.items.add(i1);
}
public void removeItem(Item i1) {
this.items.remove(i1);
}
public void addPerson(Person p1) {
this.people.add(p1);
}
public void removePerson(Person p1){
this.people.remove(p1);
}
public boolean checkOut(Item i1, Person p1) {
int itemsOut = this.getItemsForPerson(p1).size();
if ((i1.getPerson() == null) &&
itemsOut < p1.getMaxItems()){
i1.setPerson(p1);
return true;
}
else{
return false;
}
}
public boolean CheckIn(Item i1) {
if (i1.getPerson() != null){
i1.setPerson(null);
return true;
}
else{
return false;
}
}
public ArrayList<Item> getItemsForPerson(Person p1) {
ArrayList<Item> result = new ArrayList<Item>();
for (Item aItem : this.getItems()) {
if ((aItem.getPerson() != null) &&
(aItem.getPerson().getName()
.equals(p1.getName())))
{
result.add(aItem);
}
}
return result;
}
public ArrayList<Item> getAvailableItems() {
ArrayList<Item> result = new ArrayList<Item>();
for (Item aItem : this.getItems()) {
if (aItem.getPerson() == null){
result.add(aItem);
}
}
return result;
}
public ArrayList<Item> getUnavailableItems() {
ArrayList<Item> result = new ArrayList<Item>();
for (Item aItem : this.getItems()) {
if (aItem.getPerson() != null){
result.add(aItem);
}
}
return result;
}
public String toString() {
return this.getName() + ": "+
this.getItems().size() + " items; " +
this.getPeople().size() + " people.";
}
public static void main(String[] args) {
//creating new items
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e) {
}
Inventory testInventory = new Inventory("Test Drive Inventory");
Item i1 = new Item("Camera");
Item i2 = new Item("Headphones");
Item i3 = new Item("Microphones");
//creating new people
Person jim = new Person();
Person sue = new Person();
Person obo = new Person();
jim.setName("Jim");
sue.setName("Sue");
obo.setName("Bob");
//adding them
testInventory.addItem(i1);
testInventory.addItem(i2);
testInventory.addItem(i3);
testInventory.addPerson(obo);
testInventory.addPerson(jim);
testInventory.addPerson(sue);
//printing status
System.out.println("Just created new inventory");
testInventory.printStatus();
//checking items out
System.out.println("Check out Camera to Sue");
testInventory.checkOut(i1, sue);
testInventory.printStatus();
System.out.println("Do some more stuff");
testInventory.CheckIn(i1);
testInventory.checkOut(i2, jim);
testInventory.printStatus();
}
private void printStatus() {
System.out
.println("Status Report of MyInventory \n" +
this.toString());
for (Item thisItem : this.getItems()) {
System.out.println(thisItem);
}
for (Person p : this.getPeople()) {
int count = this.getItemsForPerson(p).size();
System.out.println(p + " (has " + count
+ " of my items)");
}
System.out.println("Items Available: "
+ this.getAvailableItems().size());
System.out.println("--- End of Status Report ---");
}
public void InventoryWindow() {
JFrame inventory = new JFrame();
FlowLayout myLayout = new FlowLayout();
inventory.setLayout(myLayout);
JPanel myPanel = (JPanel)inventory.getContentPane();
inventory.setContentPane(myPanel);
GridLayout layout = new GridLayout(3, 3, 5, 5);
myPanel.setBackground(new Color(127,255,212));
JLabel label = new JLabel();
inventory.add(label);
inventory.setSize(600, 500);
inventory.setVisible(true);
inventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inventory.setLocation(400, 200);
inventory.setTitle("Inventory of Equipment");
inventory.setDefaultLookAndFeelDecorated(true);
inventory.setResizable(false);
}
}
这就是代码。我需要将System.out.println重定向到JFrame清单。 如果你们能给我一个答案或一个例子,那将是非常有帮助的。我看了几篇帖子,但我不知道如何调整它们以适应这段代码。