A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
filewriter not working
jonathan childs
Greenhorn
Joined: Jul 23, 2007
Posts: 4
posted
Jul 30, 2007 08:36:00
0
sorry put in general forum to but was unsure of whether was advanced problem or not
my filewriter code in this program doesnt seem to be executing anyone notice why?
import java.io.*; // for file writing part of assignment import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Marina3 implements ActionListener { //we have listed all the components that are needed in the gui below JButton exit = new JButton("exit"); String timearrive, timeleave; JButton enter = new JButton("submit button"); String[] priceStrings = { "Hour", "Daily", "Weekly", "Monthly", "Annual" }; JTextField cname = new JTextField(10); JTextField clength = new JTextField(10); int arrival, leave; JComboBox pricerate; String price; //This is a Graphical User Interface built in a Java application for the //java revisited courrsework public Marina3(){ JFrame Marina3 = new JFrame(); // i am addingActionListener to the appropriate buttons etc exit.addActionListener(this); enter.addActionListener(this); cname.addActionListener(this); clength.addActionListener(this); //I am now using panels display labels, Buttons etc which will be used to create the gui inerface JLabel welcomelabel = new JLabel("please enter details"); welcomelabel.setPreferredSize(new Dimension(175,100)); welcomelabel.setForeground(Color.red); JPanel FirstPanel = new JPanel(); FirstPanel.add(welcomelabel); FirstPanel.setBackground(Color.blue); JLabel label1 = new JLabel("Full Name"); label1.setPreferredSize(new Dimension(175,100)); label1.setForeground(Color.red); JLabel label2 = new JLabel("Length of yatch"); label2.setPreferredSize(new Dimension(175,100)); label2.setForeground(Color.red); JLabel label3 = new JLabel("Rate you would like"); label3.setPreferredSize(new Dimension(175,100)); label3.setForeground(Color.red); JLabel label4 = new JLabel("Price Rates available"); label4.setPreferredSize(new Dimension(175,100)); label4.setForeground(Color.red); JLabel label5 = new JLabel("Hourly Rate �0.50"); label5.setPreferredSize(new Dimension(175,100)); label5.setForeground(Color.red); JLabel label6 = new JLabel("Daily Rate �1.00"); label6.setPreferredSize(new Dimension(175,100)); label6.setForeground(Color.red); JLabel label7 = new JLabel("Weekly Rate �6.30"); label7.setPreferredSize(new Dimension(175,100)); label7.setForeground(Color.red); JLabel label8 = new JLabel("Monthly Rate �19.00"); label8.setPreferredSize(new Dimension(175,100)); label8.setForeground(Color.red); JLabel label9 = new JLabel("Annual Rate �132.00"); label9.setPreferredSize(new Dimension(175,100)); label9.setForeground(Color.red); ImageIcon image = new ImageIcon("tn_Sailboat_Cartoon_1.jpg"); JLabel label10 = new JLabel(image, SwingConstants.LEFT); pricerate = new JComboBox(priceStrings); pricerate.setSelectedIndex(0); pricerate.addActionListener(this); JPanel SecondPanel = new JPanel(); SecondPanel.setBackground(Color.blue); SecondPanel.add(label1); SecondPanel.add(cname); SecondPanel.add(label2); SecondPanel.add(clength); SecondPanel.add(label3); SecondPanel.add(pricerate); SecondPanel.add(exit); SecondPanel.add(enter); JPanel ThirdPanel = new JPanel(); ThirdPanel.setBackground(Color.blue); ThirdPanel.add(label4); ThirdPanel.add(label5); ThirdPanel.add(label6); ThirdPanel.add(label7); ThirdPanel.add(label8); ThirdPanel.add(label9); JPanel FourthPanel = new JPanel(); FourthPanel.setBackground(Color.blue); JPanel FifthPanel = new JPanel();; FifthPanel.setBackground(Color.blue); FifthPanel.add(label10); Marina3.getContentPane().add(FirstPanel, BorderLayout.NORTH); Marina3.getContentPane().add(SecondPanel, BorderLayout.CENTER); Marina3.getContentPane().add(ThirdPanel, BorderLayout.WEST); Marina3.getContentPane().add(FourthPanel, BorderLayout.EAST); Marina3.getContentPane().add(FifthPanel, BorderLayout.SOUTH); Marina3.pack(); Marina3.setVisible(true); } public void actionPerformed(ActionEvent e) { //if user wants to exit the system if ( e.getSource() == exit ) { System.exit(0); } //got to edit so if user presses button then something happens else if ( e.getSource() == enter ) { if (((String)pricerate.getSelectedItem()).equals("Hour")){ // joptionpanes for price rates available and time arrival and leave if applicable timearrive = JOptionPane.showInputDialog("please enter time you wish to enter the marina"); timeleave = JOptionPane.showInputDialog(" please enter time you wish to leave the marina"); JOptionPane.showMessageDialog(null, "Your total charge is �0.50"); } else if (((String)pricerate.getSelectedItem()).equals("Daily")){ JOptionPane.showMessageDialog(null, "Your total charge is �1.00"); } else if (((String)pricerate.getSelectedItem()).equals("Weekly")){ JOptionPane.showMessageDialog(null, "Your total charge is �6.30"); } else if (((String)pricerate.getSelectedItem()).equals("Monthly")){ JOptionPane.showMessageDialog(null, "Your total charge is �19.00"); } else if (((String)pricerate.getSelectedItem()).equals("Annual")){ JOptionPane.showMessageDialog(null, "Your total charge is �132.00"); } JOptionPane.showMessageDialog(null,"Thank you for your custom and will contact you shortly to confirm your booking"); //if applicable for the time arriving and leaving of the customer String name = cname.getText(); int length = Integer.parseInt(clength.getText()); String price = (String)pricerate.getSelectedItem(); arrival = Integer.parseInt(timearrive); leave = Integer.parseInt(timeleave); try { FileWriter fw = new FileWriter ("cdetails.txt"); BufferedWriter out = new BufferedWriter(fw); out.write((String)name + " " + (int)length + " " + (String)price + " " + (int)arrival + " " + (int)leave); out.close(); } catch (IOException b){b.printStackTrace();} } //end of actionperformed method } public static void main(String[] args) { //I have created the gui new Marina3(); } }
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8263
I like...
posted
Jul 30, 2007 09:59:00
0
Please do not post the
same topic
more than once. It causes confusion and duplication of effort as the community tries to help everyone.
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[
How To Ask Questions On JavaRanch
]
I agree. Here's the link:
http://aspose.com/file-tools
subject: filewriter not working
Similar Threads
gui problems
Java Board Game
filewriter not working
jmenubar,menuitem
Java Guessing Game - OutOfMemoryError
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter