| | |
quick question w\ a program
![]() |
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
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 "
"have the Frame display in the center of the monitor "
java Syntax (Toggle Plain Text)
//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; private Button exitButton; private TextField lengthField = new TextField(10); private TextField widthField = new TextField(10); private TextField areaField = new TextField(10); private TextField perimeterField = new TextField(10); //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"); //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(5, 2, 5, 5)); //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) { } //and action performed } //end class
this, which you already have,
should already take care of that.
Have you tried it?
Java Syntax (Toggle Plain Text)
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
----------------------------------------------
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
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!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
If you do need the frame "always on top" you would use this:
Hope that helps
Java Syntax (Toggle Plain Text)
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!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
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:
by the way I know I have not done error checking just wanna make sure everything is right first before I check for errors
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:
java Syntax (Toggle Plain Text)
//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
![]() |
Similar Threads
- nevermind: ignore arrays problem - quick question (C++)
- quick question w\ a program (Java)
- quick question w\ a program (C++)
- Quick Question: Is J# the same thing as Java? (Java)
- just a quick question (C++)
- Simple Time/Date Program (C)
- reading input ... quick C++ question ... (C++)
Other Threads in the Java Forum
- Previous Thread: i need 6th version java tutorial
- Next Thread: Urgent ... LRSTRUCT and BRSTruct ????
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






