-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailLayout.java
More file actions
161 lines (116 loc) · 4.76 KB
/
Copy pathMailLayout.java
File metadata and controls
161 lines (116 loc) · 4.76 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Tisha Greenidge
* CSCIE-10b
* HW 4
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class MailLayout extends JFrame {
public static Font f = new Font ("Helvetica", Font.BOLD, 10);
public static void formatPanels (JPanel panel, JLabel label, JTextField textBox,
JPanel container) {
label.setFont(f);
panel.setLayout ( new BorderLayout() );
panel.add (label, BorderLayout.WEST);
panel.add (textBox, BorderLayout.CENTER);
panel.setBackground(Color.WHITE);
container.add (panel);
}
public static void formatPanels (JPanel panel, JLabel label, JComboBox<String> comboBox,
JPanel container) {
label.setFont(f);
panel.setLayout ( new BorderLayout() );
panel.add (label, BorderLayout.WEST);
panel.add (comboBox, BorderLayout.CENTER);
panel.setBackground(Color.WHITE);
container.add (panel);
}
public static void writeToFile (String textMsg) {
FileOutputStream out = null;
byte[] msgInBytes = textMsg.getBytes();
try {
FileOutputStream outputFile = new FileOutputStream("outbox.txt");
outputFile.write(msgInBytes);
outputFile.close();
} catch(Exception e) {
System.out.println(e);
}
}
public MailLayout () {
"Tisha Yahoo - [email protected]"};
setSize(500, 500);
setTitle("New Message");
JPanel messageDetails = new JPanel();
messageDetails.setLayout ( new BoxLayout(messageDetails, BoxLayout.PAGE_AXIS) );
JTextField toBox = new JTextField ("", 40 );
JLabel toLabel = new JLabel ("To:");
JPanel emailTo = new JPanel();
formatPanels (emailTo, toLabel, toBox, messageDetails);
JTextField ccBox = new JTextField ("", 40);
JLabel ccLabel = new JLabel ("Cc:");
JPanel emailCc = new JPanel();
formatPanels (emailCc, ccLabel, ccBox, messageDetails);
JTextField bccBox = new JTextField ("", 40);
JLabel bccLabel = new JLabel ("Bcc:");
JPanel emailBcc = new JPanel();
formatPanels (emailBcc, bccLabel, bccBox, messageDetails);
JTextField subjectBox = new JTextField ("", 40);
JLabel subjectLabel = new JLabel ("Subject:");
//change title of frame when text is entered
subjectBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
setTitle(subjectBox.getText());
}
});
JPanel emailSubject = new JPanel();
formatPanels (emailSubject, subjectLabel, subjectBox, messageDetails);
JComboBox<String> fromBox = new JComboBox<> (emails);
JLabel fromLabel = new JLabel ("From:");
JPanel emailFrom = new JPanel();
formatPanels (emailFrom, fromLabel, fromBox, messageDetails);
JTextArea emailMessage = new JTextArea (15, 100);
JButton sendButton = new JButton ("Send");
// On my MAc, button appears white until clicked, then changes to blue
sendButton.setBackground (Color.BLUE);
//output contents to file
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String message = emailMessage.getText();
writeToFile(message);
}
});
sendButton.addActionListener ( new SendMessage() );
sendButton.setFont(f);
add (sendButton, BorderLayout.NORTH);
add (messageDetails, BorderLayout.CENTER);
add (emailMessage, BorderLayout.SOUTH);
setDefaultCloseOperation (EXIT_ON_CLOSE);
pack();
}
public static void main (String [] args) {
MailLayout emailLayout = new MailLayout();
emailLayout.setVisible (true);
}
}
/**
* When clicked, this button should cause the content of
* the JTextArea to be written to a file named outbox.txt After the message is
* written to file, the screen should be cleared and title should be reset.
*/
class SendMessage implements ActionListener {
public void actionPerformed (ActionEvent e) {
JButton sendButton = (JButton) e.getSource();
JFrame frame = (JFrame) SwingUtilities.getRoot(sendButton);
//destroy old screen after save
frame.setVisible(false);
frame.dispose();
//reset screen
MailLayout emailLayout = new MailLayout();
emailLayout.setVisible (true);
}
}