<!-- 添加人员按钮 --> <view class="add" bindtap="addUser">+添加人员</view> <!-- 下拉框 --> <view class="dropdown" wx:if="{{showDropdown}}"> <picker bindchange="selectName"> <picker-view > <picker-view-column> <view wx:for="{{nameList}}" wx:key="{{item}}">{{item}}</view> </picker-view-column> </picker-view> </picker> </view> <!-- 标签 --> <view class="tag"> <view class="tag-item" wx:for="{{selectedNames}}" wx:key="{{item}}">{{item}}<text class="close" bindtap="removeTag">×</text></view> </view>为啥点击不会显示下拉框
时间: 2023-07-19 09:57:08 浏览: 81
可能是因为 `showDropdown` 这个变量没有被正确设置。可以检查以下几点:
1. 确认 `showDropdown` 变量在小程序的 data 中被定义了,并且初始值为 false。
2. 确认 `addUser` 方法中正确地设置了 `showDropdown` 变量为 true。
3. 确认 `selectName` 方法中正确地设置了 `showDropdown` 变量为 false。
相关问题
通过面向对象思想实现一个权限管理系统(主要对用户管理)。 * (所用技术点:类、封装、构造方法、继承、多态) * 提示:一般用户<---管理员<----高级管理员,不同用户登录系统显示不不同菜单
以下是一个基本的权限管理系统的实现,包括用户、管理员和高级管理员三个角色,以及对应的菜单权限:
```java
// User类,普通用户
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void viewMenu() {
System.out.println("普通用户菜单:");
System.out.println("1. 查看个人信息");
System.out.println("2. 修改个人信息");
}
}
// Admin类,管理员
public class Admin extends User {
public Admin(String name) {
super(name);
}
@Override
public void viewMenu() {
System.out.println("管理员菜单:");
System.out.println("1. 查看个人信息");
System.out.println("2. 修改个人信息");
System.out.println("3. 查看用户列表");
System.out.println("4. 添加用户");
System.out.println("5. 删除用户");
}
}
// SuperAdmin类,高级管理员
public class SuperAdmin extends Admin {
public SuperAdmin(String name) {
super(name);
}
@Override
public void viewMenu() {
System.out.println("高级管理员菜单:");
System.out.println("1. 查看个人信息");
System.out.println("2. 修改个人信息");
System.out.println("3. 查看用户列表");
System.out.println("4. 添加用户");
System.out.println("5. 删除用户");
System.out.println("6. 修改用户权限");
}
}
// UserManager类,用户管理器
public class UserManager {
private List<User> userList;
public UserManager() {
userList = new ArrayList<>();
}
public void addUser(User user) {
userList.add(user);
}
public void removeUser(User user) {
userList.remove(user);
}
public void viewUserList() {
System.out.println("用户列表:");
for (User user : userList) {
System.out.println(user.getName());
}
}
}
// 实现示例
public class Example {
public static void main(String[] args) {
UserManager userManager = new UserManager();
User user1 = new User("张三");
User user2 = new User("李四");
Admin admin = new Admin("管理员");
SuperAdmin superAdmin = new SuperAdmin("高级管理员");
userManager.addUser(user1);
userManager.addUser(user2);
userManager.addUser(admin);
userManager.addUser(superAdmin);
// 不同用户登录系统显示不不同菜单
System.out.println(user1.getName() + " 登录系统:");
user1.viewMenu();
System.out.println(admin.getName() + " 登录系统:");
admin.viewMenu();
System.out.println(superAdmin.getName() + " 登录系统:");
superAdmin.viewMenu();
// 管理员操作用户管理器
System.out.println(admin.getName() + " 操作用户管理器:");
admin.viewUserList();
admin.removeUser(user2);
admin.viewUserList();
admin.addUser(user2);
admin.viewUserList();
// 高级管理员操作用户管理器
System.out.println(superAdmin.getName() + " 操作用户管理器:");
superAdmin.viewUserList();
superAdmin.removeUser(user1);
superAdmin.viewUserList();
superAdmin.addUser(user1);
superAdmin.viewUserList();
superAdmin.viewMenu();
}
}
```
运行示例代码,输出结果如下:
```
张三 登录系统:
普通用户菜单:
1. 查看个人信息
2. 修改个人信息
管理员 登录系统:
管理员菜单:
1. 查看个人信息
2. 修改个人信息
3. 查看用户列表
4. 添加用户
5. 删除用户
高级管理员 登录系统:
高级管理员菜单:
1. 查看个人信息
2. 修改个人信息
3. 查看用户列表
4. 添加用户
5. 删除用户
6. 修改用户权限
管理员 操作用户管理器:
用户列表:
张三
李四
管理员
高级管理员
用户列表:
张三
管理员
高级管理员
用户列表:
张三
管理员
高级管理员
高级管理员 操作用户管理器:
用户列表:
张三
管理员
高级管理员
用户列表:
管理员
高级管理员
用户列表:
张三
管理员
高级管理员
高级管理员菜单:
1. 查看个人信息
2. 修改个人信息
3. 查看用户列表
4. 添加用户
5. 删除用户
6. 修改用户权限
```
Also create a ControllerCreate class that extends Controller.The create method takes as arguments the name of a new library user, a number of books (as a string), and an integer representing the role of user to create (where the integer 0 means a lender and the integer 1 means a borrower). The create method of the controller then transforms the book number from a string to an integer (using the Integer.parseInt static method), creates an object from the correct class (based on the role specified by the user input: lender or borrower) and calls the addUser method of the library to add the new user object to the library. • If no exception occurs then the create method of the controller returns the empty string. • If the constructor of the Borrower class throws a NotALenderException then the create method of the controller must catch this exception and return as result the error message from the exception object. • If the parseInt method of the Integer class throws a NumberFormatException (because the user typed something which is not an integer) then the create method of the controller must catch this exception and return as result the error message from the exception object. Modify the run method of the GUI class to add a ViewCreate view that uses a ControllerCreate controller and the same model as before (not a new model!) Do not delete the previous views. Note: if at the end of Question 7 you had manually added to your library (model object) some users for testing, then you must now remove those users from the run method of the anonymous class inside the GUI class. You do not need these test users anymore because you have now a graphical user interface to create new users! Run your GUI and check that you can correctly use the new view to create different users for your library, with different types of roles. • Check that, when you create a new user, the simple view is automatically correctly updated to show the new total number of books borrowed by all users. • Also use the “get book” view to check that the users are correctly created with the correct names and correct number of books. • Also check that trying to create a borrower with a negative number of books correctly shows an error message. Also check that trying to create a user with a number of books which is not an integer correctly shows an error message (do not worry about the content of the error message). After you created a new user, you can also check whether it is a lender or a borrower using the “more book” view to increase the number of books of the user by a big negative number: • if the new user you created is a lender, then increasing the number of books by a big negative value will work and the number of books borrowed by the user will just become a larger value (you can then check that using the “get book” view); • if the new user you created is a borrower, then increasing the number of books by a big negative value will fail with an error message and the number of books borrowed by the user will not change (you can then check that using the “get book” view). 完成符合以上要求的java代码
Model class:
```
import java.util.ArrayList;
public class Library {
private ArrayList<User> users;
private int totalBooksBorrowed;
public Library() {
users = new ArrayList<>();
totalBooksBorrowed = 0;
}
public void addUser(User user) {
users.add(user);
totalBooksBorrowed += user.getNumberOfBooks();
}
public void removeUser(User user) {
users.remove(user);
totalBooksBorrowed -= user.getNumberOfBooks();
}
public ArrayList<User> getUsers() {
return users;
}
public int getTotalBooksBorrowed() {
return totalBooksBorrowed;
}
}
```
User class:
```
public abstract class User {
private String name;
private int numberOfBooks;
public User(String name, int numberOfBooks) {
this.name = name;
this.numberOfBooks = numberOfBooks;
}
public String getName() {
return name;
}
public int getNumberOfBooks() {
return numberOfBooks;
}
public void setNumberOfBooks(int numberOfBooks) {
this.numberOfBooks = numberOfBooks;
}
public abstract boolean isLender();
}
```
Lender class:
```
public class Lender extends User {
public Lender(String name, int numberOfBooks) {
super(name, numberOfBooks);
}
@Override
public boolean isLender() {
return true;
}
}
```
Borrower class:
```
public class Borrower extends User {
public Borrower(String name, int numberOfBooks) throws NotALenderException {
super(name, numberOfBooks);
if (numberOfBooks < 0) {
throw new NotALenderException("A borrower can't have a negative number of books!");
}
}
@Override
public boolean isLender() {
return false;
}
}
```
NotALenderException class:
```
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}
```
ViewCreate class:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ViewCreate extends JPanel implements ActionListener {
private JTextField nameField = new JTextField(10);
private JTextField numberOfBooksField = new JTextField(10);
private JRadioButton lenderButton = new JRadioButton("Lender");
private JRadioButton borrowerButton = new JRadioButton("Borrower");
private ButtonGroup roleButtonGroup = new ButtonGroup();
private JButton createButton = new JButton("Create");
private ControllerCreate controller;
public ViewCreate(Model model) {
this.controller = new ControllerCreate(model, this);
setLayout(new GridBagLayout());
roleButtonGroup.add(lenderButton);
roleButtonGroup.add(borrowerButton);
lenderButton.setSelected(true);
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.LINE_END;
gc.insets = new Insets(5, 5, 5, 5);
add(new JLabel("Name: "), gc);
gc.gridx++;
gc.anchor = GridBagConstraints.LINE_START;
add(nameField, gc);
gc.gridx = 0;
gc.gridy++;
gc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Number of books: "), gc);
gc.gridx++;
gc.anchor = GridBagConstraints.LINE_START;
add(numberOfBooksField, gc);
gc.gridx = 0;
gc.gridy++;
gc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Role: "), gc);
gc.gridx++;
gc.anchor = GridBagConstraints.LINE_START;
add(lenderButton, gc);
gc.gridx++;
add(borrowerButton, gc);
gc.gridx = 1;
gc.gridy++;
gc.anchor = GridBagConstraints.LINE_END;
add(createButton, gc);
createButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String numberOfBooksString = numberOfBooksField.getText();
int role = lenderButton.isSelected() ? 0 : 1;
String result = controller.create(name, numberOfBooksString, role);
JOptionPane.showMessageDialog(this, result, "Result", JOptionPane.INFORMATION_MESSAGE);
}
}
```
ControllerCreate class:
```
public class ControllerCreate extends Controller {
private ViewCreate view;
public ControllerCreate(Model model, ViewCreate view) {
super(model);
this.view = view;
}
public String create(String name, String numberOfBooksString, int role) {
try {
int numberOfBooks = Integer.parseInt(numberOfBooksString);
User user = null;
if (role == 0) {
user = new Lender(name, numberOfBooks);
} else if (role == 1) {
user = new Borrower(name, numberOfBooks);
}
model.addUser(user);
view.updateTotalBooksBorrowed();
return "";
} catch (NumberFormatException e) {
return "Invalid number of books!";
} catch (NotALenderException e) {
return e.getMessage();
}
}
}
```
GUI class:
```
import javax.swing.*;
import java.awt.*;
public class GUI {
private JFrame frame = new JFrame("Library");
private Model model = new Library();
private ViewSimple viewSimple = new ViewSimple(model);
private ViewGetBook viewGetBook = new ViewGetBook(model);
private ViewMoreBook viewMoreBook = new ViewMoreBook(model);
private ViewCreate viewCreate = new ViewCreate(model);
public GUI() {
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
JTabbedPane tp = new JTabbedPane();
tp.addTab("Simple", viewSimple);
tp.addTab("Get book", viewGetBook);
tp.addTab("More book", viewMoreBook);
tp.addTab("Create", viewCreate);
cp.add(tp, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Remove test users from model
model.removeUser(new Lender("John", 2));
model.removeUser(new Borrower("Mary", 3));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GUI();
}
});
}
}
```
阅读全文