d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Need Help With An Assignment In Java
Prev12
Add Reply New Topic New Poll
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 10 2013 12:00pm
Quote (GetSpanked @ Sep 10 2013 01:23pm)
how would i get my code to run in a window rather than the command line?


Use Swing GUI toolkit

here's a tutorial

http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Sep 10 2013 03:10pm
Quote (labatymo @ Sep 10 2013 01:00pm)
Use Swing GUI toolkit

here's a tutorial

[URL=http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming]http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming[/URL]


I know I have to use GUI to do it, I just don't understand GUI.
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Sep 10 2013 03:13pm
by the way, i have this program completely working in a command prompt line.

It isn't required to use GUI to make windows for it, but I am interested in trying this.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 10 2013 04:04pm
Quote (GetSpanked @ Sep 10 2013 05:10pm)
I know I have to use GUI to do it, I just don't understand GUI.


hence the link he gave. you can also look at oracle's tutorials for layouts and such.

essentially you have containers that hold controls, layouts that determine how the controls looks, then controls react to user input via action listeners.
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Sep 10 2013 04:10pm
Quote (carteblanche @ Sep 10 2013 05:04pm)
hence the link he gave. you can also look at oracle's tutorials for layouts and such.

essentially you have containers that hold controls, layouts that determine how the controls looks, then controls react to user input via action listeners.


I am reading that, I have my code all done to run in a command prompt, im just having trouble understanding how to convert that into a GUI panel from the code that I have. I am new to programming in general and java is the only language that I am attempting to learn at this moment.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 10 2013 04:15pm
Quote (GetSpanked @ Sep 10 2013 06:10pm)
I am reading that, I have my code all done to run in a command prompt, im just having trouble understanding how to convertthat into a GUI panel from the code that I have.  I am new to programming in general and java is the only language that I am attempting to learn at this moment.


forget about your code for a few hours. go through the standard java swing tutorials until you understand how to use it. play around with them. make changes and run them again to see what changed. once you understand it, then go back and rewrite your program.

even experienced developers have to go through tutorials first. that's why they are there. you think i converted my java app to an android app immediately when i was learning android? i went through a few android tutorials first, then i slowly rewrote my app.

This post was edited by carteblanche on Sep 10 2013 04:20pm
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Sep 10 2013 04:25pm
Quote (carteblanche @ Sep 10 2013 05:15pm)
forget about your code for a few hours. go through the standard java swing tutorials until you understand how to use it. play around with them. make changes and run them again to see what changed. once you understand it, then go back and rewrite your program.

even experienced developers have to go through tutorials first. that's why they are there. you think i converted my java app to an android app immediately when i was learning android? i went through a few android tutorials first, then i slowly rewrote my app.


okay thanks
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 11 2013 07:23am
here's how to do it using a GUI

Code
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
// You must create a JFrame class that extends JFrame
public class Main extends JFrame {

 final static double PARKING_FEE = 20;

 // This is the constructor for your JFrame class
 Main( ) {

   // Calling "super" calls the constructor on JFrame and takes a string for
   // the title as it's argument

   super( "Tickets" );
   setBounds( 100, 100, 300, 100 );
   setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   Container con = this.getContentPane( );
   JPanel jPanel = new JPanel( );
   con.add( jPanel );

   // This is a combo box that holds all the categories
   final JComboBox jComboBoxCategory = new JComboBox( );
   jComboBoxCategory.setToolTipText( "Category" );
   jComboBoxCategory.addItem( "Student" );
   jComboBoxCategory.addItem( "Alumni" );
   jComboBoxCategory.addItem( "Faculty & Staff" );
   jComboBoxCategory.addItem( "Military" );
   jComboBoxCategory.addItem( "General Public" );
   jPanel.add( jComboBoxCategory );

   // This is a checkbox
   final JCheckBox jCheckBoxParking = new JCheckBox( "Parking" );
   jPanel.add( jCheckBoxParking );

   // This is a label that is used to display the price
   final JLabel jLablePrice = new JLabel( "Total: $0.00 " );
   jPanel.add( jLablePrice );

   // This adds a listener to jComboBoxCategory. The function,
   // itemStateChanged() is triggered if the user changes the selection in the
   // combobox. After that we call the recalculate function and set the text in
   // the label to the return value

   jComboBoxCategory.addItemListener( new ItemListener( ) {
     @Override
     public void itemStateChanged( ItemEvent itemEvent ) {
       jLablePrice.setText( "Total: "
           + NumberFormat.getCurrencyInstance( ).format(
               recalculate( jComboBoxCategory.getSelectedIndex( ),
                   jCheckBoxParking.isSelected( ) ) ) );
     }
   } );

   // This is the same as the jComboBoxCategory listener
   jCheckBoxParking.addItemListener( new ItemListener( ) {
     @Override
     public void itemStateChanged( ItemEvent itemEvent ) {
       jLablePrice.setText( "Total: "
           + NumberFormat.getCurrencyInstance( ).format(
               recalculate( jComboBoxCategory.getSelectedIndex( ),
                   jCheckBoxParking.isSelected( ) ) ) );
     }
   } );

   setVisible( true );
 }

 // This function recalculates the price. We pass it the itemIndex from
 // jComboBoxCategory and isSelected from jCheckBoxParking. The index starts at
 // 0, so 0 = "Student" and 4 = "General Public"

 public static double recalculate( int category, boolean parking ) {

   int result = 60;

   switch ( category ) {
     case 0 :
       result *= 0.5;
       break;
     case 1 :
       result *= 0.9;
       break;
     case 2 :
       result *= 0.85;
       break;
     case 3 :
       result *= 0.8;
       break;
     default :
       break;
   }

   if ( parking ) {
     result += PARKING_FEE;
   }

   return result;

 }

 public static void main( String args[] ) {
   new Main( );
 }
}


This post was edited by labatymo on Sep 11 2013 07:34am
Member
Posts: 1
Joined: Sep 10 2013
Gold: 0.00
Sep 11 2013 10:48am
what all do you have so far?
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Sep 11 2013 12:15pm
Quote (ejk929 @ Sep 11 2013 11:48am)
what all do you have so far?


im finished
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll