-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataExchangeFrame.java
65 lines (51 loc) · 1.92 KB
/
DataExchangeFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dataExchange;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* A frame with a menu whose File->Connect action shows a password dialog.
*/
public class DataExchangeFrame extends JFrame {
public static final int TEXT_ROWS = 20;
public static final int TEXT_COLUMNS = 40;
private PasswordChooser dialog = null;
private JTextArea textArea;
public DataExchangeFrame() {
// construct a File menu
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
// add Connect and Exit menu items
JMenuItem connectItem = new JMenuItem("Connect");
connectItem.addActionListener(new ConnectAction());
fileMenu.add(connectItem);
// The Exit item exits the program
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(exitItem);
textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
add(new JScrollPane(textArea), BorderLayout.CENTER);
pack();
}
/**
* The Connect action pops up the password dialog.
*/
private class ConnectAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// if first time, construct dialog
if (dialog == null) dialog = new PasswordChooser();
// set default values
dialog.setUser(new User("your name", null));
// pop up dialog
if (dialog.showDialog(DataExchangeFrame.this, "Connect")) {
// if accepted, retrieve user input
User user = dialog.getUser();
textArea.append("user name = " + user.getName() + ", password = " +
(new String(user.getPassword())) + "\n");
}
}
}
}