gpt4 book ai didi

java - JList 未在单独的 Pane 上更新(Swing、Java)

转载 作者:行者123 更新时间:2023-12-01 16:51:38 25 4
gpt4 key购买 nike

在我的第一个名为“密码保管库”的选项卡上,我有一个 JList,其中包含存储密码的“网站”列表。在第二个选项卡上,我有一个添加“密码”功能。当我添加密码时,JList 仅在 Pane 上更新。当我切换回另一个选项卡“密码保管库”时,它尚未更新。当我重新启动该程序时,它已更新。我使用了一个字段,所以这不是局部变量的问题。

public final class PasswordManagerView1 extends JFrame
implements PasswordManagerView, ListSelectionListener {
/**
* JList.
*/
private JList<String> list;

/**
* JButtons.
*/
private JButton enter, unlock, reset, buttonAddEnter;

/**
* Controller.
*/
private PasswordManagerController controller;

/**
* Dimensions.
*/
private Dimension maxSize;

/**
* JTabbedPanes.
*/
private JTabbedPane tabbedPane;

/**
* JTextField.
*/
private JTextField passwordDisplay, textField;

/**
* PasswordField.
*/
private JPasswordField passwordField, resetField, passwordFieldadd;

/**
* Useful Constants.
*/
private static final int MAX_SIZE_HORI = 800, MAX_SIZE_VERTI = 400,
EMPTY_BORDER_SIZE = 5;

/**
* Constructor.
*/
public PasswordManagerView1() {
super("Password Manager");
JTabbedPane tabbedPane = new JTabbedPane();
//Initial JPanel creation
tabbedPane.setBorder(
new EmptyBorder(PasswordManagerView1.EMPTY_BORDER_SIZE,
PasswordManagerView1.EMPTY_BORDER_SIZE,
PasswordManagerView1.EMPTY_BORDER_SIZE,
PasswordManagerView1.EMPTY_BORDER_SIZE));
this.maxSize = new Dimension(PasswordManagerView1.MAX_SIZE_HORI,
PasswordManagerView1.MAX_SIZE_VERTI);
tabbedPane.setPreferredSize(this.maxSize);
this.getContentPane().add(tabbedPane);

//Initial JTabbedPane creation

//Tab creation
JComponent panel1 = this.makePasswordVault();
ImageIcon icon = new ImageIcon("lock-icon.png");
tabbedPane.addTab("Password Vault", icon, panel1,
"View the passwords in the vault");
JComponent panel3 = this.makeAddPanel("Add Password");
tabbedPane.addTab("Add Password", icon, panel3,
"Add passwords to the vault");
//JComponent panel2 = this.makeAddPanel("ALSO ADDS");
//tabbedPane.addTab("Delete Password", icon, panel2,
// "Deletes a password from the vault");

JComponent panel4 = this.makeInfoPanel();
tabbedPane.addTab("Info", icon, panel4,
"View settings and program info");
//Pack up
this.setResizable(false);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

@Override
public JComponent makeAddPanel(String text) {
JPanel panel = new JPanel();
panel.setLayout(null);
//Creation of string array of with stores
String[] listContentC = this.getStore();

this.list = new JList<>(listContentC);
this.list.setBounds(0, 0, 233, 360);
JScrollPane pane = new JScrollPane(this.list);
pane.setBounds(0, 0, 233, 360);
panel.add(pane);
JLabel labelAdd = new JLabel("Add Password");
labelAdd.setHorizontalAlignment(SwingConstants.CENTER);
labelAdd.setFont(new Font("Times New Roman", Font.PLAIN, 24));
labelAdd.setBounds(427, 38, 163, 31);
panel.add(labelAdd);

this.passwordFieldadd = new JPasswordField();
this.passwordFieldadd.setBounds(536, 100, 116, 22);
panel.add(this.passwordFieldadd);

this.textField = new JTextField();
this.textField.setBounds(375, 100, 116, 22);
panel.add(this.textField);

JLabel lblWebsite = new JLabel("Website");
lblWebsite.setHorizontalAlignment(SwingConstants.CENTER);
lblWebsite.setFont(new Font("Times New Roman", Font.PLAIN, 13));
lblWebsite.setBounds(401, 126, 56, 16);
panel.add(lblWebsite);

JLabel lblPassword = new JLabel("Password");
lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
lblPassword.setFont(new Font("Times New Roman", Font.PLAIN, 13));
lblPassword.setBounds(566, 126, 56, 16);
panel.add(lblPassword);

this.buttonAddEnter = new JButton("Enter");
this.buttonAddEnter.setBounds(465, 161, 97, 25);
panel.add(this.buttonAddEnter);
this.buttonAddEnter.addActionListener(this);
return panel;
}

@Override
public JComponent makeInfoPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
StringBuilder toPrint = new StringBuilder();
SimpleReader in = new SimpleReader1L("data/Notice.txt");
while (!in.atEOS()) {
toPrint.append(in.nextLine() + "\n");
}
String toPrintString = toPrint.toString();
JTextArea noticeText = new JTextArea(toPrintString);
noticeText.setEditable(false);
JScrollPane noticeTextScroll = new JScrollPane(noticeText);
panel.add(noticeTextScroll);
in.close();
return panel;

}

@Override
public JComponent makePasswordVault() {
JPanel panel = new JPanel();
panel.setLayout(null);
/*
* Ask for help on this.
*
* I would have liked to create the listContentC by passing nothing into
* the controller and then the controller using the model to extract the
* data from a text file. However, my array was always initialized to
* something null and caused a runtime error.
*
*
*/

//Creation of string array of with stores
String[] listContentC = this.getStore();

//GUI setup of list
this.list = new JList<>(listContentC);
this.list.setLayoutOrientation(JList.VERTICAL);
this.list.addListSelectionListener(this);
JScrollPane pane = new JScrollPane(this.list);
pane.setBounds(0, 0, 233, 360);
panel.add(pane);

//The label creation (for instructions)
JLabel labelA = new JLabel("the store and press enter!");
labelA.setFont(new Font("Times New Roman", Font.PLAIN, 13));
labelA.setBounds(294, 70, 157, 31);
panel.add(labelA);
JLabel labelB = new JLabel("To view a password, click on");
labelB.setFont(new Font("Times New Roman", Font.PLAIN, 13));
labelB.setBounds(284, 54, 163, 31);
panel.add(labelB);

//Enter button creation
this.enter = new JButton("Enter");
this.enter.setBounds(303, 128, 116, 25);
panel.add(this.enter);
this.enter.setEnabled(false);
this.enter.addActionListener(this);

//Password Display field creation
this.passwordDisplay = new JTextField();
this.passwordDisplay.setBackground(Color.WHITE);
this.passwordDisplay.setEditable(false);
this.passwordDisplay
.setFont(new Font("Times New Roman", Font.PLAIN, 13));
this.passwordDisplay.setHorizontalAlignment(SwingConstants.CENTER);
this.passwordDisplay.setText("");
this.passwordDisplay.setBounds(303, 247, 116, 22);
panel.add(this.passwordDisplay);
this.passwordDisplay.setColumns(10);

//Password Label creation
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
passwordLabel.setFont(new Font("Times New Roman", Font.PLAIN, 13));
passwordLabel.setBounds(336, 218, 56, 16);
panel.add(passwordLabel);

//Master password notice
JLabel mastPass = new JLabel("Enter master password to unlock vault:");
mastPass.setFont(new Font("Times New Roman", Font.PLAIN, 13));
mastPass.setBounds(532, 54, 218, 31);
panel.add(mastPass);

//Password Field
this.passwordField = new JPasswordField();
this.passwordField.setBounds(532, 128, 116, 24);
panel.add(this.passwordField);

//Unlock button
this.unlock = new JButton("Unlock");
this.unlock.setFont(new Font("Times New Roman", Font.PLAIN, 13));
this.unlock.setBounds(660, 127, 97, 25);
panel.add(this.unlock);
this.unlock.addActionListener(this);

//New setup label
JLabel labelC = new JLabel("Reset/Set up new master pass:");
labelC.setHorizontalAlignment(SwingConstants.CENTER);
labelC.setFont(new Font("Times New Roman", Font.PLAIN, 13));
labelC.setBounds(532, 217, 218, 16);
panel.add(labelC);

//New setup label
JLabel defaultLabel = new JLabel("Default Password = \"password\"");
defaultLabel.setHorizontalAlignment(SwingConstants.CENTER);
defaultLabel.setFont(new Font("Times New Roman", Font.PLAIN, 13));
defaultLabel.setBounds(560, 77, 171, 16);
panel.add(defaultLabel);

this.resetField = new JPasswordField();
this.resetField.setBounds(532, 246, 116, 24);
panel.add(this.resetField);

this.reset = new JButton("Update");
this.reset.setFont(new Font("Times New Roman", Font.PLAIN, 13));
this.reset.setBounds(660, 245, 97, 25);
this.reset.setEnabled(false);
panel.add(this.reset);
this.reset.addActionListener(this);
return panel;
}

@Override
public String[] getStore() {

int storeCount = 0;
SimpleReader in = new SimpleReader1L("data/store.txt");
while (!in.atEOS()) {
storeCount++;
in.nextLine();
}
in.close();
String[] listContentC = new String[storeCount];
SimpleReader in2 = new SimpleReader1L("data/store.txt");
for (int i = 0; i < storeCount; i++) {
listContentC[i] = in2.nextLine();
}
in2.close();
return listContentC;
}

@Override
public void registerObserver(PasswordManagerController controller) {
this.controller = controller;
}

@Override
public void updateEnterButtonVaultTab(boolean result) {
this.enter.setEnabled(result);
}

@Override
public void actionPerformed(ActionEvent event) {
//Wait cursor
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

//What button was pressed
Object source = event.getSource();
if (source == this.enter) {
int index = this.list.getSelectedIndex();
System.out.println(index);
if (this.list.getSelectedIndex() != -1) {
this.controller.processEnterEvent(this.list.getSelectedValue());
}
} else if (source == this.unlock) {
this.controller
.processUnlockEvent(this.passwordField.getPassword());
} else if (source == this.reset) {
this.controller.processResetEvent(this.resetField.getPassword());
} else if (source == this.buttonAddEnter) {
this.controller.processAddEvent(this.textField.getText(),
this.passwordFieldadd.getPassword());
//needs done in model
this.updateListModel();
}
this.setCursor(Cursor.getDefaultCursor());
}

@Override
public void updateListModel() {
//MOVE TO THE MODEL
String[] temp = this.getStore();
DefaultListModel<String> temp2 = new DefaultListModel<>();
for (int i = 0; i < temp.length; i++) {
temp2.addElement(temp[i]);
}
this.list.setModel(temp2);
}

@Override
public JTabbedPane getTabbedPane() {
return this.tabbedPane;
}

@Override
public void setTabbedPane(JTabbedPane tabbedPane) {
this.tabbedPane = tabbedPane;
}

@Override
public void updatePasswordField() {
this.passwordField.setText("");
}

@Override
public void updateResetPasswordDisplay() {
this.resetField.setText("");
}

@Override
public void displayWrongPass() {
JOptionPane.showMessageDialog(this, "Wrong password entered!");

}

@Override
public void displayUpdatedPass() {
JOptionPane.showMessageDialog(this, "Master password updated!");
}

@Override
public void updateResetButton(boolean result) {
this.reset.setEnabled(result);
}

@Override
public void updatePasswordDisplay(char[] password) {
StringBuilder passwordCreation = new StringBuilder();
for (int i = 0; i < password.length; i++) {
passwordCreation.append(password[i]);
}
this.passwordDisplay.setText(passwordCreation.toString());

}

@Override
public JComponent makeTextPanel(String text) {
// TODO Auto-generated method stub
return null;
}

@Override
public void valueChanged(ListSelectionEvent arg0) {

}

}

最佳答案

您的构造函数调用 makePasswordPanel(),其中包含以下行:

   this.list = new JList<>(listContentC);

构造函数然后调用 makeAddPanel(),其中包含以下行:

   this.list = new JList<>(listContentC);

现在您已经丢弃了第一个列表的句柄,并将其替换为对新列表的引用。然后在 updateListModel 中,更改 this.list (即 makeAddPanel 列表)的模型。无法同时更新密码面板的模型,因为您已放弃对该面板的引用。

关于java - JList 未在单独的 Pane 上更新(Swing、Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39000561/

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