Results 1 to 7 of 7
Thread: small issues with a program
- 04-25-2008, 02:10 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 22
- Rep Power
- 0
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”
by the way I know I have not done error checking just wanna make sure everything is right first before I check for errorsJava 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
- 04-25-2008, 03:45 AM #2
Ok, what is your question?
freedom exists in the world of ideas
- 04-25-2008, 04:54 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, first explain your question properly. And post your code where you stuck with, and also it is better if others can test your code easily.
- 04-25-2008, 06:57 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 22
- Rep Power
- 0
my issue is that the exit the application will not exit...it does not do anything
Also the errors I am getting are:
C:\Documents and Settings\Don & Diane Kruep\My Documents\RectangleApp.java:131: ';' expected
Calculate button was clicked
^
C:\Documents and Settings\Don & Diane Kruep\My Documents\RectangleApp.java:131: cannot resolve symbol
symbol : class Calculate
location: class RectangleApp
Calculate button was clicked
^
2 errors
Tool completed with exit code 1
- 04-25-2008, 07:16 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Seems to you don't have an action listener to the button, did you?
- 04-25-2008, 07:26 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Try this jim, you have add action listener in wrong way.
Java Code:// exit button exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } });
- 04-25-2008, 08:28 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I do this just for fun ;)
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import javax.swing.JOptionPane; //create a subclass at the fram class public class RectangleApp extends Frame implements ActionListener { //initialize 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 double length; private double width; private double area; private double perimeter; public RectangleApp() // Default constructor { //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(); 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); // exit button exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // calculate button calculateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calculateResults(); } }); } //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.setResizable(false); f.setVisible(true); } //end main private void clearText() { lengthField.setText(null); widthField.setText(null); areaField.setText(null); perimeterField.setText(null); } private void calculateResults() { try{ length = Double.parseDouble(lengthField.getText()); width = Double.parseDouble(widthField.getText()); area = length * width; perimeter = 2 * (length + width); areaField.setText(String.valueOf(area)); perimeterField.setText(String.valueOf(perimeter)); } catch(NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "Invalid inputs are found!", "Error!", 0); clearText(); } } public void actionPerformed(ActionEvent e) { //test for menu item clicks String arg = e.getActionCommand(); //exit was clicked if(arg.equals("Exit")) { System.exit(0); } //clear was clikced if(arg.equals("Clear")) { clearText(); } //end if about } //end action performed } //end class
Similar Threads
-
Building small web application in java for practice.
By Saurabh321 in forum New To JavaReplies: 1Last Post: 02-01-2008, 03:38 PM -
Small scale Java Editor
By Greenfrog99 in forum AWT / SwingReplies: 0Last Post: 01-27-2008, 08:46 PM -
Execute small java programs through Internet.........is thst possible??
By atanu.dey in forum New To JavaReplies: 1Last Post: 01-05-2008, 07:48 PM -
Small tennis simulation in Java
By diego in forum New To JavaReplies: 1Last Post: 12-02-2007, 01:32 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks