import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
public class Transfer extends JFrame implements ActionListener
{
//Declare output stream
DataOutputStream output;
//Construct a panel for the fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
//Construct labels and text boxes
JLabel nameLabel = new JLabel("Name: ");
JTextField name = new JTextField(30);
JLabel idLabel = new JLabel ("Student ID");
JTextField id = new JTextField(30);
JLabel courseNumLabel = new JLabel("Transfer Course Number: ");
JTextField courseNum = new JTextField(30);
JLabel localCourseLabel = new JLabel("Local Course Number:");
JTextField localCourse = new JTextField(30);
//Construct button
JButton submitButton = new JButton("Submit");
JButton exitButton = new JButton("Exit");
public static void main(String[] args)
{
Transfer f = new Transfer();
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.setSize(450,300);
f.setTitle("Transfer Course Substitutions");
f.setVisible(true);
} //ends main method
public Transfer()
{
Container c = getContentPane();
c.setLayout((new BorderLayout()));
fieldPanel.setLayout(new GridLayout(4,2));
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//Add fields to rows
fieldPanel.add(nameLabel);
fieldPanel.add(name);
fieldPanel.add(idLabel);
fieldPanel.add(id);
fieldPanel.add(courseNumLabel);
fieldPanel.add(courseNum);
fieldPanel.add(localCourseLabel);
fieldPanel.add(localCourse);
//Add button to panel
buttonPanel.add(submitButton);
buttonPanel.add(exitButton);
//Add Panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//add functionality to buttons
submitButton.addActionListener(this);
exitButton.addActionListener(this);
try
{
new DataOutputStream(new FileOutputStream("Transfer.dat"));
} //ends try statement
catch (IOException io)
{
System.exit(1);
} //ends catch statement
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submittions", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
System.exit(0);
} //ends windowClosing statement
} //ends window adpater
); //ends window listener
}//ends transfer constructor
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg == "Submit")
{
try
{
output.writeUTF(name.getText());
output.writeUTF(id.getText());
output.writeUTF(courseNum.getText());
output.writeUTF(localCourse.getText());
} //ends try statement
catch(IOException ex)
{
System.exit(1);
} //ends catch statement
clearFields();
} //ends if statement
else //code to execute if the user clicks Exit
{
try
{
output.close();
} //ends try statement
catch(IOException c)
{
System.exit(1);
}//ends catch statement
System.exit(0);
} //ends else statement
}//ends action performed method
public void clearFields()
{
//Clear fields and reset the focus
name.setText("");
id.setText("");
courseNum.setText("");
localCourse.setText("");
name.requestFocus();
} //ends clear fields method
} //ends transfer class
Ok I have attached the "weird" message I am getting in the command prompt in the back.
I dont understand what causes this. I think it comes from something in my action performed method, but I don't understand what.
Appreciate any guidance/help.