我有一個應用程序,其中包含部門名稱列表 - 當用戶單擊其中一個顯示其詳細信息的JPanel時應該出現。我在我的模型專用類中加載了一個Department對象,但將整個對象傳遞到自定義的DepartmentDetailsPanel構造函數並提取數據以在其中顯示(例如名稱,用戶成員列表等...)是不好的做法?根據Java中的模型 - 視圖 - 控制器製作視圖
public class DepartmentDetailsPanel extends JPanel {
private ClassLoader cl = this.getClass().getClassLoader();
private JPanel iconAndName = new JPanel();
private JLabel icon;
private JLabel name;
private JPanel body = new JPanel();
private JPanel basicInfo = new JPanel();
private JLabel headLabel = new JLabel(Constants.head_name_lable);
private JLabel managerLabel = new JLabel(Constants.manager_name_label);
private JLabel typeLabel = new JLabel(Constants.type_label);
private JLabel head;
private JLabel manager;
private JLabel type;
private JPanel membersPanel = new JPanel();
private JLabel usersLabel = new JLabel(Constants.members_label);
private JList members;
private JPanel memberButtons = new JPanel();
private JButton removeButton = new JButton(Constants.remove);
private JButton addButton = new JButton(Constants.add);
public DepartmentDetailsPanel(Department d) {
setLayout(new BorderLayout());
setBorder(new EmptyBorder(10, 10, 10, 10));
iconAndName.setLayout(new FlowLayout(FlowLayout.LEFT));
int dType = d.getType();
if (dType == 1)
icon = new JLabel(new ImageIcon(cl.getResource("leader.png")));
if (dType == 2)
icon = new JLabel(new ImageIcon(cl.getResource("creative.png")));
if (dType == 3)
icon = new JLabel(new ImageIcon(cl.getResource("finance.png")));
name = new JLabel(d.getName());
iconAndName.add(icon);
iconAndName.add(name);
add(iconAndName, BorderLayout.NORTH);
basicInfo.setLayout(new GridLayout(3, 2));
basicInfo.add(typeLabel);
type = new JLabel(Constants.department_types[dType-1]);
basicInfo.add(type);
basicInfo.add(headLabel);
head = new JLabel(d.getHeadName());
basicInfo.add(head);
basicInfo.add(managerLabel);
manager = new JLabel(d.getManagerName());
basicInfo.add(manager);
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
body.add(basicInfo);
membersPanel.setLayout(new BorderLayout());
membersPanel.add(usersLabel, BorderLayout.NORTH);
JComponent memberList = makeList(extractMembers(d), members);
membersPanel.add(memberList, BorderLayout.CENTER);
memberButtons.setLayout(new BoxLayout(memberButtons, BoxLayout.Y_AXIS));
memberButtons.add(addButton);
memberButtons.add(removeButton);
membersPanel.add(memberButtons, BorderLayout.EAST);
body.add(membersPanel);
add(body, BorderLayout.CENTER);
}
private JComponent makeList(String[] data, JList list) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
panel.setBackground(Constants.lightGrey);
if (data == null){
JLabel message = new JLabel(Constants.nothing_to_view);
panel.add(message);
} else {
list = new JList(data);
list.setVisibleRowCount(10);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setBackground(Constants.lightGrey);
panel.add(new JScrollPane(list));
}
return panel;
}
private String[] extractMembers(Department d){
String[] members = null;
List<User> list = d.getMembers();
if (list != null || list.size()==0){
members = new String[list.size()];
int i = 0;
for (User u: list){
members[i] = u.getName() +" "+ u.getLastname();
i++;
}
}
return members;
}
}
在我看來這是一個不好的做法。一個構造函數只能用來初始化你的實例變量。如果你需要做更多的事情(就像你的情況),你應該考慮創建一個[工廠方法](http://stackoverflow.com/questions/929021/what-are-static-factory-methods-in-java)。 – Tom