PDA

View Full Version : List Box interface, gotta be something simple


tools
10-05-2010, 11:38 AM
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");
}
}
}

Philip M
10-05-2010, 09:37 PM
1) This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

2) See:- http://www.codingforums.com/showthread.php?t=17515.
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.