new to javawhats the best way to go about this?
21 Replies - 1790 Views - Last Post: 14 December 2009 - 04:47 PM
#1
new to java
Posted 09 December 2009 - 10:59 AM
Now I have a project where I need to make a swimming pool calculator. All I really need to do is enter in length, width, average depth and create a button to do the calculation. I guess im confused on what the best thing to use for this. Do I use a frame? or panel? I guess it could be an applet too. Should I just use awt package or should I use swing? I guess it's all prefrence maybe?
this really shouldn't be hard to do but i'm having a hard time getting started. I'm not asking for code here but advise. I can create a basic frame but I get confused on adding in my components. I know I'll need lables, textfields,and a button. It will have to use actionListener for the event of clicking the button.
It's when I sit down to write the code is when I get all messed up so to speak. Do all three things (applet, frame, and panel) need to use containers? Again I'm sorry because I kinda of understand how to do this but just lack the ability to putt it together. Again I'm not asking for the code I really do want to write this myself and make it work. But I have been searching and reading and am just having a hard time finding anything that will show me just how to putt the whole thing together.
Thanks in advance for any guidance you can give me!
Replies To: new to java
#2
Re: new to java
Posted 09 December 2009 - 11:46 AM
2) If you make an applet, you will have to embed it in an HTML and run it in a browser. I recommend just using Swing to make a desktop application.
3) As a basic outline of the classes you will need, it will be as follows:
-Your main class (SomethingWindow) should extend JFrame
-You should have your GUI components in JPanels.
-You can set the Layouts of JPanels
-You can add to Layouts or JPanels
-You will use JLabel, JButton, JTextField
-You will need to make a private inner class to listen for the button press that implements ActionListener
I recommend looking up a tutorial or getting a book. Getting into GUI programming can be a tough concept to grasp (layouts and Panels will drive you mad, trust me). Maybe once I'm done this semester and I have some more time to breathe I'll write a nice Intro to GUI tutorial. Hope that helps though
#3
Re: new to java
Posted 09 December 2009 - 01:23 PM
TriggaMike, on 9 Dec, 2009 - 10:46 AM, said:
2) If you make an applet, you will have to embed it in an HTML and run it in a browser. I recommend just using Swing to make a desktop application.
3) As a basic outline of the classes you will need, it will be as follows:
-Your main class (SomethingWindow) should extend JFrame
-You should have your GUI components in JPanels.
-You can set the Layouts of JPanels
-You can add to Layouts or JPanels
-You will use JLabel, JButton, JTextField
-You will need to make a private inner class to listen for the button press that implements ActionListener
I recommend looking up a tutorial or getting a book. Getting into GUI programming can be a tough concept to grasp (layouts and Panels will drive you mad, trust me). Maybe once I'm done this semester and I have some more time to breathe I'll write a nice Intro to GUI tutorial. Hope that helps though
thanks for the reply, yea I'm sure I'll get the hang of it but it just takes time like anything. I feel a bit like a drowning fish at the moment. this isn't much but here is the basic code I have so far. First I'm just trying to get the frame created and the lables in there. I have the lables in there "sort of" I'll worry about figureing out how to do the orrientation and all that once I get the comonents added. Then I'll get the functionality of the button and so forth done as well but for now I'm just trying to get the frame and components added and looking correct for now.
import javax.swing.*;
public class SwimmingPoolCalc {
public static void main(String[] args)
{
JFrame frame = new JFrame("Swimming Pool Calculator");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("Enter Legth of Pool");
JLabel label2 = new JLabel("Enter width of Pool");
JLabel label3 = new JLabel("Enter average volume of Pool");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label1);
panel.add(label2);
panel.add(label3);
//public JTextField()
}
#4
Re: new to java
Posted 09 December 2009 - 01:44 PM
Also, does it need to be like a punch-in-the-numbers-in-the-application calculator like MS's built in one or do you just want a dialog to pop up and prompt you for input and do the calculations internally? For something that small you could probably just do the second option, as you don't really need all that UI for just a simple thing like that. In the case of the second option, just do JOptionPane.
This post has been edited by NeoTifa: 09 December 2009 - 01:44 PM
#5
Re: new to java
Posted 09 December 2009 - 01:50 PM
NeoTifa, on 9 Dec, 2009 - 12:44 PM, said:
Also, does it need to be like a punch-in-the-numbers-in-the-application calculator like MS's built in one or do you just want a dialog to pop up and prompt you for input and do the calculations internally? For something that small you could probably just do the second option, as you don't really need all that UI for just a simple thing like that. In the case of the second option, just do JOptionPane.
No I would like it to be an app that comes up and has 3 buttons 1, enter legth of pool. 2 enter width, 3 enter volume.
Then have a button that says' calculate volume and a text field to display the volume. I don't really want to use JOptionPane.
Fairly simple it seems. Just trying to get the layout done and running into walls. Seems like everytime I find another place to show you how to do it it's diffent then the last.
and yea I will need to import and take from the awt package I'm sure. But I'd still like to use swing for the main stuff so I"m looking to use JFrame,JTextField, JButton ect.
Thanks.
This post has been edited by gec5741: 09 December 2009 - 01:53 PM
#6
Re: new to java
Posted 09 December 2009 - 01:53 PM
So, you want a text field at the top and a total of 4 buttons?
#7
Re: new to java
Posted 09 December 2009 - 02:34 PM
NeoTifa, on 9 Dec, 2009 - 12:53 PM, said:
So, you want a text field at the top and a total of 4 buttons?
Im sorry I totally miss typed!!! I meant 3 lables with 3 textfields and 1 button that says "calculate volume" on it. And one last text field to display the output. Hope that made more sense.
#8
Re: new to java
Posted 09 December 2009 - 04:44 PM
#9
Re: new to java
Posted 10 December 2009 - 07:51 AM
sam.adams61, on 9 Dec, 2009 - 03:44 PM, said:
Thank you I will take a look!!!
#10
Re: new to java
Posted 10 December 2009 - 08:06 AM
TriggaMike, on 9 Dec, 2009 - 05:46 PM, said:
I'd be curious to know where you got the idea that AWT was not to be used, or that it was deprecated. Also, as far as I'm aware, nimbus is a Swing L&F released as part of 6u10, so it's not replacing Swing, but it is an alternative to Metal...
#11
Re: new to java
Posted 10 December 2009 - 01:46 PM
gec5741, on 10 Dec, 2009 - 06:51 AM, said:
sam.adams61, on 9 Dec, 2009 - 03:44 PM, said:
Thank you I will take a look!!!
What's the best layout to use for this? I was playing with GridBagLayout but seems kinda of complicated. I'd like to be able to position my lables to the left followed by the appropriate txtfield and so on.
thanks.
#12
Re: new to java
Posted 10 December 2009 - 02:18 PM
gec5741, on 10 Dec, 2009 - 12:46 PM, said:
gec5741, on 10 Dec, 2009 - 06:51 AM, said:
sam.adams61, on 9 Dec, 2009 - 03:44 PM, said:
Thank you I will take a look!!!
What's the best layout to use for this? I was playing with GridBagLayout but seems kinda of complicated. I'd like to be able to position my lables to the left followed by the appropriate txtfield and so on.
thanks.
Here's what I've come up with so far minus the implimatation of action listener or the code to do the math.
sort of what I want but I would like to pretty it up. Not sure if gridbaglayout is the best way to do this. I'd kind of like all my test with the lables to start at the same point to the left then still have all my textfields line up as well on the right of the lables. I'd also like to add a larger font lable with the name of the app along the top middle maybe. Any input would be appreciated. I'm learning at least!
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
public class SwimmCalc {
public static void main(String[] args){
JFrame frame = new JFrame("Swimming Pool Calculator");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450,250);
JPanel panel = new JPanel(new GridBagLayout());
frame.getContentPane().add(panel, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints();
//JLabel titel = new JLabel("Swimming Pool Calculator");
//c.gridx = 0;
//c.gridy = 0;
//panel.add(titel, c);
JLabel lLabel = new JLabel("Enter in the legth of the pool");
c.gridx = 0;
c.gridy = 1;
panel.add(lLabel, c);
c.insets = new Insets(10,10,10,10);
JLabel wLabel = new JLabel("Enter in the width of the pool");
c.gridx = 0;
c.gridy = 2;
panel.add(wLabel, c);
JLabel dLabel = new JLabel("Enter in the average depth of the pool");
c.gridx = 0;
c.gridy = 3;
panel.add(dLabel, c);
JTextField lTextField = new JTextField(10);
c.gridx = 2;
c.gridy = 1;
panel.add(lTextField, c);
JTextField wTextField = new JTextField(10);
c.gridx = 2;
c.gridy = 2;
panel.add(wTextField, c);
JTextField dTextField = new JTextField(10);
c.gridx = 2;
c.gridy = 3;
panel.add(dTextField, c);
JButton calc = new JButton("Calculate Volume");
c.gridx = 0;
c.gridy = 4;
panel.add(calc, c);
JTextField total = new JTextField(10);
c.gridx = 2;
c.gridy = 4;
panel.add(total, c);
}
}
#13
Re: new to java
Posted 10 December 2009 - 08:19 PM
TriggaMike, on 9 Dec, 2009 - 10:46 AM, said:
You can give to AWT all the bad things you want but not inefficient
If Swing took soo much time to impose itself it is because it is a lot less efficient and consume a lot more resource than AWT
Until JRE 1.4 the rule was "unless you do not really need performance stay away from Swing" which has been fortunatly completly redesigned in 1.4.
Actually most of the Swing components still rely on their AWT counter part to achieve what they have to do so you will always have more overhead using Swing than AWT directly
This post has been edited by pbl: 10 December 2009 - 08:22 PM
#14
Re: new to java
Posted 10 December 2009 - 08:31 PM
This post has been edited by macosxnerd101: 10 December 2009 - 08:31 PM
#15
Re: new to java
Posted 10 December 2009 - 09:13 PM
macosxnerd101, on 10 Dec, 2009 - 07:31 PM, said:
Well now that I have my gui made I need to figure out how to make it functional. I'm pretty sure I understand how to make the ActionListener and actionperformed. really all the action needs to do is multiply length*width*depth. But what I'm not sure is how to actually get the data the user input into my text fields and do the multiplication.
I"m guessing I need to first declare 4 variables all doubles being double length, width, depth, volume;
Now how do I actuall get and use the data the user inputs into the text fields? Will that need to be parsed into doube?
I thought this would be easier then it's turing out to be.
|
|

New Topic/Question
Reply




MultiQuote









|