hello all i have a problem with a gui program i have created.
import java.io.*; // for file writing part of assignment
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.*;
public class Marina implements ActionListener
{
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);
private JComboBox pricerate;
//This is a Graphical User Interface built in a Java application for the
//java revisited courrsework
public Marina(){
JFrame Marina = new JFrame();
//I am now using panels display labels, Buttons etc which will be used to create the gui inerface
JLabel welcomelabel = new JLabel("please enter your details below");
welcomelabel.setPreferredSize(new Dimension(175,100));
welcomelabel.setForeground(Color.red);
JPanel FirstPanel = new JPanel();
FirstPanel.add(welcomelabel);
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);
enter.addActionListener(this);
exit.addActionListener(this);
cname.addActionListener(this);
clength.addActionListener(this);
pricerate = new JComboBox(priceStrings);
pricerate.setSelectedIndex(1);
pricerate.addActionListener(this);
JPanel SecondPanel = new JPanel();
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.add(label4);
ThirdPanel.add(label5);
ThirdPanel.add(label6);
ThirdPanel.add(label7);
ThirdPanel.add(label8);
ThirdPanel.add(label9);
JPanel FourthPanel = new JPanel();
// add boat image or something here if got time
JPanel FifthPanel = new JPanel();;
// add boat image or something here if got time
//i am now going to add the panels to the frame so all the labels and
//textfields etc are displayed
Marina.getContentPane().add(FirstPanel, BorderLayout.NORTH);
Marina.getContentPane().add(SecondPanel, BorderLayout.CENTER);
Marina.getContentPane().add(ThirdPanel, BorderLayout.WEST);
Marina.getContentPane().add(FourthPanel, BorderLayout.EAST);
Marina.getContentPane().add(FifthPanel, BorderLayout.SOUTH);
Marina.pack();
Marina.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//if command for when the user wants an hourly rate
if ( e.getSource() == exit ) {
System.exit(0); //still not working find out why
}
//got to edit so if user presses button then something happens
else if ( e.getSource() == enter ) {
if (((String)pricerate.getSelectedItem()).equals("hour")) {
timearrive = JOptionPane.showInputDialog("please enter time you wish to enter the marina");
timeleave = JOptionPane.showInputDialog(" please enter time you wish to leave the marina");
//gonna use the scanner now to put input into cdetails text file
String name = cname.getText();
int length = Integer.parseInt(length.getText());
String price.getSelectedItem(length,getText());
//Strings to be edited so time customers arrives and leaves can be stored in file
String arrival =
String leave =
JOptionPane.showMessageDialog(null,"Thank you for your custom and will contact you shortly to confirm your booking");
//import a scanner class here
}
}
// a file writer to be inserted later to write customer details into the program
//I have imported a sperate class into this class containing the code for the filewirting process
//if user gives one of the other rates
else
{
//import price class here
JOptionPane.showMessageDialog(null, "Thank you for your custom we have stored your details and will contact you shortly to confirm your booking");
}
// import a scanner class here
//I have improted a sperate class into this class containing the code for the filewirting process
}
//the main method for my program
public static void main(String[] args) throws IOException {
//I have created the gui
new Marina();
}
}
at the moment a gui is displaying but changes have been made since then.
the main things i want to do are
my joption panes show up when i change options on my jcombobox but i want them to show up only when i press the submit button. the command i have wrote to try and do this is in my actionperformed method.
i have created strings called arrival and leave from which i wish to get the text entered into the input joption panes timearrive and timeleave but i am unsure of the command needed to be written to do this
also i have this other class ( i would use a scanner but have been told to use a filweriter and been given example code to help me create the class) which i want to write the strings etc into a document i have created called cdetails.txt but currently has problems. i plan to put this class into my program as shown as above later on when correct
import java.util.*;
import java.io.*;
public class WriteToFile2 {
String name, price, arrival, leave;
int length;
public static void main(String args[]) {
try {
FileWriter fw = new FileWriter ("cdetails.txt");
BufferedWriter out = new BufferedWriter(fw);
// need to get a enter command or something working here out.write("");
out.write(name + " " + length + " " + price + " " + arrival + " " + leave + " ");
}
catch (IOException e) {}
}
}