Results 1 to 6 of 6
- 12-10-2009, 09:41 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Jlabel,Jbutton,If, Trying to connect them all??
i am having trouble putting together all the pieces of the puzzle to work together in harmony. My final project is simple,i have a jlabel with the question of how much does your dog weigh, I have a button and a jtextfield, the user enters the amount of there dog, and the program will tell them what size dog there's is... ( e.g. small, large )
I cant figure out what works with what, i have tried things from if statements but they don't work with strings, to get source, but i have a problem with errors... What should i use to get the outcome i want...? here is my starting code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Final2 extends JFrame implements ActionListener
{
JLabel question = new JLabel("How much does your dog weigh?");
Font bigFont = new Font("Arial", Font.BOLD, 16);
JTextField answer = new JTextField(10);
JButton pressMe = new JButton("press me");
JLabel greeting = new JLabel("");
final int WIDTH = 300;
final int HEIGHT = 300;
Container con = getContentPane();
public Final2()
{
super("Final Project");
setSize(WIDTH,HEIGHT);
setLayout(new FlowLayout());
question.setFont(bigFont);
greeting.setFont(bigFont);
greeting.setForeground(Color.WHITE);
question.setForeground(Color.WHITE);
add(question);
add(answer);
add(pressMe);
add(greeting);
pressMe.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setBackground(Color.BLUE);
}
public void actionPerformed(ActionEvent e)
{
String Small = "1";
String breed = answer.getText();
String greet = "Your dog is of " + breed + " breed.";
greeting.setText(greet);
}
public static void main(String[] args)
{
Final2 frame = new Final2();
}
}
- 12-10-2009, 10:09 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
2 tips for getting a reply:
1. Use Code tags, the php tags is the best cause they keep the identing.
2. Dont make once line that is 10000 characters long, No one cares to read it.
3. Use enter more ofthen.
- 12-10-2009, 10:30 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
although i do appreciate your constructive criticism, do you have any knowledge upon the question i have posted previous?
- I cant figure out what works with what, i have tried things from if statements but they don't work with strings, to get source, but i have a problem with errors
... What should i use to get the outcome i want...?
here is my starting code...
As I see it, you'll need to give us more information for us to be able to help you including defining fully just what you're trying to achieve, showing the code causing errors and the error messages themselves (not an interpretation of them). I also agree that you should edit your first post to add code tags so that we can read your code. Please see my signature below to find out more about code tags. The easier you make it for us to help you, the greater your chances of getting good help.
Much luck!
-
Likely the key to your project is to translate a String into an int. Please note that I'm just guessing here because we really can't say for sure based on the scant information presented in your post. If that's the case, then you'll want to parse the input String into an int by using Integer.parseInt(...). If you do use this, it's generally a good idea to do it inside of a try/catch block so you can handle bad input, for instance if you are expecting them to enter a number, and the instead enter "fubar". For example:
Java Code:public void actionPerformed(ActionEvent e) { // placed in a try-catch block since we need to parse a String to a number // and need a way to gracefully handle bad input try { // get text and parse it into an int int weight = Integer.parseInt(answer.getText()); String dogSize = ""; // create string variable and initialize it with "" // if blocks or a switch block goes here so that // based on the weight, you can assign a proper String to dogSize // TODO: add your code here! // use dogSize here String greet = "Your dog is a " + dogSize + "-sized breed."; greeting.setText(greet); } // what happens if bad input entered: catch (NumberFormatException nfe) { greeting.setText("You must enter a whole number only."); } }
- 12-11-2009, 07:15 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Thank you,
The final program is supposed to ask the user for there dogs weight, after entered the weight is supposed to match to a word( ex. 1-30lb will be small, 31-60lb will be medium) so if the user enters 30 a string will be call forth saying your dog is a small dog.
to tell you the full end of it(sorry for the incomplete information) I want my program to ask the user a question using Jlabel, using a jtextfield to accept the answer, and using the jbutton to pull the answer in.... im am currently trying to use the try and catch , i do belive that it will be the key. but so far a little stuck, we never used that one in class.
Similar Threads
-
[SOLVED] JButton actionlistener, if statement, JLabel visibility
By JonoF in forum New To JavaReplies: 2Last Post: 04-19-2009, 06:39 AM -
jLabel
By Matty in forum AWT / SwingReplies: 3Last Post: 09-22-2008, 11:22 PM -
GUI - JLabel
By Azndaddy in forum New To JavaReplies: 8Last Post: 05-02-2008, 08:03 AM -
JLabel
By Jack in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 02:55 PM -
JLabel
By Freddie in forum AWT / SwingReplies: 2Last Post: 05-29-2007, 03:19 PM
Bookmarks