1

我是个大菜鸟。我刚刚完成了一本java教科书,现在我正在制作一个游戏作为学习实验。我很难过......这是我的问题:

我从 Person 类创建了六个对象。

每个对象都有一个布尔变量,自动设置为 false。

我可以从组合框中选择每个 Person 对象并显示有关它们的信息,但我只能显示用于将对象链接到组合框的信息(如:person1.name 允许我在组合框,并允许我在其他地方显示名称)。

我希望能够更改每个对象中的其他实例变量。我知道如何创建一种方法来更改每个对象,但我必须创建六个方法,其中一个专门针对每个特定对象。

我想更改用户从组合框中选择的对象的布尔实例变量。我以为我可以用一组对象来做到这一点,但我一直未能设置该数组。数组是正确的方法吗?还是有其他方法?

4

2 回答 2

3
  • Your JComboBox appears to be holding String objects that correspond to each Person's name, i.e., JComboBox<String>.
  • Instead, the JComboBox should hold a collection Person objects, JComboBox<Person>.
  • You can control how the Person object is displayed by giving it a clean toString() method that returns the Person's name.
  • Even better would be to give the JComboBox a custom cell renderer that properly display's the Person's name.
  • Give the JComboBox an ActionListener to listen for the user's selection, get the selected object via the JComboBox's getSelectedItem(), and then call the appropriate setter method to set the selected Person's boolean field.

You state:

I know how to make a method to change each object, but I'd have to make six methods, one which specifically refers to each specific object.

Not so. A single method would work just fine, one that can work with any number of Person objects.

If any of my suggestions are unclear, please let me know via a comment. If you've got more specific questions, please edit your original question, but also consider posting pertinent code as well.


For example:

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PersonTest {
   protected static final Color SELECTED_COLOR = Color.red;

   @SuppressWarnings("serial")
   private static void createAndShowGui() {
      Person[] people = { new Person("Duck", "Donald"),
            new Person("Mouse", "Mickey"), new Person("Trump", "Donald"),
            new Person("Bunny", "Easter"), new Person("Claus", "Santa"),
            new Person("Kringle", "Chris")};

      final JComboBox<Person> combo = new JComboBox<>(people);
      final JList<Person> personJList = new JList<>(people);
      combo.setSelectedIndex(-1);

      JPanel mainPanel = new JPanel();
      mainPanel.add(new JScrollPane(combo));
      mainPanel.add(new JScrollPane(personJList));

      combo.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent evt) {
            Person selectedPerson = (Person) combo.getSelectedItem();
            selectedPerson.setCheck(!selectedPerson.isCheck());
            personJList.repaint();
         }
      });

      personJList.setCellRenderer(new DefaultListCellRenderer() {
            @Override
         public Component getListCellRendererComponent(JList<?> list,
               Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Person person = (Person) value;
            Component component = super.getListCellRendererComponent(list,
                  value, index, isSelected, cellHasFocus);
            if (person != null) {
               if (person.isCheck()) {
                  component.setForeground(SELECTED_COLOR);
               } else {
                  component.setForeground(null);
               }
            }
            return component;
         }
      });

      JFrame frame = new JFrame("PersonTest");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Person {
   private String firstName;
   private String lastName;
   private boolean check;

   public Person(String lastName, String firstName) {
      this.lastName = lastName;
      this.firstName = firstName;
      this.check = false;
   }

   public boolean isCheck() {
      return check;
   }

   public void setCheck(boolean check) {
      this.check = check;
   }

   public String getFirstName() {
      return firstName;
   }

   public String getLastName() {
      return lastName;
   }

   @Override
   public String toString() {
      return String.format("%s,  %s", lastName, firstName);
   }

}
于 2013-08-10T20:50:58.280 回答
3

假设您JComboBox实际上包含对Person对象的引用,您应该提供合适的 getter 和 setter,它们允许您访问对象中包含的值。

例如...

public class Person {
    private String name;

    public String getName() {
        return name;
    }
}

创建一个只读Person的,名称不能更改。

但是,如果我们这样做...

public class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String value) {
        name = value;
    }
}

这使得 name 属性也可写,例如

于 2013-08-10T21:09:08.930 回答