quick question w\ a program

Reply

Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

quick question w\ a program

 
0
  #1
Apr 24th, 2008
I am in the middle of a program and have a quick question and not sure how i do this...

"have the Frame display in the center of the monitor "

  1.  
  2. //import packages
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.datatransfer.*;
  6. import java.text.DecimalFormat;
  7. import javax.swing.JOptionPane;
  8.  
  9. //create a subclass at the fram class
  10. public class RectangleApp extends Frame implements ActionListener
  11. {
  12. //construct variables
  13. private Label lengthLabel = new Label("Enter the length: ", Label.RIGHT);
  14. private Label widthLabel = new Label("Enter the width: ", Label.RIGHT);
  15. private Label areaLabel = new Label("Area: ", Label.RIGHT);
  16. private Label perimeterLabel = new Label("Perimeter: ", Label.RIGHT);
  17. private Panel topPanel;
  18. private Button calculateButton;
  19. private Button exitButton;
  20. private TextField lengthField = new TextField(10);
  21. private TextField widthField = new TextField(10);
  22. private TextField areaField = new TextField(10);
  23. private TextField perimeterField = new TextField(10);
  24.  
  25. //constructor method
  26. public RectangleApp()
  27. {
  28. //create an instance of the menu
  29. MenuBar mnuBar = new MenuBar();
  30. setMenuBar(mnuBar); //display the previously constructed MenuBar
  31.  
  32. //construct and populate File Menu
  33. Menu mnuFile = new Menu("File"); //create a command on the MenuBar
  34. mnuBar.add(mnuFile);
  35. MenuItem mnuFileExit = new MenuItem("Exit");
  36.  
  37. //construct a command to go under a menu
  38. mnuFile.add(mnuFileExit);
  39.  
  40. //construct and populate the edit menu
  41. Menu mnuEdit = new Menu("Edit");
  42. mnuBar.add(mnuEdit);
  43. MenuItem mnuEditClear = new MenuItem("Clear");
  44. mnuEdit.add(mnuEditClear);
  45.  
  46. //register the action listener with each of the menuitems
  47. mnuFileExit.addActionListener(this);
  48. mnuEditClear.addActionListener(this);
  49.  
  50. //assign an ActionCommand to each of the MenuItems
  51. mnuFileExit.setActionCommand("Exit");
  52. mnuEditClear.setActionCommand("Clear");
  53.  
  54. //set layouts for the Frame and Panels
  55. setLayout(new BorderLayout());
  56. topPanel.setLayout(new GridLayout(5, 2, 5, 5));
  57.  
  58.  
  59. //allow the x to close the application
  60. addWindowListener(new WindowAdapter()
  61. {
  62. public void windowClosing(WindowEvent e)
  63. {
  64. System.exit(0);
  65. }
  66. } //end window adapter
  67. );
  68. } //end calculator[] constructor method
  69. public static void main(String args [])
  70. {
  71. //construct an instance of the Frame (Calculator)
  72. RectangleApp f = new RectangleApp();
  73. f.setTitle("Area and Perimeter of a Rectangle");
  74. f.setSize(350, 200);
  75. f.setLocationRelativeTo(null);
  76. f.setVisible(true);
  77. } //end main
  78. public void actionPerformed(ActionEvent e)
  79. {
  80.  
  81.  
  82.  
  83. } //and action performed
  84. } //end class
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,355
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: quick question w\ a program

 
0
  #2
Apr 24th, 2008
this, which you already have,

  1. f.setLocationRelativeTo(null);

should already take care of that.
Have you tried it?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: quick question w\ a program

 
0
  #3
Apr 24th, 2008
As Masijade said what you have there should work fine, is it that you need the frame to be on top of other windows as well as centered?
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: quick question w\ a program

 
0
  #4
Apr 24th, 2008
If you do need the frame "always on top" you would use this:

  1. f.setAlwaysOnTop(true)

Hope that helps
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: quick question w\ a program

 
0
  #5
Apr 24th, 2008
ahhhhh advice...never work on a program when you are tired 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:

  1. //import packages
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.datatransfer.*;
  5. import java.text.DecimalFormat;
  6. import javax.swing.JOptionPane;
  7.  
  8. //create a subclass at the fram class
  9. public class RectangleApp extends Frame implements ActionListener
  10. {
  11. //construct variables
  12. private Label lengthLabel = new Label("Enter the length: ", Label.RIGHT);
  13. private Label widthLabel = new Label("Enter the width: ", Label.RIGHT);
  14. private Label areaLabel = new Label("Area: ", Label.RIGHT);
  15. private Label perimeterLabel = new Label("Perimeter: ", Label.RIGHT);
  16. private Panel topPanel;
  17. private Button calculateButton = new Button("Calculate");
  18. private Button exitButton = new Button("Exit the Program");
  19. private TextField lengthField = new TextField(10);
  20. private TextField widthField = new TextField(10);
  21. private TextField areaField = new TextField(10);
  22. private TextField perimeterField = new TextField(10);
  23. private boolean first;
  24. private boolean clearText;
  25. private DecimalFormat calcPattern;
  26. private double length;
  27. private double width;
  28. private double area;
  29. private double perimeter;
  30.  
  31. //constructor method
  32. public RectangleApp()
  33. {
  34. //create an instance of the menu
  35. MenuBar mnuBar = new MenuBar();
  36. setMenuBar(mnuBar); //display the previously constructed MenuBar
  37.  
  38. //construct and populate File Menu
  39. Menu mnuFile = new Menu("File"); //create a command on the MenuBar
  40. mnuBar.add(mnuFile);
  41. MenuItem mnuFileExit = new MenuItem("Exit");
  42.  
  43. //construct a command to go under a menu
  44. mnuFile.add(mnuFileExit);
  45.  
  46. //construct and populate the edit menu
  47. Menu mnuEdit = new Menu("Edit");
  48. mnuBar.add(mnuEdit);
  49. MenuItem mnuEditClear = new MenuItem("Clear");
  50. mnuEdit.add(mnuEditClear);
  51.  
  52. //register the action listener with each of the menuitems
  53. mnuFileExit.addActionListener(this);
  54. mnuEditClear.addActionListener(this);
  55.  
  56. //assign an ActionCommand to each of the MenuItems
  57. mnuFileExit.setActionCommand("Exit");
  58. mnuEditClear.setActionCommand("Clear");
  59.  
  60. //construct components and initialize beginning values
  61. topPanel = new Panel();
  62. calcPattern = new DecimalFormat("###,###.##");
  63. topPanel.add(lengthLabel);
  64. topPanel.add(lengthField);
  65. length = 0.0;
  66. topPanel.add(widthLabel);
  67. topPanel.add(widthField);
  68. width = 0.0;
  69. topPanel.add(areaLabel);
  70. topPanel.add(areaField);
  71. area = 0.0;
  72. areaField.setEditable(false);
  73. topPanel.add(perimeterLabel);
  74. topPanel.add(perimeterField);
  75. perimeter = 0.0;
  76. perimeterField.setEditable(false);
  77. topPanel.add(calculateButton);
  78. topPanel.add(exitButton);
  79.  
  80. //set layouts for the Frame and Panels
  81. setLayout(new BorderLayout());
  82. topPanel.setLayout(new GridLayout(5, 2, 5, 5));
  83.  
  84. //add components to frame
  85. add(topPanel, BorderLayout.NORTH);
  86.  
  87.  
  88. //allow the x to close the application
  89. addWindowListener(new WindowAdapter()
  90. {
  91. public void windowClosing(WindowEvent e)
  92. {
  93. System.exit(0);
  94. }
  95. } //end window adapter
  96. );
  97. } //end calculator[] constructor method
  98. public static void main(String args [])
  99. {
  100. //construct an instance of the Frame (Calculator)
  101. RectangleApp f = new RectangleApp();
  102. f.setTitle("Area and Perimeter of a Rectangle");
  103. f.setSize(350, 200);
  104. f.setLocationRelativeTo(null);
  105. f.setVisible(true);
  106. } //end main
  107. public void actionPerformed(ActionEvent e)
  108. {
  109. //test for menu item clicks
  110. String arg = e.getActionCommand();
  111.  
  112. //exit was clicked
  113. if(arg == "Exit");
  114. System.exit(0);
  115.  
  116. //clear was clikced
  117. if(arg.equals("Clear"))
  118. {
  119. clearText = true;
  120. first = true;
  121.  
  122. } //end if about
  123.  
  124. //Calculate button was clicked
  125. {
  126. if(arg.equals("Calculate"))
  127.  
  128. convert data in TextField to int
  129. double length = Double.parseDouble(lengthField.getText());
  130. double width = Double.parseDouble(widthField.getText());
  131. double area = Double.parseDouble(areaField.getText());
  132. double perimeter = Double.parseDouble(perimeterField.getText());
  133. area = length * width;
  134. perimeter = length + width;
  135. } //end the if go
  136.  
  137. //exitbutton was clicked
  138. if(arg.equals("Exit the program"))
  139. {
  140. System.exit(0);
  141. } //end the if exit
  142.  
  143. } //end action performed
  144. } //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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC