ich übe nur einen schnellen Passwort-Manager mache, bevor ich wieder zur Schule gehen. Es ist mir egal, dass es Strings verwendet, um Passwörter im Klartext zu speichern ... das ist nicht der Punkt.Mehrere JFrames mit actionListeners
Was ich brauche Hilfe ist in meinem zweiten JFrame-Fenster Ich möchte das Passwort anzeigen, wenn der Benutzer auf einen JButton, die "Website-Name" (ich verwende eine Karte) klickt. Allerdings kann ich im zweiten JFrame-Fenster keinen meiner Buttons dazu bringen, etwas zu tun.
Hier ist der Code. Dies ist nur die Ansichtsklasse. Ich glaube nicht, dass das Modell oder der Controller hier anwendbar sind.
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
/**
* View class.
*
* @author Redacted
*/
@SuppressWarnings("serial")
public final class PasswordManagerView1 extends JFrame
implements PasswordManagerView {
//controller
private PasswordManagerController controller;
/**
* GUI widgets that need to be in scope in actionPerformed method, and
* related constants. (Each should have its own Javadoc comment, but these
* are elided here to keep the code shorter.)
*/
private static final int ROWS_IN_BUTTON_PANEL_GRID = 1,
COLUMNS_IN_BUTTON_PANEL_GRID = 2, KEY_LENGTH = 10,
VALUE_LENGTH = 15;
// JLabels
private final JLabel keyText, valueText;
// JTextFields
private final JTextField key, value;
// JButtons
private final JButton resetButton, enterButton, recallButton, testButton;
//constructor
public PasswordManagerView1() {
//JFrame title
super("Password Manager");
//widgets
this.testButton = new JButton("Test Button");
this.recallButton = new JButton("Recall");
this.valueText = new JLabel("Enter password here",
SwingConstants.CENTER);
this.keyText = new JLabel("Enter store here", SwingConstants.CENTER);
this.key = new JTextField(KEY_LENGTH);
this.value = new JTextField(VALUE_LENGTH);
this.resetButton = new JButton("Reset");
this.enterButton = new JButton("Enter");
//Button panel
JPanel buttonPanel = new JPanel(new GridLayout(
ROWS_IN_BUTTON_PANEL_GRID, COLUMNS_IN_BUTTON_PANEL_GRID));
//Add to button panel
buttonPanel.add(this.resetButton);
buttonPanel.add(this.enterButton);
//Grid layout
this.setLayout(new GridLayout(0, 2, 5, 0));
//Add to layout
this.add(this.key);
this.add(this.value);
this.add(this.keyText);
this.add(this.valueText);
this.add(this.resetButton);
this.add(this.enterButton);
this.add(this.recallButton);
this.add(this.testButton);
//observers
this.resetButton.addActionListener(this);
this.enterButton.addActionListener(this);
this.key.addActionListener(this);
this.value.addActionListener(this);
this.testButton.addActionListener(this);
//Pack up
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* Register argument as observer/listener of this.
*
* @param controller
* controller to register
*/
@Override
public void registerObserver(PasswordManagerController controller) {
this.controller = controller;
}
/**
* Updates key display based on String provided as argument.
*
* @param key
* new value of input display
*/
@Override
public void updateKeyDisplay(String key) {
this.key.setText(key);
}
/**
* Updates value display based on String provided as argument.
*
* @param value
* new value of output display
*/
@Override
public void updateValueDisplay(String value) {
this.value.setText(value);
}
@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.resetButton) {
this.controller.processResetEvent();
} else if (source == this.enterButton) {
this.controller.processEnterEvent(this.key.getText(),
this.value.getText());
} else if (source == this.recallButton) {
this.controller.processRecallEvent();
} else if (source == this.testButton) {
//Creates new JFrame window
JFrame test = new JFrame();
//Reads in store names
SimpleReader in = new SimpleReader1L("data/store.txt");
//Counts how many buttons to create
int passwordCount = 0;
while (!in.atEOS()) {
System.out.println(in.nextLine());
passwordCount++;
}
/*
* Previous operation went to the end of the file. We have to read
* in the file again.
*/
SimpleReader in2 = new SimpleReader1L("data/store.txt");
//Layout gets set.
test.setLayout(new FlowLayout());
//Creation of an array of JButtons
JButton[] storeButtons = new JButton[passwordCount];
for (int i = 0; i < passwordCount; i++) {
storeButtons[i] = new JButton(in2.nextLine());
storeButtons[i].addActionListener(this);
test.add(storeButtons[i]);
}
//Creates an array of Strings for the passwords
String[] passwordString = new String[passwordCount];
SimpleReader in3 = new SimpleReader1L("data/password.txt");
for (int i = 0; i < passwordCount; i++) {
passwordString[i] = in3.nextLine();
}
Object sourceFrame = event.getSource();
/*
* I'm just trying to get this to work with one of my buttons. I
* will use a for loop to cover all the buttons later.
*/
if (sourceFrame.equals(passwordString[0])) {
JOptionPane.showMessageDialog(test, "HEY");
}
test.pack();
test.setVisible(true);
in.close();
in2.close();
in3.close();
}
//Cursor normal again
this.setCursor(Cursor.getDefaultCursor());
}
}
'storeButtons [i] .addActionListener (this);' 'this' ist die erster Frame, nicht der zweite Frame, also wird nicht funktionieren – Jerry06
Was sollen die 'JButton' tun? Es sieht so aus, als ob Sie einen 'ActionListener' von' this' hinzufügen, der nichts tut, wenn die Quelle einem der 'storeButtons' entspricht. – Arthur
@ Jerry06 Wenn ich versuche, ‚Test‘ vor der Zugabe ‚storeButtons [i] .addActionListener (this)‘ Ich, dass storeButtons kann nicht auf eine Klasse oder ein Feld gelöst werden. Wenn ich versuche, dies durch 'test' zu ersetzen, bekomme ich die Methode addActionListener (ActionListener) im Typ AbstractButton ist nicht anwendbar für die Argumente. – frillybob