7 Replies - 1154 Views - Last Post: 11 September 2012 - 02:08 PM Rate Topic: -----

#1 Blwurcul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 21-February 11

Creating a GUI for Ship classes

Posted 07 September 2012 - 11:06 AM

Hello there,

I am trying to complete the design of three ship classes. I think I have the classes (and their inheritance) set up properly. But now I need to create a test program that has a graphical user interface which allows the user to enter the information for a ship and includes a set of radio buttons to select the type of ship to create.

I've set up a GUI shell but after that I'm a little lost in the woods. Any input would be appreciated!

Here is the GUI shell:
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;

/* This will be the UI shell */
public class ShipGUI 
{ 
  
    public static void main(String[] args) 
    { 
        //Creates and set up the window 
        JFrame frame = new JFrame("ShipGUI"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  
        //Creates the menu bar 
        JMenuBar greenMenuBar = new JMenuBar(); 
        greenMenuBar.setOpaque(true); 
        greenMenuBar.setBackground(new Color(154, 165, 127)); 
        greenMenuBar.setPreferredSize(new Dimension(200, 20)); 
  
        //Creates a yellow label 
        JLabel yellowLabel = new JLabel(); 
        yellowLabel.setOpaque(true); 
        yellowLabel.setBackground(new Color(248, 213, 131)); 
        yellowLabel.setPreferredSize(new Dimension(200, 180)); 
  
        //Set the menu bar and add the label to the content pane. 
        frame.setJMenuBar(greenMenuBar); 
        frame.getContentPane().add(yellowLabel, BorderLayout.CENTER); 
  
        //Display the window. 
        frame.pack(); 
        frame.setVisible(true);
        
    } 

}



And here are the three ship classes:

SHIP
 

import java.awt.*;
import javax.swing.*;

public class Ship
{
    private String ShipName;
    private String Year;
    
    //blank constructor
    public Ship()
    {
    	
    } 
    
    //add year and name to construtor
    public Ship(String ShipName,String Year)
    {
        SetShipName(ShipName);    
        SetYear(Year);
    }
    
    //sets and gets for the data feilds
    public void SetShipName(String ShipName)
    {
        this.ShipName = ShipName;
    }
    
    public String GetShipName()
    {
        return this.ShipName;
    }
    
    public void SetYear(String Year)
    {
        this.Year = Year;
    }
    public String GetYear()
    {
        return this.Year;
    }
    
    //toString returns a string for the data in the class
   public String toString()
   {
        String strOut;
        strOut = "Ship Name: " + this.ShipName + "\nYear: " + this.Year; 
        return strOut;
    }
}



CRUISE
 
public class CruiseShip extends Ship
{
    //holds passenger capacity
    private int MaxPassengers;
    
    //blank constructor
    public CruiseShip()
    {
    	
    }
    
    //a construtor with all data for this class
    public CruiseShip(String ShipName, String Year, int MaxPassengers)
    {
        super(ShipName, Year);//class the super class constructor for this data
        this.MaxPassengers=MaxPassengers;
    }
    
    //set and get for the Passeneger feild
    public void SetMaxPassengers(int MaxPassengers)
    {
        this.MaxPassengers = MaxPassengers;
    }
    
    public int GetMaxPassengers()
    {
        return this.MaxPassengers;
    }
    
    //tostring will override the toString in the Ship class
    public String toString()
    {
        String strOut;
        strOut = "Ship Name: " + GetShipName() + "\nMax Passenegers: " + GetMaxPassengers(); 
        return strOut;
    }
}


CARGO
 

public class CargoShip extends Ship
{	
    //hold the tonnage the ship can hold
    private int CargoCapcity;
    
    //a blank constructor
    public CargoShip()
    {
    	
    }
    
    //a constructor with all data needed for this class
    public CargoShip(String ShipName, String Year, int CargoCapcity)
    {
        super(ShipName, Year);//send this data to the super class Ship
        this.CargoCapcity = CargoCapcity;
    }
    
    //set and get for the tonnage
    public void SetCargoCapcity(int CargoCapcity)
    {
        this.CargoCapcity = CargoCapcity;
    }
    
    public int GetCargoCapcity()
    {
        return this.CargoCapcity;
    }
    
    //toString overrides the superclass 'Ship' toString Method
    public String toString()
    {
        String strOut;
        strOut = "Ship Name: " + GetShipName() + "\nCargo Capcity " + GetCargoCapcity()+" tons"; 
        return strOut;
    }
}



Again, thanks in advance for your time. Getting this all set would be a huge help!

This post has been edited by Blwurcul: 07 September 2012 - 11:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a GUI for Ship classes

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Creating a GUI for Ship classes

Posted 07 September 2012 - 01:25 PM

You're going to need to read up on Swing and design a form to input ship data. You will need JRadioButton, JTextField, JButton, and JLabel. Then you will want to find the LayoutManager(s) that set it up the way you want and then make an ActionListener to record the data when the button is pressed.
Was This Post Helpful? 0
  • +
  • -

#3 Blwurcul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 21-February 11

Re: Creating a GUI for Ship classes

Posted 07 September 2012 - 02:20 PM

Yes, clearly I'm a rookie when it comes to setting up GUI's.

I am hoping for an example to go by. I'll be extending functionality of this code in coming assignments so want to make sure I get a good base (and understanding) of how to implement JOptionPane GUIs.
Was This Post Helpful? 0
  • +
  • -

#4 Blwurcul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 21-February 11

Re: Creating a GUI for Ship classes

Posted 07 September 2012 - 02:58 PM

Ok, I've found a great model for the JOptionPane GUI I need and I've set it up to reflect radio button choices for my different ships.

Now, if I could just get some help as to how I pull in the different class info based on the radio button selection and display the associated info...I'll be all set.

Here's where I'm at:

 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 

public class GUITest extends JPanel
                             implements ActionListener {
    static String shipString = "Ship";
    static String cruiseString = "Cruise Ship";
    static String cargoString = "Cargo Ship";

 
    JLabel picture;
 
    public GUITest() {
        super(new BorderLayout());
 
        //Create the radio buttons.
        JRadioButton shipButton = new JRadioButton(shipString);
        shipButton.setMnemonic(KeyEvent.VK_B)/>;
        shipButton.setActionCommand(shipString);
        shipButton.setSelected(true);
 
        JRadioButton cruiseButton = new JRadioButton(cruiseString);
        cruiseButton.setMnemonic(KeyEvent.VK_C);
        cruiseButton.setActionCommand(cruiseString);
 
        JRadioButton cargoButton = new JRadioButton(cargoString);
        cargoButton.setMnemonic(KeyEvent.VK_D);
        cargoButton.setActionCommand(cargoString);
 
 
        //Group the radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(shipButton);
        group.add(cruiseButton);
        group.add(cargoButton);

 
        //Register a listener for the radio buttons.
        shipButton.addActionListener(this);
        cruiseButton.addActionListener(this);
        cargoButton.addActionListener(this);

 
        //Set up the picture label.
        picture = new JLabel(createImageIcon("images/"
                                             + shipString
                                             + ".gif"));
 
        //The preferred size is hard-coded to be the width of the
        //widest image and the height of the tallest image.
        //A real program would compute this.
        picture.setPreferredSize(new Dimension(177, 122));
 
 
        //Put the radio buttons in a column in a panel.
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(shipButton);
        radioPanel.add(cruiseButton);
        radioPanel.add(cargoButton);

 
        add(radioPanel, BorderLayout.LINE_START);
        add(picture, BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }
 
    /** Listens to the radio buttons. */
    public void actionPerformed(ActionEvent e) {
        picture.setIcon(createImageIcon("images/"
                                        + e.getActionCommand()
                                        + ".gif"));
    }
 
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = GUITest.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
 
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Ship It!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        JComponent newContentPane = new GUITest();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}



Was This Post Helpful? 0
  • +
  • -

#5 Blwurcul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 21-February 11

Re: Creating a GUI for Ship classes

Posted 08 September 2012 - 02:04 PM

So just to be clear, what I'm looking to have the GUI do is this:

1) User enters the data for each type of ship (name, year, capacity, passengers).
2) User selects the type of ship (ship, cargo or cruise) using a radio button function.
3) Then the driver displays the info for each ship created.

Really appreciate any help and time offered here. It's giving me trouble.
Was This Post Helpful? 0
  • +
  • -

#6 Blwurcul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 21-February 11

Re: Creating a GUI for Ship classes

Posted 10 September 2012 - 07:35 AM

Ok,

I think I'm getting closer here. But I'm still not producing my GUI. I think I have my logic in place but there seems to be a problem at the public ShipDemo() (defining my panel).

If anyone could give me some insight, it would be be much appreciated:

 

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ShipDemo extends Ship 
{
	private JPanel panel; 
	private JLabel messageLabel; 
	private JLabel messageLabel1; 
	private JLabel messageLabel2;
	private JLabel messageLabel3;
	
	private JTextField output; 
	private JTextField TextField; 
	private JTextField TextField1;
	private JTextField TextField2;
	
	private JRadioButton cruiseButton;      
    private JRadioButton cargoButton;       
    private JRadioButton shipButton;
    private ButtonGroup radioButtonGroup; 
 
	private final int WINDOW_WIDTH = 500; 
	private final int WINDOW_HEIGHT = 200;
	
	public ShipDemo()
	{
		setTitle("Ship Information Center");
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		setLayout(new BorderLayout(13,15));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		buildPanel();
		add(panel);
		setVisible(true);
	}
	
	private void buildPanel()
	{
		messageLabel = new JLabel("Enter the ship's name: ");
		TextField = new JTextField(20);
		
		messageLabel1 = new JLabel("Enter the ship's year: ");
		TextField1 = new JTextField(20);
		
		messageLabel2 = new JLabel("Enter Cappacity for Cargo Ship"+
						"/nEnter Max Passengers for Cruise Ship");
		TextField2 = new JTextField(20);
		
		messageLabel3 = new JLabel("Enter Information For 3 Different"+ " Ships");
	
		// Groups the radio buttons.
		radioButtonGroup = new ButtonGroup();
		radioButtonGroup.add(cruiseButton);
		radioButtonGroup.add(cargoButton);
		radioButtonGroup.add(shipButton);
		
		// Add action listeners to the radio buttons.
		cruiseButton.addActionListener(new RadioButtonListener());
		cargoButton.addActionListener(new RadioButtonListener());
		shipButton.addActionListener(new RadioButtonListener());
		
		output = new JTextField(30);
		output.setEditable(false);

		// Create a panel and add the components to it.
		panel = new JPanel();
		panel.add(messageLabel);
		panel.add(messageLabel1);
		panel.add(messageLabel2);
		panel.add(messageLabel3);
		panel.add(TextField);
		panel.add(TextField1);
		panel.add(TextField2);
		
		panel.add(output);
		panel.add(cargoButton);
		panel.add(cruiseButton);
		panel.add(shipButton);
	}
	
	
	/*Private inner class that handles the event when
      the user clicks one of the radio buttons. */
   private class RadioButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
		  // To hold the user's input
         String shipName;
         String Year;  
         String shipVariable;       
         
         // Get shipName and shipYear
         shipName = TextField.getText();
         Year = TextField1.getText();
         shipVariable = TextField2.getText();
         
         Ship[] s = new Ship[3];
         
         for (int index=0;index<s.length;index++)
         {
			 s[index]= new Ship();
			 
			// Determine which radio button was clicked.
			if (e.getSource() == cargoButton)
			{
				int capacity;
				shipName = TextField.getText();
				Year = TextField1.getText();
				shipVariable = TextField2.getText();
				
				capacity = Integer.parseInt(shipVariable);
				
				CargoShip ship1 = new CargoShip(Year,shipName,capacity);
				output.setText(ship1.toString()); 
			}
			else if (e.getSource() == cruiseButton)
			{
				output.setText("Working");
           
			}
			else if (e.getSource() == shipButton)
			{
				output.setText("Working");
            
			}
		}
	}
}
	
public static void main (String args[]) 
	{
		 new ShipDemo();
	}
		
}


Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Creating a GUI for Ship classes

Posted 10 September 2012 - 10:19 AM

Does BorderLayout let you use two int parameters? Also, unless Ship extends JFrame, where is your JFrame?
Was This Post Helpful? 0
  • +
  • -

#8 Blwurcul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 21-February 11

Re: Creating a GUI for Ship classes

Posted 11 September 2012 - 02:08 PM

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1