Can someone give me a visual, I learn more visually.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class Ch07Ex03 extends JFrame
{
DecimalFormat currency = new DecimalFormat("##0.00");
private JPanel panel;
private JLabel messageLabel;
private JLabel results;
private JLabel totalCostLabel;
private JTextField textField;
private JButton submitButton;
private JButton resetButton;
private final int WINDOW_WIDTH = 600;
private final int WINDOW_HEIGHT = 500;
/**
Constructor
*/
public Ch07Ex03()
{
//Set the window title.
setTitle("Order");
//Set the size of the window.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
//Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add a GridLayout manager to the content pane.
setLayout(new GridLayout(3, 2));
//Build the panel and add it to the frame.
buildPanel();
panel.setBorder(BorderFactory.createTitledBorder("Customer Order"));
//set the panel background to light gray.
getContentPane().setBackground(Color.lightGray);
//set the messageLabel and textField to Blue.
messageLabel.setForeground(Color.BLUE);
textField.setBackground(Color.BLUE);
//set the totalCost label and results label to red.
totalCostLabel.setForeground(Color.RED);
results.setForeground(Color.RED);
//Create 6 panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
//Add the components to the panels.
panel1.add(messageLabel);
panel2.add(textField);
panel3.add(submitButton);
panel4.add(resetButton);
panel5.add(totalCostLabel);
panel6.add(results);
//Add the panels to the content pane.
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
//Display the window.
setVisible(true);
}
/**
The buildPanel method adds a label, text field, and a button to the panel.
*/
private void buildPanel()
{
//Create a label to display instructions.
messageLabel = new JLabel("Enter the number of items");
//Creates a text field 10 characters wide.
textField = new JTextField(10);
//Create a button with the caption "Submit".
submitButton = new JButton("Submit");
//Create a button with the caption "Reset."
resetButton = new JButton("Clear");
//Creates a label to display the results.
results = new JLabel("Result");
//Creates a label for Total Cost.
totalCostLabel = new JLabel("Total Cost");
//Add an action listener to the button.
submitButton.addActionListener(new ButtonListener());
//Add an action listener to the button.
resetButton.addActionListener(new ButtonListener());
//Create a JPanel object and let the panel field reference it.
panel = new JPanel();
//Add the label, text field, and button components to the panel.
panel.add(messageLabel);
panel.add(textField);
panel.add(results);
panel.add(submitButton);
panel.add(resetButton);
}
/**
SubmitButtonListener is an action listener class for the submit button
*/
private class ButtonListener implements ActionListener
{
/**
the actionPerformed method executes when the user clicks on the submit button.
@param e the event object.
*/
public void actionPerformed(ActionEvent e)
{
String input; //To hold the users input
int numOrdered; //The number items ordered
//Get the text entered by the user into the text field.
input = textField.getText();
//Convert the input to items.
numOrdered = Integer.parseInt(input);
double total = numOrdered * 50.00;
if(e.getSource() == submitButton)
{
//Display the result.
results.setText(currency.format(total));
}
else if(e.getSource() == resetButton)
{
textField.setText("");
}
}
}
public static void main(String[] args)
{
new Ch07Ex03();
}
}
This post has been edited by tricket_7: 22 October 2009 - 11:47 AM

New Topic/Question
Reply




MultiQuote




|