我在代码行中收到错误“从内部类引用的局部变量必须是最终的或实际上是最终的”
newscrollPanel = new JScrollPane(newList);
我在staffPanel构造函数中将newscrollPanel贴花了。
Id希望能够在同一位置添加newscrollPanel变量,我添加其他GUI元素(请参见代码的底部),以便面板按名称显示在旁边。我发布的代码ive是我认为可以做到的方式,但是我收到开始时描述的错误。如果我将面板添加到sortbylastName操作侦听器中,则面板将显示在注销按钮后的末尾,该按钮为false(请参见此图片https://i.imgur.com/sh28fbX.png)。图片。我怎样才能做到这一点?谢谢
public class staffPanel {
private JPanel Panel = new JPanel(new GridLayout(0, 2, 0, 15));
public staffPanel(User userInput, UserManagement managementDatabase) {
JButton getName = new JButton("Show your full name");
JButton changePassword = new JButton("Change your password");
JButton updatefullName = new JButton("Update your full name and position");
JButton sortbylastName = new JButton("Sort by last name");
JButton LogOut = new JButton("Log out");
JButton searchButton = new JButton("Search");
JTextField oldPassword = new JTextField();
JTextField newPassword = new JTextField();
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JTextField position = new JTextField();
JTextField searchBar = new JTextField("Search User");
JLabel usersfullName = new JLabel();
JLabel spacer;
JLabel oldpasswordLabel = new JLabel("Old password");
JLabel newpasswordLabel = new JLabel("New password");
JLabel firstnameLabel = new JLabel("First name");
JLabel lastnameLabel = new JLabel("Last name");
JLabel positionLabel = new JLabel("Position");
JScrollPane newscrollPanel;
getName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent clicked) {
usersfullName.setText(getFullName(userInput));
}
});
LogOut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame mainFrame = (JFrame) SwingUtilities.getWindowAncestor(Panel);
Panel.setSize(400, 100);
Panel.setVisible(false);
userLogin newLogin = new userLogin(managementDatabase);
mainFrame.add(newLogin.getPanel());
}
});
changePassword.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent clicked) {
boolean result = (managementDatabase.changePassword(userInput.getUserName(), oldPassword.getText(), newPassword.getText()));
if (result == true) {
JOptionPane.showMessageDialog(null, "Password Sucessfully Channged");
} else {
JOptionPane.showMessageDialog(null, "Passowrd cannot be changed ", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
});
updatefullName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent clicked) {
if (firstName.getText().isEmpty() || lastName.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "username or password field empty!", "ERROR", JOptionPane.ERROR_MESSAGE);
} else {
Staff newStaff = (Staff) userInput;
newStaff.setPoistion(position.getText());
userInput.setFirstName(firstName.getText());
userInput.setLastName(lastName.getText());
JOptionPane.showMessageDialog(null, "Username updated");
}
}
});
sortbylastName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent clicked) {
List<Customer> customerList;
JList<String> newList;
customerList = Customer.filterOnlyCustomerList(managementDatabase.getList());
List<String> sortedList = customerList.stream().flatMap(e -> Stream.of(e.getFullName() + " " + e.getCustomerAddress())).sorted().collect(Collectors.toList());
newList = new JList<>(sortedList.toArray(new String[sortedList.size()]));
newscrollPanel = new JScrollPane(newList);
Panel.add(newscrollPanel);
}
}
);
Panel.add(getName);
Panel.add(usersfullName);
Panel.add(changePassword);
Panel.add(
new JLabel());
Panel.add(oldpasswordLabel);
Panel.add(newpasswordLabel);
Panel.add(oldPassword);
Panel.add(newPassword);
Panel.add(updatefullName);
Panel.add(
new JLabel());
Panel.add(firstnameLabel);
Panel.add(lastnameLabel);
Panel.add(firstName);
Panel.add(lastName);
Panel.add(positionLabel);
Panel.add(position);
Panel.add(sortbylastName);
Panel.add(spacer = new JLabel(" "));
Panel.add(searchButton);
Panel.add(searchBar);
Panel.setVisible(
true);
Panel.add(LogOut);
;
}
public JPanel getPanel() {
return Panel;
}
public String getFullName(User input) {
return input.getFullName();
}
}