CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
//import java.util.*;
public class Email extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
private JLabel emailLabel, passwordLabel, exception1Label, exception2Label, blank1Label, blank2Label;
private JTextField emailTextField, passwordTextField;
private JButton loginB, cancelB;
private LoginButtonHandler loginbHandler;
private CancelButtonHandler cancelbHandler;
static String email = "", password = "", errorMsg1, errorMsg2;
public Email() {
setTitle("Login");
setSize(WIDTH, HEIGHT);
emailLabel = new JLabel("Email: ");
blank1Label = new JLabel("");
exception1Label = new JLabel(errorMsg1);
passwordLabel = new JLabel("Password: ");
blank2Label = new JLabel("");
exception2Label = new JLabel(errorMsg2);
emailTextField = new JTextField();
passwordTextField = new JPasswordField();
loginB = new JButton("Login");
loginbHandler = new LoginButtonHandler();
loginB.addActionListener(loginbHandler);
cancelB = new JButton("Cancel");
cancelbHandler = new CancelButtonHandler();
cancelB.addActionListener(cancelbHandler);
Container pane = getContentPane();
GridLayout gridLayout = new GridLayout (4,2);
FlowLayout flowLayout = new FlowLayout();
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
northPanel.setLayout(gridLayout);
southPanel.setLayout(flowLayout);
northPanel.add(emailLabel);
northPanel.add(emailTextField);
northPanel.add(blank1Label);
northPanel.add(exception1Label);
northPanel.add(passwordLabel);
northPanel.add(passwordTextField);
northPanel.add(blank2Label);
northPanel.add(exception2Label);
southPanel.add(loginB);
southPanel.add(cancelB);
pane.add(northPanel, BorderLayout.NORTH);
pane.add(southPanel, BorderLayout.SOUTH);
centerFrame(WIDTH, HEIGHT);
}
public void centerFrame(int frameWidth, int frameHeight) {
Toolkit aToolkit = Toolkit.getDefaultToolkit();
Dimension screen = aToolkit.getScreenSize();
int xPositionOfFrame = (screen.width - frameWidth) / 2;
int yPositionOfFrame = (screen.height - frameHeight) / 2;
setBounds(xPositionOfFrame, yPositionOfFrame, frameWidth, frameHeight);
}
private class LoginButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent a) {
NewEmail ne = new NewEmail();
ne.setDefaultCloseOperation(EXIT_ON_CLOSE);
ne.setVisible(true);
// declare I/O objects
File outFile;
FileWriter aFileWriter;
PrintWriter aPrintWriter = null;
// get data from text fields
String aEmail = emailTextField.getText();
String aPassword = passwordTextField.getText();
// place data in string
String outputStr = aEmail + ", " + aPassword + "\r\n";
try
{
outFile = new File( "email.txt" ); // create File object
aFileWriter = new FileWriter( outFile );
aPrintWriter = new PrintWriter( aFileWriter );
aPrintWriter.println( outputStr ); // write string to output file
} // end try
catch(IOException error)
{
System.out.println( "I/O Error" );
}
finally
{
aPrintWriter.close();
}
}
}
private class CancelButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent a){
System.exit(0);
}
}
public static class NewEmail extends JFrame {
private static final int WIDTH = 800;
private static final int HEIGHT = 500;
private JLabel emailLabel, subjectLabel, blank1Label, blank2Label;
private JTextField emailTextField, subjectTextField;
private JTextArea msgTextArea;
private JButton sendB, cancelB;
private JPanel labelPanel, fieldPanel, buttonPanel, topPanel, mainPanel, bottomPanel;
private SendButtonHandler sendbHandler;
private CancelButtonHandler cancelbHandler;
String email = "";
String subject = "";
String msg = "";
String errorMsg1;
public NewEmail() {
setTitle("New Email");
setSize(WIDTH, HEIGHT);
emailLabel = new JLabel("To: ", SwingConstants.RIGHT);
blank1Label = new JLabel("");
blank2Label = new JLabel("");
subjectLabel = new JLabel("Subject: ", SwingConstants.RIGHT);
emailTextField = new JTextField();
subjectTextField = new JTextField();
msgTextArea = new JTextArea();
sendB = new JButton("Send");
sendbHandler = new SendButtonHandler();
sendB.addActionListener(sendbHandler);
cancelB = new JButton("Cancel");
cancelbHandler = new CancelButtonHandler();
cancelB.addActionListener(cancelbHandler);
Container pane = getContentPane();
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(4,1,3,3));
fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(4,1,3,3));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,1,3,3));
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1,3));
bottomPanel = new JPanel();
labelPanel.add(emailLabel);
fieldPanel.add(emailTextField);
labelPanel.add(blank1Label);
fieldPanel.add(blank2Label);
labelPanel.add(subjectLabel);
fieldPanel.add(subjectTextField);
mainPanel.add(msgTextArea);
topPanel.add(labelPanel);
topPanel.add(fieldPanel);
topPanel.add(buttonPanel);
bottomPanel.add(sendB);
bottomPanel.add(cancelB);
pane.add(topPanel, BorderLayout.NORTH);
pane.add(bottomPanel, BorderLayout.SOUTH);
pane.add(mainPanel, BorderLayout.CENTER);
centerFrame (WIDTH, HEIGHT);
}
public void centerFrame(int frameWidth, int frameHeight) {
Toolkit aToolkit = Toolkit.getDefaultToolkit();
Dimension screen = aToolkit.getScreenSize();
int xPositionOfFrame = (screen.width - frameWidth) / 2;
int yPositionOfFrame = (screen.height - frameHeight) / 2;
setBounds(xPositionOfFrame, yPositionOfFrame, frameWidth, frameHeight);
}
private class SendButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent a) {
ConfirmEmail ne = new ConfirmEmail();
ne.setDefaultCloseOperation(EXIT_ON_CLOSE);
ne.setVisible(true);
}
}
private class CancelButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent a) {
System.exit(0);
}
}
}
public static class ConfirmEmail extends JFrame {
private static final int WIDTH = 800;
private static final int HEIGHT = 500;
//private JLabel emailLabel, subjectLabel, blank1Label, blank2Label;
//private JTextField confirmTextField, emailTextField,subjectTextField;
private JTextArea msgTextArea;
private JButton sendB, cancelB;
private JPanel labelPanel, fieldPanel, buttonPanel, topPanel, mainPanel, bottomPanel;
private NewemailButtonHandler newbHandler;
private CancelButtonHandler cancelbHandler;
//String email = "";
//String subject = "";
String msg = "";
//String errorMsg1;
public ConfirmEmail() {
setTitle("Email Confirmation");
setSize(WIDTH, HEIGHT);
//emailLabel = new JLabel("To: ", SwingConstants.RIGHT);
//blank1Label = new JLabel("");
//blank2Label = new JLabel("");
// subjectLabel = new JLabel("Subject: ", SwingConstants.RIGHT);
// confirmTextField = new JTextField();
msgTextArea = new JTextArea("Dear student23@yahoo.com");
sendB = new JButton("New email");
newbHandler = new NewemailButtonHandler();
sendB.addActionListener(newbHandler);
cancelB = new JButton("Cancel");
cancelbHandler = new CancelButtonHandler();
cancelB.addActionListener(cancelbHandler);
Container pane = getContentPane();
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(4,1,3,3));
fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(4,1,3,3));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,1,3,3));
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1,3));
bottomPanel = new JPanel();
//labelPanel.add(emailLabel);
//fieldPanel.add(emailTextField);
//labelPanel.add(blank1Label);
//fieldPanel.add(blank2Label);
//labelPanel.add(subjectLabel);
// fieldPanel.add(confirmTextField);
mainPanel.add(msgTextArea);
topPanel.add(labelPanel);
topPanel.add(fieldPanel);
topPanel.add(buttonPanel);
bottomPanel.add(sendB);
bottomPanel.add(cancelB);
pane.add(topPanel, BorderLayout.NORTH);
pane.add(bottomPanel, BorderLayout.SOUTH);
pane.add(mainPanel, BorderLayout.CENTER);
centerFrame (WIDTH, HEIGHT);
}
public void centerFrame(int frameWidth, int frameHeight) {
Toolkit aToolkit = Toolkit.getDefaultToolkit();
Dimension screen = aToolkit.getScreenSize();
int xPositionOfFrame = (screen.width - frameWidth) / 2;
int yPositionOfFrame = (screen.height - frameHeight) / 2;
setBounds(xPositionOfFrame, yPositionOfFrame, frameWidth, frameHeight);
}
private class NewemailButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent a) {
NewEmail ne = new NewEmail();
ne.setDefaultCloseOperation(EXIT_ON_CLOSE);
ne.setVisible(true);
}
}
private class CancelButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent a) {
System.exit(0);
}
}
public static void main(String[] args) {
JFrame aemail = new Email();
aemail.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aemail.setVisible(true);
NewEmail app = new NewEmail();
app.setDefaultCloseOperation(EXIT_ON_CLOSE);
app.setVisible(false);
//JFrame aconfirm = new ConfirmEmail();
//aconfirm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//aconfirm.setVisible(true);
ConfirmEmail con = new ConfirmEmail();
con.setDefaultCloseOperation(EXIT_ON_CLOSE);
con.setVisible(false);
}
}
}
I want to ask about:
1) Is my coding for linking the loginForm and NewEmail is correct?
2)How to validate errors in the logIn form; where i want to check for the format of the email such as xxx@xxx.xxx
3) I want to do exception in the loginForm "invalid email address" , in the New Email form (in "TO" box)
4) How to read the email address in the Confirm email; i want it to be automatically read the user's email such as "Dear xxx@xxx.xxx" --this shud be automatic after we press "SEND" button