Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

course substitutions

$
0
0
I must write a interface to enter course information for transfer students. The created data file should be sequential in nature, with the student's name, his/her ID number, the course number from the previous college, and the accepted course number at the current college.

The code compiles fine but when I run the program there is no textboxes where I can enter my data can someone please show me what I did wrong, i cannot figure it out.

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 each row
		JPanel firstRow = new JPanel();
		JPanel secondRow = new JPanel();
		JPanel thirdRow = new JPanel();
		JPanel fourthRow = new JPanel();

		//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(35);
		JLabel idLabel = new JLabel("Student ID:");
		JTextField id = new JTextField(40);
		JLabel transferLabel = new JLabel("Transfer Course Number:");
		JTextField transfer = new JTextField(45);
		JLabel courseLabel = new JLabel("Local Course Number:");
		JTextField course = new JTextField(40);

		//Construct buttons
		JButton submitButton = new JButton("Submit");
		JButton exitButton = new JButton("Exit");

	public static void main(String[] args)
	{
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "The UIMangament could not set the Look and Feel for this application.", "Error", JOptionPane.INFORMATION_MESSAGE);
		}
		Transfer f = new Transfer();
		f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		f.setSize(450, 300);
		f.setTitle("Transfer Course Substitutions");
		f.setResizable(false);
		f.setLocation(300, 200);
		f.setVisible(true);
	}

	public Transfer()
	{
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		fieldPanel.setLayout(new GridLayout(4,2));
		FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,4,20);
		firstRow.setLayout(rowSetup);
		secondRow.setLayout(rowSetup);
		thirdRow.setLayout(rowSetup);
		fourthRow.setLayout(rowSetup);
		buttonPanel.setLayout(new FlowLayout (FlowLayout.CENTER));


		//add fields to rows
		firstRow.add(nameLabel);
		secondRow.add(idLabel);
		thirdRow.add(transferLabel);
		fourthRow.add(courseLabel);

		//add JTextFields to rows
		JTextField nameLabel = new JTextField(35);
		JTextField idLabel = new JTextField(40);
		JTextField transferLabel = new JTextField(40);
		JTextField courseLabel = new JTextField(45);

		//add rows to panel
		fieldPanel.add(firstRow);
		fieldPanel.add(secondRow);
		fieldPanel.add(thirdRow);
		fieldPanel.add(fourthRow);

		//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
		{
			output = new DataOutputStream(new FileOutputStream("Transfer.dat"));
		}
		catch(IOException io)
		{
			System.exit(1);
		}
		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 submission", JOptionPane.YES_NO_OPTION);
					if (answer == JOptionPane.YES_OPTION);
					System.exit(0);
				}
			}
			);
		}

		public void actionPerformed(ActionEvent e)
		{
			String arg = e.getActionCommand();
			if (arg == "Submit")
			{
				try
				{
					output.writeUTF(name.getText());
					output.writeUTF(id.getText());
					output.writeUTF(transfer.getText());
					output.writeUTF(course.getText());

					JOptionPane.showMessageDialog(null, "The Information has been saved.", "Submission successful", JOptionPane.INFORMATION_MESSAGE);
				}
				catch(IOException c)
				{
					System.exit(1);
				}
				clearFields();
			}
			else
			{
				try
				{
					output.close();
				}
				catch(IOException c)
				{
					System.exit(0);
				}
			}
		}

		public boolean checkFields()
		{
			if((name.getText().compareTo("")<1)			||
			  (id.getText().compareTo("")<1)			||
			  (transfer.getText().compareTo("")<1)		||
			  (course.getText().compareTo("")<1))
			  {
				  JOptionPane.showMessageDialog(null, "You must complete all fields.", "Data Entry Error", JOptionPane.WARNING_MESSAGE);
				  return false;
			  }
			  else
			  {
				  return true;
			  }
		  }

		  public void clearFields()
		  {
			  name.setText("");
			  id.setText("");
			  transfer.setText("");
			  course.setText("");
		  }
	  }

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>