Echo Leader
Posts : 985 Join date : 2011-05-15 Age : 32 Location : Columbus
| Subject: My JAVA Project Mon Jun 20, 2011 2:29 am | |
| No, I didn't get it completely finished, but I did get all this done: - Quote :
- import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.*;
/********************************************************************* Program Name: final Programmer's Name: Andrew Program Description: This program will do a lot of stuff **********************************************************************/
public class Final extends JFrame { //define tab 1 class variables //define tab 2 class variables private JButton bCalcVolume, bExit; private JTextField setName, lengthSet, widthSet, depthSet, volumeSet; //define tab 3 class variables private JFrame mainFrame; private JButton bRefresh, bAddCust; //tab 3 add customer private JFrame mainFrame2; private JTextField custName, custAddress, custCity, custState, custZip, custPhone; private JLabel custLabel, addressLabel, cityLabel, stateLabel, zipLabel, phoneLabel, warningLabel; private JPanel panel; private JButton bAddCustomer, bClose, bDeleteFile, bYes, bNo; private JTextArea statusBox; //define tab 5 class variables private JButton bSetName; public Final() { super( "Final Project" );
JTabbedPane tab = new JTabbedPane(); // constructing the first panel JLabel l2 = new JLabel("Today's Date: ", SwingConstants.CENTER); Calendar cal = new GregorianCalendar(); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); int day = cal.get(Calendar.DAY_OF_MONTH); l2.setText("Today's Date: " + month + "/" + day + "/" + year); JPanel p2 = new JPanel(); p2.add( l2 ); bExit = new JButton("Exit"); p2.add(bExit); //set the mnemonic of each button bExit.setMnemonic('x'); //button listeners bExitHandler e1Handler = new bExitHandler(); bExit.addActionListener(e1Handler); tab.addTab( "General", null, p2, "General" ); /***********************END OF PANEL 1****************************/ /*****************************************************************/ // constructing the second panel JLabel L1a = new JLabel( "Enter the Length of the swimming pool:"); JPanel p1 = new JPanel(); p1.add( L1a ); lengthSet = new JTextField(6); p1.add( L1a ); p1.add( lengthSet ); JLabel L1b = new JLabel( "Enter the Width of the swimming pool:"); p1.add( L1b ); widthSet = new JTextField(6); p1.add( widthSet ); JLabel L1c = new JLabel( "Enter the average Depth of the swimming pool:"); p1.add( L1c ); depthSet = new JTextField(6); p1.add( depthSet ); JLabel L1d = new JLabel( "Swimming Pool Volume:"); p1.add( L1d ); volumeSet = new JTextField(6); p1.add( volumeSet ); bCalcVolume = new JButton("Calculate Volume"); p1.add(bCalcVolume); bExit = new JButton("Exit"); p1.add(bExit); tab.addTab( "Options", null, p1, "Options" ); //set the mnemonic of each button bCalcVolume.setMnemonic('C'); bExit.setMnemonic('x'); //button listeners bExitHandler eHandler = new bExitHandler(); bExit.addActionListener(eHandler); bCalcVolumeHandler eHandler1 = new bCalcVolumeHandler(); bCalcVolume.addActionListener(eHandler1); tab.addTab( "Swimming Pool Calc", null, p1, "Swimming Pool Calc" ); /***********************END OF PANEL 2****************************/ /*****************************************************************/ // constructing the third panel JTextArea custDisplay = new JTextArea(10,30); custDisplay.setEditable(false); JPanel p3 = new JPanel(); p3.add(custDisplay); bRefresh = new JButton("Refresh"); p3.add(bRefresh); bAddCust = new JButton("Add Customer"); p3.add(bAddCust); bExit = new JButton("Exit"); p3.add(bExit); //set the mnemonic of each button bAddCust.setMnemonic('A'); bRefresh.setMnemonic('R'); bExit.setMnemonic('x'); //button listeners bExitHandler eHandler2 = new bExitHandler(); bExit.addActionListener(eHandler2); bRefreshHandler eHandler3 = new bRefreshHandler(); bRefresh.addActionListener(eHandler3); bAddCustHandler eHandler4 = new bAddCustHandler(); bAddCust.addActionListener(eHandler4); tab.addTab( "Customers", null, p3, "Customers" ); /***********************END OF PANEL 3****************************/ /*****************************************************************/ // constructing the fourth panel JLabel l4 = new JLabel( "Time" ); JPanel p4 = new JPanel(); JLabel saying = new JLabel( "There was not enough." ); saying.setFont( new Font( "Serif", Font.ITALIC, 20 ) ); p4.add( saying ); tab.addTab( "Time", null, p4, "Time" ); /***********************END OF PANEL 4****************************/ /*****************************************************************/ // constructing the last panel JLabel l5 = new JLabel( "option panel" ); JLabel l6 = new JLabel( "Change Company Name:" ); setName = new JTextField(20); JPanel p5 = new JPanel(); p5.add( l6 ); p5.add( setName ); bSetName = new JButton("Set New Name"); p5.add(bSetName); bExit = new JButton("Exit"); p5.add(bExit); tab.addTab( "Options", null, p5, "Options" ); //set the mnemonic of each button bSetName.setMnemonic('S'); bExit.setMnemonic('x'); //button listeners bExitHandler eHandler10 = new bExitHandler(); bExit.addActionListener(eHandler10); bSetNameHandler eHandler11 = new bSetNameHandler(); bSetName.addActionListener(eHandler11);
// add JTabbedPane to container getContentPane().add( tab ); setSize( 370, 500 ); setVisible( true ); } /***********************END OF PANEL 5****************************/ /*****************************************************************/ //set button to call add customer form class bAddCustHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { //initialize all class variables mainFrame = new JFrame(""); bAddCustomer = new JButton("Add Customer"); bClose = new JButton("Close"); bDeleteFile = new JButton("Delete File"); custLabel = new JLabel("Customer Name:"); custName = new JTextField(15); addressLabel = new JLabel("Address:"); custAddress = new JTextField(20); cityLabel = new JLabel("City:"); custCity = new JTextField(20); stateLabel = new JLabel("State:"); custState = new JTextField(2); zipLabel = new JLabel("ZIP:"); custZip = new JTextField(4); phoneLabel = new JLabel("Phone:"); custPhone = new JTextField(7); panel = new JPanel(); statusBox = new JTextArea(3,25); statusBox.setEditable(false);
//add a window listener to the main frame //so that it closes when x is pressed on the top //right corner of the GUI. mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
//get a content pane for the frame and add it to a container Container c = mainFrame.getContentPane(); //set the container layout c.setLayout(new BorderLayout());
//add all components to the panel panel.add(custLabel); panel.add(custName); panel.add(addressLabel); panel.add(custAddress); panel.add(cityLabel); panel.add(custCity); panel.add(stateLabel); panel.add(custState); panel.add(zipLabel); panel.add(custZip); panel.add(phoneLabel); panel.add(custPhone); panel.add(bAddCustomer); panel.add(bClose); panel.add(bDeleteFile); panel.add(statusBox); //add the panel to the container c.add(panel);
//set the mnemonic of each button bAddCustomer.setMnemonic('A'); bClose.setMnemonic('C'); bDeleteFile.setMnemonic('D'); //button listeners bAddCustomerHandler eHandler = new bAddCustomerHandler(); bAddCustomer.addActionListener(eHandler); bCloseHandler eHandler1 = new bCloseHandler(); bClose.addActionListener(eHandler1); bDeleteFileHandler eHandler2 = new bDeleteFileHandler(); bDeleteFile.addActionListener(eHandler2); //set the size of the frame and visibility mainFrame.setSize(310, 320); mainFrame.setVisible(true); } //set delete file button class bDeleteFileHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { //initialize all class variables mainFrame2 = new JFrame(""); bYes = new JButton("Yes"); bNo = new JButton("No"); warningLabel = new JLabel("Are you sure you want to delete customer.txt?"); panel = new JPanel();
//add a window listener to the main frame //so that it closes when x is pressed on the top //right corner of the GUI. mainFrame2.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
//get a content pane for the frame and add it to a container Container c = mainFrame2.getContentPane(); //set the container layout c.setLayout(new BorderLayout());
//add all components to the panel panel.add(warningLabel); panel.add(bYes); panel.add(bNo); //add the panel to the container c.add(panel);
//set the mnemonic of each button bYes.setMnemonic('Y'); bNo.setMnemonic('N'); //button listeners bYesHandler eHandler = new bYesHandler(); bYes.addActionListener(eHandler); bNoHandler eHandler1 = new bNoHandler(); bNo.addActionListener(eHandler1); //set the size of the frame and visibility mainFrame2.setSize(310, 100); mainFrame2.setVisible(true); } } } //set no button class bNoHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { System.exit(0); } } //set yes button class bYesHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { boolean success = (new File("customers")).delete(); if (!success) { statusBox.setText("Delete command failed!"); } } } //set close button class bCloseHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { System.exit(0); } } //set add customer info button class bAddCustomerHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { //reset error colored fields custZip.setBackground(Color.WHITE); custPhone.setBackground(Color.WHITE); //check for zip and Phone number fields to be numbers try { String zipCheck = custZip.getText(); double zip = Double.parseDouble(zipCheck); try { String phoneCheck = custPhone.getText(); double phone = Double.parseDouble(phoneCheck); //Will write to the customers.txt file try { FileWriter fileW = new FileWriter("customers.txt", true); System.out.println("Writing data to customers.txt file"); fileW.write("Name: "); fileW.write(custName.getText()); fileW.write(","); fileW.write(" "); fileW.write("Address: "); fileW.write(custAddress.getText()); fileW.write(","); fileW.write(" "); fileW.write("City: "); fileW.write(custCity.getText()); fileW.write(","); fileW.write(" "); fileW.write("State: "); fileW.write(custState.getText()); fileW.write(","); fileW.write(" "); fileW.write("ZIP: "); fileW.write(custZip.getText()); fileW.write(","); fileW.write(" "); fileW.write("Phone: "); fileW.write(custPhone.getText()); fileW.close(); FileReader fileR = new FileReader("customers.txt"); BufferedReader buffIn = new BufferedReader(fileR); System.out.println("Done!"); buffIn.close(); statusBox.setText("Customer added!"); } catch(IOException e1) { statusBox.setText("Adding the customer failed!"); } } catch(Exception e) { custPhone.setBackground(Color.YELLOW); statusBox.setText("Please enter a valid phone number."); } } catch(Exception e) { custZip.setBackground(Color.YELLOW); statusBox.setText("Please enter a valid zip code."); } } } //set refresh button class bRefreshHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { //refresh button code } } //set calculate volume button class bCalcVolumeHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { //reset error colored fields widthSet.setBackground(Color.WHITE); lengthSet.setBackground(Color.WHITE); depthSet.setBackground(Color.WHITE); try { String userDepth = depthSet.getText(); String userWidth = widthSet.getText(); String userLength = lengthSet.getText(); //convert user input to doubles double depth = Double.parseDouble(userDepth); double width = Double.parseDouble(userWidth); double length = Double.parseDouble(userLength); double volume = length * width * depth; //Will calculate the volume of the pool String Volume = Double.toString(volume); volumeSet.setText(Volume); } catch(Exception e) { JOptionPane.showMessageDialog(null,"Please enter valid input.", "ERROR",2); //Will display if input is incorrect try { String userLength = lengthSet.getText(); double length = Double.parseDouble(userLength); } catch(Exception e1) { lengthSet.setBackground(Color.YELLOW); } try { String userWidth = widthSet.getText(); double width = Double.parseDouble(userWidth); } catch(Exception e2) { widthSet.setBackground(Color.YELLOW); } try { String userDepth = depthSet.getText(); double depth = Double.parseDouble(userDepth); } catch(Exception e3) { depthSet.setBackground(Color.YELLOW); } } { //Will write to the Data.txt file try { FileWriter fileW = new FileWriter("Data.txt", true); System.out.println("Writing data to Data.txt file"); fileW.write("length:"); fileW.write(lengthSet.getText()); fileW.write(","); fileW.write(" "); fileW.write("width:"); fileW.write(widthSet.getText()); fileW.write(","); fileW.write(" "); fileW.write("avg depth:"); fileW.write(depthSet.getText()); fileW.write(","); fileW.write(" "); fileW.write("volume:"); fileW.write(volumeSet.getText()); fileW.write(","); fileW.close(); FileReader fileR = new FileReader("Data.txt"); BufferedReader buffIn = new BufferedReader(fileR); String textData = buffIn.readLine(); System.out.println(textData); buffIn.close(); System.out.println("\n"); } catch(IOException e1) { JOptionPane.showMessageDialog(null,e1.getMessage(), "ERROR",2); //Will display error message if unable to write to file } } } } //set new name button class bSetNameHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { String title = setName.getText(); setTitle(title); } } //exit button class bExitHandler implements ActionListener { public void actionPerformed(ActionEvent arg0) { System.exit(0); } }
public static void main( String args[] ) { Final demo = new Final(); demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
Last edited by Echo on Mon Jun 20, 2011 2:26 pm; edited 1 time in total | |
|
JohnPwnsYou
Posts : 456 Join date : 2011-05-21
| Subject: Re: My JAVA Project Mon Jun 20, 2011 3:21 am | |
| | |
|
Echo Leader
Posts : 985 Join date : 2011-05-15 Age : 32 Location : Columbus
| Subject: Re: My JAVA Project Mon Jun 20, 2011 3:33 am | |
| It does a few things. It has a tabbed interface, it calculates the volume of shit (which is then added to data.txt) you can add customers (which are then written to customers.txt) which can then be retrieved and listed and other shit like that. Nothing really useful, but it was for us to use every technique we've learned over the past 8 weeks | |
|
Skies
Posts : 86 Join date : 2011-05-22 Age : 31 Location : Texshus
| Subject: Re: My JAVA Project Mon Jun 20, 2011 1:48 pm | |
| | |
|
Shottymaster
Posts : 734 Join date : 2011-05-15 Age : 28 Location : Peoria, AZ
| Subject: Re: My JAVA Project Mon Jun 20, 2011 2:05 pm | |
| I can do tons of useless shit in VB and C#. | |
|
Echo Leader
Posts : 985 Join date : 2011-05-15 Age : 32 Location : Columbus
| Subject: Re: My JAVA Project Mon Jun 20, 2011 2:22 pm | |
| Java was the class I was taking :p | |
|
Malicih
Posts : 87 Join date : 2011-05-16 Age : 29
| Subject: Re: My JAVA Project Sun Jun 26, 2011 7:49 pm | |
| lol VB. Most useless class I've ever taken. I probably could have done the entire book they gave us in like 2 weeks max. To bad I spent that whole class playing computer games....
I really don't want to but it'd probably be good to get a start on learning java and the basics of other coding languages before college huh? | |
|
Skies
Posts : 86 Join date : 2011-05-22 Age : 31 Location : Texshus
| Subject: Re: My JAVA Project Sat Jul 02, 2011 1:45 pm | |
| C++ is a good one to learn. Java is too, but it teaches bad coding habits. I started with python. | |
|
Shottymaster
Posts : 734 Join date : 2011-05-15 Age : 28 Location : Peoria, AZ
| Subject: Re: My JAVA Project Sun Jul 03, 2011 1:49 am | |
| I taught myself MS-DOS batch coding in like, 7th or 8th grade ._. Not really a coding language, but still can teach basics. I wrote a calculator and some other useless stuff that does basically nothing. | |
|
Skies
Posts : 86 Join date : 2011-05-22 Age : 31 Location : Texshus
| Subject: Re: My JAVA Project Wed Jul 06, 2011 10:25 am | |
| Mmm, shell scripting. It's quite useful to know in certain situations. Especially in Linux environments. | |
|
Sponsored content
| Subject: Re: My JAVA Project | |
| |
|