gpt4 book ai didi

java - Java中根据模型- View - Controller 制作 View

转载 作者:行者123 更新时间:2023-12-02 04:45:23 25 4
gpt4 key购买 nike

我有一个应用程序,其中包含部门名称列表 - 当用户单击其中一个部门名称时,应出现显示其详细信息的 JPanel。我在模型专用类中加载了一个 Department 对象,但是将整个对象传递到自定义 DepartmentDetailsPanel 构造函数并从中提取数据以显示(例如名称、用户成员列表等)是一种不好的做法吗?

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;
}

}

最佳答案

只要除了获取字段值之外不执行任何操作就可以。 View 必须使用模型,否则如何显示模型?

关于java - Java中根据模型- View - Controller 制作 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com