Java 具有actionListeners的多个JFrame
我正在做一个快速密码管理器,只是为了在我回学校之前练习一下。我不在乎它使用字符串以明文形式存储密码。。。这不是重点 我需要一些帮助的是在我的第二个JFrame窗口中,当用户单击JButton时,我想显示密码,即“网站名”(我正在使用地图)。然而,我无法让我的任何按钮在第二个JFrame窗口中实际执行任何操作 这是密码。这只是视图类。我认为模型或控制器在这里不适用Java 具有actionListeners的多个JFrame,java,swing,jframe,jbutton,actionlistener,Java,Swing,Jframe,Jbutton,Actionlistener,我正在做一个快速密码管理器,只是为了在我回学校之前练习一下。我不在乎它使用字符串以明文形式存储密码。。。这不是重点 我需要一些帮助的是在我的第二个JFrame窗口中,当用户单击JButton时,我想显示密码,即“网站名”(我正在使用地图)。然而,我无法让我的任何按钮在第二个JFrame窗口中实际执行任何操作 这是密码。这只是视图类。我认为模型或控制器在这里不适用 import java.awt.Cursor; import java.awt.FlowLayout; import java.awt
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());
}
}
导入java.awt.Cursor;
导入java.awt.FlowLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.SwingConstants;
导入components.simplereader.simplereader;
导入components.simplereader.SimpleReader1L;
/**
*查看类。
*
*@作者编辑
*/
@抑制警告(“串行”)
公共最终类PasswordManagerView1扩展了JFrame
实现PasswordManagerView{
//控制器
专用密码管理器控制器;
/**
*需要在actionPerformed方法的范围内的GUI小部件,以及
*相关常量。(每个都应该有自己的Javadoc注释,但是
*此处省略这些选项是为了缩短代码。)
*/
私有静态最终整数行在按钮面板网格中=1,
按钮面板网格中的列=2,键长度=10,
值_长度=15;
//JLabels
专用最终JLabel键文本、valueText;
//JTextFields
私有最终JTextField密钥、值;
//钮扣
私有最终JButton resetButton、enterButton、recallButton、testButton;
//建造师
公共密码管理器视图1(){
//JFrame标题
超级(“密码管理器”);
//小部件
this.testButton=新JButton(“测试按钮”);
this.recallButton=新的JButton(“召回”);
this.valueText=new JLabel(“在此处输入密码”,
SwingConstants.中心);
this.keyText=newjlabel(“在此处输入store”,SwingConstants.CENTER);
this.key=新的JTextField(key\u长度);
this.value=新的JTextField(value\u长度);
this.resetButton=newjbutton(“Reset”);
this.enterButton=newjbutton(“回车”);
//按钮面板
JPanel buttonPanel=新的JPanel(新的网格布局(
行在按钮面板网格中,列在按钮面板网格中);
//添加到按钮面板
buttonPanel.add(此.resetButton);
buttonPanel.add(此.enterButton);
//网格布局
这个.setLayout(新的GridLayout(0,2,5,0));
//添加到布局
this.add(this.key);
本.加(本.值);
this.add(this.keyText);
this.add(this.valueText);
this.add(this.reset按钮);
this.add(this.enter按钮);
this.add(this.recall按钮);
this.add(this.testButton);
//观察员
this.resetButton.addActionListener(this);
this.enterButton.addActionListener(this);
this.key.addActionListener(this);
this.value.addActionListener(this);
this.testButton.addActionListener(this);
//收拾
这个包();
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此.setVisible(true);
}
/**
*将参数注册为此的观察者/侦听者。
*
*@param控制器
*注册控制器
*/
@凌驾
公共无效注册表观察服务器(密码管理器控制器){
this.controller=控制器;
}
/**
*根据作为参数提供的字符串更新键显示。
*
*@param-key
*输入显示的新值
*/
@凌驾
公共void updateKeyDisplay(字符串键){
this.key.setText(key);
}
/**
*根据作为参数提供的字符串更新值显示。
*
*@param值
*输出显示的新值
*/
@凌驾
公共void updateValueDisplay(字符串值){
this.value.setText(值);
}
@凌驾
已执行的公共无效操作(操作事件){
//等待光标
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_Cursor));
//按下了什么按钮
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){
//创建新的JFrame窗口
JFrame测试=新JFrame();
//读取存储名称
SimpleReaderIn=newSimpleReader1L(“data/store.txt”);
//计算要创建的按钮数
int passwordCount=0;
而(!in.atEOS()){
System.out.println(in.nextLine());
密码计数++;
}
/*
*上一个操作已到达文件结尾。我们必须读取
*再次输入文件。
*/
SimpleReaderIn2=新的SimpleReader1L(“data/store.txt”);
//布局得到设置。
test.setLayout(新的FlowLayout());
//创建jbutton数组
JButton[]storeButtons=newjbutton[passwordCount];
for(int i=0;istoreButtons[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Store button " + i + " was pressed.");
}
});
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");
}
}
}