-
small issues with a program
I got everything done on my program with a couple minor issues...first my clear button in the menu exits my program and my exit the program button does not do anything and everything looks right on my end...the other issue is I am getting two errors in the parsing and it looks right to me but I guess I am doing something wrong:
instructions:
1. 4 Labels (align them to the right by using the parameter Label.RIGHT when you construct your label… Label lengthLabel = new Label(“Enter the length: “, Label.RIGHT)
2. 4 TextFields (2 for input and 2 for output… your output fields should be read-only). All of your TextFields can be length 10.
3. 2 Buttons… one to calculate the area and perimeter and one to exit… use the labels for the buttons shown below
area = length * width
perimeter = 2 * (length + width)
4. create a menu for File -> Exit (to terminate the application)
5. create a menu for Edit -> Clear (to clear all TextFields)
6. only calculate/display the area and perimeter if length and width are valid and greater than zero… if not, display an error message in a dialog box, clear out all TextFields, and put the cursor in the length field.
7. when you click the Calculate button, be sure all 4 values in the TextFields are formatted to two decimal places with a comma in the thousands place (see below)
8. be sure to include code to terminate the application when the X is clicked
9. use a GridLayout for the frame with 5 rows, 2 columns, and 5 pixels for spacing horizontally and vertically
10. utilize the setActionCommand() method to allow for only one “exit” section of code in your actionPerformed() method
11. have the Frame display in the center of the monitor with a width of 350 and height of 200
12. have the Frame title of “Area and Perimeter of a Rectangle”
Code:
//import packages
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
//create a subclass at the fram class
public class RectangleApp extends Frame implements ActionListener
{
//construct variables
private Label lengthLabel = new Label("Enter the length: ", Label.RIGHT);
private Label widthLabel = new Label("Enter the width: ", Label.RIGHT);
private Label areaLabel = new Label("Area: ", Label.RIGHT);
private Label perimeterLabel = new Label("Perimeter: ", Label.RIGHT);
private Panel topPanel;
private Button calculateButton = new Button("Calculate");
private Button exitButton = new Button("Exit the Program");
private TextField lengthField = new TextField(10);
private TextField widthField = new TextField(10);
private TextField areaField = new TextField(10);
private TextField perimeterField = new TextField(10);
private boolean first;
private boolean clearText;
private DecimalFormat calcPattern;
private double length;
private double width;
private double area;
private double perimeter;
//constructor method
public RectangleApp()
{
//create an instance of the menu
MenuBar mnuBar = new MenuBar();
setMenuBar(mnuBar); //display the previously constructed MenuBar
//construct and populate File Menu
Menu mnuFile = new Menu("File"); //create a command on the MenuBar
mnuBar.add(mnuFile);
MenuItem mnuFileExit = new MenuItem("Exit");
//construct a command to go under a menu
mnuFile.add(mnuFileExit);
//construct and populate the edit menu
Menu mnuEdit = new Menu("Edit");
mnuBar.add(mnuEdit);
MenuItem mnuEditClear = new MenuItem("Clear");
mnuEdit.add(mnuEditClear);
//register the action listener with each of the menuitems
mnuFileExit.addActionListener(this);
mnuEditClear.addActionListener(this);
//assign an ActionCommand to each of the MenuItems
mnuFileExit.setActionCommand("Exit");
mnuEditClear.setActionCommand("Clear");
//construct components and initialize beginning values
topPanel = new Panel();
calcPattern = new DecimalFormat("###,###.##");
topPanel.add(lengthLabel);
topPanel.add(lengthField);
length = 0.0;
topPanel.add(widthLabel);
topPanel.add(widthField);
width = 0.0;
topPanel.add(areaLabel);
topPanel.add(areaField);
area = 0.0;
areaField.setEditable(false);
topPanel.add(perimeterLabel);
topPanel.add(perimeterField);
perimeter = 0.0;
perimeterField.setEditable(false);
topPanel.add(calculateButton);
topPanel.add(exitButton);
//set layouts for the Frame and Panels
setLayout(new BorderLayout());
topPanel.setLayout(new GridLayout(5, 2, 5, 5));
//add components to frame
add(topPanel, BorderLayout.NORTH);
//allow the x to close the application
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} //end window adapter
);
} //end calculator[] constructor method
public static void main(String args [])
{
//construct an instance of the Frame (Calculator)
RectangleApp f = new RectangleApp();
f.setTitle("Area and Perimeter of a Rectangle");
f.setSize(350, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
} //end main
public void actionPerformed(ActionEvent e)
{
//test for menu item clicks
String arg = e.getActionCommand();
//exit was clicked
if(arg == "Exit");
System.exit(0);
//clear was clikced
if(arg.equals("Clear"))
{
clearText = true;
first = true;
} //end if about
//Calculate button was clicked
{
if(arg.equals("Calculate"))
convert data in TextField to int
double length = Double.parseDouble(lengthField.getText());
double width = Double.parseDouble(widthField.getText());
double area = Double.parseDouble(areaField.getText());
double perimeter = Double.parseDouble(perimeterField.getText());
area = length * width;
perimeter = length + width;
} //end the if go
//exitbutton was clicked
if(arg.equals("Exit the program"))
{
System.exit(0);
} //end the if exit
} //end action performed
} //end class
by the way I know I have not done error checking just wanna make sure everything is right first before I check for errors
-
You've got a lot to learn. So do I. I hope that points you in the right direction. Don't forget to validate the data.
Code:
//import packages
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
//create a subclass at the fram class
public class RectangleApp extends Frame implements ActionListener
{
//construct variables
private Label lengthLabel = new Label("Enter the length: ", Label.RIGHT);
private Label widthLabel = new Label("Enter the width: ", Label.RIGHT);
private Label areaLabel = new Label("Area: ", Label.RIGHT);
private Label perimeterLabel = new Label("Perimeter: ", Label.RIGHT);
private Panel topPanel;
private Button calculateButton = new Button("Calculate");
private Button exitButton = new Button("Exit the Program");
private TextField lengthField = new TextField(10);
private TextField widthField = new TextField(10);
private TextField areaField = new TextField(10);
private TextField perimeterField = new TextField(10);
private boolean first;
private boolean clearText;
private DecimalFormat calcPattern;
private double length;
private double width;
private double area;
private double perimeter;
//constructor method
public RectangleApp()
{
//create an instance of the menu
MenuBar mnuBar = new MenuBar();
setMenuBar(mnuBar); //display the previously constructed MenuBar
//construct and populate File Menu
Menu mnuFile = new Menu("File"); //create a command on the MenuBar
mnuBar.add(mnuFile);
MenuItem mnuFileExit = new MenuItem("Exit");
//construct a command to go under a menu
mnuFile.add(mnuFileExit);
//construct and populate the edit menu
Menu mnuEdit = new Menu("Edit");
mnuBar.add(mnuEdit);
MenuItem mnuEditClear = new MenuItem("Clear");
mnuEdit.add(mnuEditClear);
//register the action listener with each of the menuitems
mnuFileExit.addActionListener(this);
mnuEditClear.addActionListener(this);
//assign an ActionCommand to each of the MenuItems
mnuFileExit.setActionCommand("Exit");
mnuEditClear.setActionCommand("Clear");
//construct components and initialize beginning values
topPanel = new Panel();
calcPattern = new DecimalFormat("###,###.##");
topPanel.add(lengthLabel);
topPanel.add(lengthField);
length = 0.0;
topPanel.add(widthLabel);
topPanel.add(widthField);
width = 0.0;
topPanel.add(areaLabel);
topPanel.add(areaField);
area = 0.0;
areaField.setEditable(false);
topPanel.add(perimeterLabel);
topPanel.add(perimeterField);
perimeter = 0.0;
perimeterField.setEditable(false);
calculateButton.addActionListener(this);
topPanel.add(calculateButton);
exitButton.addActionListener(this);
topPanel.add(exitButton);
//set layouts for the Frame and Panels
setLayout(new BorderLayout());
topPanel.setLayout(new GridLayout(5, 2, 5, 5));
//add components to frame
add(topPanel, BorderLayout.NORTH);
//allow the x to close the application
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} //end window adapter
);
} //end calculator[] constructor method
public static void main(String args [])
{
//construct an instance of the Frame (Calculator)
RectangleApp f = new RectangleApp();
f.setTitle("Area and Perimeter of a Rectangle");
f.setSize(350, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
} //end main
public void actionPerformed(ActionEvent e)
{
//test for menu item clicks
String arg = e.getActionCommand();
Object source = e.getSource();
//exit was clicked
if(arg.equals ("Exit"))
System.exit(0);
//clear was clikced
if(arg.equals("Clear"))
{
lengthField.setText("");
widthField.setText("");
areaField.setText("");
perimeterField.setText("");
clearText = true;
first = true;
} //end if about
//Calculate button was clicked
if (source == calculateButton)
{
//convert data in TextField to int
length = Double.parseDouble(lengthField.getText());
width = Double.parseDouble(widthField.getText());
area = length * width;
perimeter = length + width;
String areastring = Double.toString(area);
String perimeterstring = Double.toString(perimeter);
areaField.setText(areastring);
perimeterField.setText(perimeterstring);
} //end the if go
//exitbutton was clicked
else if(source == exitButton)
{
System.exit(0);
} //end the if exit
} //end action performed
} //end class
Similar Threads
-
By zobi316 in forum VB Classic
Replies: 3
Last Post: 03-10-2008, 07:05 AM
-
Replies: 1
Last Post: 08-12-2007, 09:38 PM
-
By stormswimmer in forum Java
Replies: 2
Last Post: 01-02-2006, 03:17 PM
-
By Preet Kanwaljit Singh in forum Careers
Replies: 0
Last Post: 07-14-2003, 02:08 PM
-
By W.Pierce in forum VB Classic
Replies: 1
Last Post: 12-11-2001, 08:28 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks