2016-08-12 1 views
-1

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

} 
+0

'storeButtons [i] .addActionListener (this);' 'this' ist die erster Frame, nicht der zweite Frame, also wird nicht funktionieren – Jerry06

+0

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

+0

@ 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

Antwort

1

Alle der storeButtons haben eine ActionListener von this. Wann immer sie geklickt werden, wird actionPerformed() ausgeführt. Aber Sie haben if Aussagen zu überprüfen, ob die Quelle der ActionEvent entweder resetButton, enterButton, recallButton und testButton. Das heißt, wenn die Quelle einer der JButton s von storeButtons ist, wird es nichts tun.

Versuchen Hinzufügen einer anonymen Klasse zum storeButtons statt und sehen, was passiert.

storeButtons[i].addActionListener(new ActionListener(){ 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     System.out.println("Store button " + i + " was pressed."); 
    } 
}); 
+0

Die i hat einen Hinweis, dass 'nicht auf dem nicht endgültig lokal Variable storeButtons in einem umschließenden definiert beziehen scope' ich es endgültig sein nicht will, wie es jedes Mal durch die Schleife modifiziert. – frillybob

+0

@frillybob [ein Array-Finale zu machen, hält Sie nicht davon ab, es zu bearbeiten] (http://stackoverflow.com/questions/10339930/final-array-in-java). Ich habe auch nur die anonyme Klasse als Beispiel benutzt. Sie können 'this' verwenden, solange Sie die' storeButtons' von dort referenzierbar machen. – Arthur

1

Sie sollten Ihren Code in neue Klasse JFrame2 für Lesbarkeit und Wartung trennen: Die JFrame2 wie folgt aus:

public JFrame2 extends JFrame implements ActionListener{ 

    String[] passwordString; 
    JButton[] storeButtons; 

    public JFrame2(){ 
      //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. 
     this.setLayout(new FlowLayout()); 
     //Creation of an array of JButtons 
     storeButtons = new JButton[passwordCount]; 
     for (int i = 0; i < passwordCount; i++) { 
      storeButtons[i] = new JButton(in2.nextLine()); 
      storeButtons[i].addActionListener(this); 
      this.add(storeButtons[i]); 
     } 
     //Creates an array of Strings for the passwords 
     passwordString = new String[passwordCount]; 
     SimpleReader in3 = new SimpleReader1L("data/password.txt"); 
     for (int i = 0; i < passwordCount; i++) { 
      passwordString[i] = in3.nextLine(); 
     } 


     this.pack(); 
     this.setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent event) { 
      JButton button = (JButton)event.getSource();//the button you are clicking 
     /* 
     * 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 (button.getText().equals(passwordString[0])) { 
      //JOptionPane.showMessageDialog(test, "HEY"); 
     } 
    } 
} 
+0

Zurück in der View-Datei würde ich einfach 'this.JFrame2()' aufrufen? Richtig? – frillybob

+0

benötigen Sie 'neue JFrame2()' in Ihrer Ansicht – Jerry06

Verwandte Themen