Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    List Box interface, gotta be something simple

    This code is almost done but I am messing up the interface with the listbox portion, can someone review and point me in the right direction?

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;

    /********************************************/
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    import javax.swing.Jlist;

    /****************************************/

    public class HoursCalc extends JApplet implements ActionListener {


    /*****************************************/
    {
    // Instance attributes used in this example
    private JPanel topPanel = new;
    private JList listbox = new;

    // Constructor of main frame
    public ListExample()
    {
    // Set the frame characteristics
    setTitle( "Simple ListBox Application" );
    setSize( 300, 100 );
    setBackground( Color.gray );

    // Create a panel to hold all other components
    topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );

    // Create some items to add to the list
    String listData[] =
    {
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4"
    };

    // Create a new listbox control
    listbox = new JList( listData );
    topPanel.add( listbox, BorderLayout.CENTER );
    }

    // Main entry point for this example
    public static void main( String args[] )
    {
    // Create an instance of the test application
    ListExample mainFrame = new ListExample();
    mainFrame.setVisible( true );
    }
    }
    /*******************************************/


    /**
    * Initialization method that will be called after the applet is loaded
    * into the browser.
    */
    public void init() {
    //Execute a job on the event-dispatching thread:
    //creating this applet's GUI.
    try {
    javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
    public void run() {
    createGUI();
    }
    });
    } catch (Exception e) {
    System.err.println("createGUI didn't successfully complete");
    }
    }

    private JLabel labelName = null;
    private JTextField fldName = null;
    private JLabel labelHours = null;
    private JTextField fldHours = null;
    private JLabel labelRate = null;
    private JTextField fldRate = null;
    private JTextField fldPay = null;
    private JTextField fldMessage = null;

    private void createGUI() {
    this.setLayout(new GridLayout(0,2));
    labelName = new JLabel("Name");
    getContentPane().add(labelName);

    fldName = new JTextField();
    getContentPane().add(fldName);

    labelRate = new JLabel("Rate");
    getContentPane().add(labelRate);

    fldRate = new JTextField("0.00");
    getContentPane().add(fldRate);

    labelHours = new JLabel("Hours");
    getContentPane().add(labelHours);

    fldHours = new JTextField("0.0");
    getContentPane().add(fldHours);


    // make the button
    //
    JButton butCalc = new JButton("Calc");
    butCalc.setVisible(true);
    getContentPane().add(butCalc);

    fldPay = new JTextField();
    getContentPane().add(fldPay);
    fldPay.setEditable(false);

    fldMessage = new JTextField();
    getContentPane().add(fldMessage);
    fldMessage.setEditable(false);

    // action listener
    //
    butCalc.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
    double rate = 0.0;
    double hours = 0.0;
    try {
    rate = Double.parseDouble(fldRate.getText());
    hours = Double.parseDouble(fldHours.getText());

    if (rate < 0.0 || hours < 0.0)
    throw new NumberFormatException ();
    if (rate > 150 | rate < 6.0 || hours > 60)
    throw new NumberFormatException ();


    double pay = rate * hours;
    if (hours > 40.0)
    pay += rate / 2.0 * (hours - 40.0);

    fldMessage.setText("");
    fldPay.setText(String.valueOf(pay));
    }
    catch (NumberFormatException exc)
    {
    fldMessage.setText("Please check numbers");
    }
    }
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    1) This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

    2) See:- *** WARNING: Questions posted here will be deleted! ***.
    You will not get any reply in this forum which is intended to be used only to post a completed (working) script for showcasing/benefit of others.

    3) When posting in this forum please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar which will insert the tags.

    Attend to (3) and then ask a mod to move this thread to the correct form.
    Last edited by Philip M; 10-05-2010 at 09:41 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •