Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Project

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Project

    I'm trying to write a simple, click event based program and I need some help. I lack the experience to handle this on my own.

    Here's what I have so far~ (I'm not worried about perfection~ the logic just has to look right.)

    import java.awt.*;
     
    public class UserSample extends Frame {
    UserSample (){
    setTitle("Frame Example");
    setSize(x,x);
    show();
    }
    public static void main(String args[]) {
    Frame objFrame;
    Button objButton1;
    Button objButton2;
    Button objButton3;
    TextField objTextField1;
    Label objLabel1;
     
    	objFrame = new UserSample ();
    	objButton1 = new Button("Red")
    	objButton2 = New Button("Blue");
    	objButton3 = New Button("Result");
    	objTextField1 = new TextField(" ",0);
    	objLabel1 = new Label();
    	objLabel1.setText("Tally Scores")
     
    	objLabel1.setBounds(x,x,x,x);
    	objTextField1.setBounds(x,x,x,x);
    	objButton1.setBounds(x,x,x,x);
    	objButton2.setBounds(x,x,x,x);
    	objButton3.setBounds(x,x,x,x);
    	objFrame.add(objLabel1);
    	objFrame.add(objTextField1);
    	objFrame.add(objButton1);
    	objFrame.add(objButton2);
    	objFRame.add(objButton3);
     
    }
    }

    As you can see I have two buttons~ Red and Blue. I want the textfield to display the result after hitting button 'result'. Help me out?
    I know it's going to be an if/then loop...or something.
    And I'm stupid enough that I wouldn't know how to have the textbox display the result.

    Thanks in advance,
    Mac
    Last edited by Mac; June 1st, 2010 at 08:46 AM.


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Project

    Firstly welcome to the forums.

    Secondly I would recommend you read this thread Java Programming Forums - Announcements in Forum : What's Wrong With My Code?.

    Now onto your post I am guessing this code has been given to you by your lecturer/tutor/whatever as a base and you have to put the functionality into it. We are more then happy to help but we will not do your homework for you. You say you don't have the experience well that is why you need to do this yourself to get experience. So you might not like to hear this but I would recommend you sit down with this decide what exactly it needs to do(I'm guessing keep count of the amount of times the 2 buttons are pressed) give it your BEST attempt and if you get stuck and start pulling your hairs out come back with that problem and we will be more then happy to help, or at least give your thoughts on how you think it sould be approached and we will steer you in the right direction.

  3. The Following User Says Thank You to Faz For This Useful Post:

    JavaPF (June 1st, 2010)

  4. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    Thank you for the welcome. I will read the thread you recommended.

    True enough, I was given the assignment by my instructor~ but not in the way that you think.

    I was told to make a practical applet----and this is what I came up with.
    I've written the code thus far by myself.

    You're exactly right on what I want it to do.
    I'm terrible with if/else statements, and we haven't written code that requires knowledge of buttons so far. This is our first applet code.

  5. #4
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Project

    OK fair enough you say you haven't written code that requires knowledge of buttons, do you have any knowledge of action listeners? You shouldn't need any If/else or loops(No such thing as an if/else loop as far as I know ) as far as I can see just something to keep a count of the clicks.

    Check out this page on buttons and action listeners. How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Maybe write some pseudo code on what you want it to do (I always hated pseudo code when I started but it's a good habit to get into early)

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    What I meant by if/else loop was that instead of writing code for every button click- if blue, tally 1 else red tally 1
    just have it loop the method?
    If that makes sense. I'm not too hot on terminology.

    Will read the pages you guys suggested~
    thanks for the nudge.

  8. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    //In initialization code:
        chinButton = new JCheckBox("Chin");
        chinButton.setMnemonic(KeyEvent.VK_C); 
        chinButton.setSelected(true);
     
        glassesButton = new JCheckBox("Glasses");
        glassesButton.setMnemonic(KeyEvent.VK_G); 
        glassesButton.setSelected(true);
     
        hairButton = new JCheckBox("Hair");
        hairButton.setMnemonic(KeyEvent.VK_H); 
        hairButton.setSelected(true);
     
        teethButton = new JCheckBox("Teeth");
        teethButton.setMnemonic(KeyEvent.VK_T); 
        teethButton.setSelected(true);
     
        //Register a listener for the check boxes.
        chinButton.addItemListener(this);
        glassesButton.addItemListener(this);
        hairButton.addItemListener(this);
        teethButton.addItemListener(this);
    ...
    public void itemStateChanged(ItemEvent e) {
        ...
        Object source = e.getItemSelectable();
     
        if (source == chinButton) {
            //...make a note of it...
        } else if (source == glassesButton) {
            //...make a note of it...
        } else if (source == hairButton) {
            //...make a note of it...
        } else if (source == teethButton) {
            //...make a note of it...
        }
     
        if (e.getStateChange() == ItemEvent.DESELECTED)
            //...make a note of it...
        ...
        updatePicture();
    }

    This is the checkbox code from the page you suggested, Faz. I could potentially mimic this, instead of having a button click counter deal...
    Something like this~
    red/blue
    Round one: check check
    Two: check check

    Not sure if that would be easier or not.

    JavaPF- in your code, explain the following for me:
                    System.out.println("You clicked the button");
                }
            });      
     
            frame1.getContentPane().add(button);
            frame1.pack();
            frame1.setVisible(true);
        }
     
     
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();

    I don't need a system.println I need it to display in the textbox...and the code is just making a button that reacts...how would I get it to count?

    Bear with me y'all. >.>

  9. #8
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Project

    OK I see what you mean by the if else thing now, you could check the text and add to the tally based on that and that way the 2 buttons could share an action listener.

    I can't say I understand what you mean by the check check text box thing though could you maybe explain that for me?

    What I would say you should do is have a pair of variables (integers). Say for the red button have a variable called redCount set it originally to 0 and then if the red button is clicked add 1 to the current total. You get me?

  10. #9
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    Imagine one of those online quizzes you take with the circle-shaped check boxes.
    Now imagine each question is Red or Blue. First circle is red, second is blue.

    It would look like:

    who won? red blue
    round 1 red blue
    round 2 red blue
    round 3 red blue
    round 4 red blue

    How would I set redCount/blueCount to work off of the button click is my main question.
    I assume that utilizes the code JavaPF referenced for me- the action/listener.
    Instead of it doing a system.println, how do I attach them to each other?

  11. #10
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    If you have AIM/Live, hit me up?
    Would make it easier than hitting refresh and jazz.

    Macabrepoet92 for AIM or @hotmail for live

  12. #11
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Project

    Sorry no I'm in work running some large queries on a database and just nipping on here while they are running(servers were just moved from Belgium to Frankfurt so plenty of gremlins slowing the whole process down this week).

    OK what you will want the action listener(or listeners) to do is increment the count by one depending on what one was clicked so say if you had an action listener for each button all the code for the one working off the red button would need is:
    redCount++
    thats the same as
    redCount += 1
    or
    redCount = redCount + 1

  13. #12
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    Here's what I'm looking at. I think I've definitely made progress.

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class UserSample extends Frame {
    UserSample (){
    setTitle("Frame Example");
    setSize(x,x);
    show();
    }
    public static void main(String args[]) {
    Frame objFrame;
    Button objButton1;
    Button objButton2;
    Button objButton3;
    TextField objTextField1;
    Label objLabel1;
     
    	objFrame = new UserSample ();
    	objButton1 = new Button("Red")
    	objButton2 = New Button("Blue");
    	objButton3 = New Button("Result");
    	objTextField1 = new TextField(" ",0);
    	objLabel1 = new Label();
    	objLabel1.setText("Tally Scores")
     
    	Button1.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e)
    	{
    	redCount++
    	}
    	});
    	Button2.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e)
    	{
    	blueCount++
    	}
    	});
    	Button3.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e)
     
    This would be an if/else statement to see which had the higher score- red or blue and print in TextField1. I assume that would look like~
    	Textfield1 = (if/else code...which I'll need help with.)
     
     
     
    	objLabel1.setBounds(x,x,x,x);
    	objTextField1.setBounds(x,x,x,x);
    	objButton1.setBounds(x,x,x,x);
    	objButton2.setBounds(x,x,x,x);
    	objButton3.setBounds(x,x,x,x);
    	objFrame.add(objLabel1);
    	objFrame.add(objTextField1);
    	objFrame.add(objButton1);
    	objFrame.add(objButton2);
    	objFRame.add(objButton3);
     
    }
    }

  14. #13
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Project

    OK first of all you are saying Button1.addActionListener, what is Button1? (I know what you mean but Java won't it's objButton1) also I would recommend giving them more meaningfull names like objButtonRed or btnRed to save you time checking which number corresponds to which colour.

    Secondly the way JavaPF has shown you is actually new to me looks alot cleaner and flexible to the way I was thinking and I haven't worked with actionlisteners(or Java for that matter) for quite a while. I would have to have a bit of a play around with it to make sure I understand it fully but it looks alright.

    As for what you put in redCount++ increments redCount by one but you will need to declare it first. As for what the Tally Scores button should do a simple if statement will do firsty you want to check if its a draw (both are equal) and if it's not check which is higher, have a go writting this yourself.

  15. #14
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    Thanks for all your help thus far, Faz. I fixed up my button names to buttonRed like you suggested, changed it to objButton and jazz.

    Looking back through old work I found a code I'd written (and completely forgotten) that found the lesser of two.
    I mimicked it and got:
    class Result
    {
    public static void main(String args[])
    {
    int redscore=redCount, bluescore=blueCount
    if(redscore>bluescore)
    {
    Textfield1 = ("Red won.")
    }
    else if(bluescore>redscore)
    {
    Textfield1 = ("Blue won.")
    }
    else if(redscore = bluescore)
    {
    Textfield1 = ("The game is a draw.")
    }
    }
    }

    Will java read blueCount/redCount as the last known integer?
    I feel I've declared those wrong...and is my Textfield1 = ("") formatted correctly?

  16. #15
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Project

    Just realized creating the class was stupid cause it interrupts the action listener. took it out so the code starts at the
    int redscore~~~~~

  17. #16
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Project

    To be honest the best way you learn these things is trying them out. If your stuck this forum should probably be your last port of call. I would generally recommend you trying doing some of your own debugging first as this is essential to learn experiment a bit see what works and what doesn't. Also one skill I have developed is learning how to search the internet to solve your problems.

    I would also say to try and work with other people in your class I don't mean do one assignement between you and hand it up the same thing that is a sure fire way of getting a 0 but whenever one of you has a problem the other(s) can help out and doing this everyone will learn. In short in programming you learn the most from the mistakes you make and solving problems then just reading theory or getting some ready made solution and if you are curious as to whether something will work there is only one way to find out.

    So I've been away for a few days so have you made any progress since then?

Similar Threads

  1. help with project
    By pairenoid in forum Object Oriented Programming
    Replies: 1
    Last Post: May 7th, 2010, 08:55 AM
  2. Project - Please Help
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 09:58 AM
  3. Need Help With Project
    By jstew132 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2009, 07:15 PM
  4. Need a project
    By helloworld922 in forum Project Collaboration
    Replies: 6
    Last Post: July 31st, 2009, 08:30 AM
  5. Adventure ride project
    By Ciwan in forum Java Theory & Questions
    Replies: 12
    Last Post: February 15th, 2009, 12:08 PM