Results 1 to 6 of 6
  1. #1
    2scoopdelux is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default 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();


    }




    }

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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.

  3. #3
    2scoopdelux is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    although i do appreciate your constructive criticism, do you have any knowledge upon the question i have posted previous?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    It would be great for you to show us these attempts and the errors that they cause.

    ... What should i use to get the outcome i want...?
    It's hard to say as I don't see where you fully defined exactly what outcome you want. We are terrible at guessing here.

    here is my starting code...
    I don't see any errors in this code, but I'm still not sure what you're trying to do and how your attempts are causing errors.

    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!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.");
        }
      }

  6. #6
    2scoopdelux is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    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

  1. Replies: 2
    Last Post: 04-19-2009, 06:39 AM
  2. jLabel
    By Matty in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2008, 11:22 PM
  3. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 08:03 AM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 02:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 03:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •