Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Buttons/Counter Question
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Crim009




PostPosted: Sun Oct 24, 2004 9:59 am   Post subject: Buttons/Counter Question

Hey guys, i got a mini assignment from school and i need some help on it. I've created 3 frames so far but i don't really understand the concept of the counters with the buttons. like how to click on "Question 1" and make it pop up and make the menu "false". I don't understand how to add counters to the buttons. So if someone could help me get started with just one of the frames, greatly appreciated I will use ur model to understand the buttons better. Comments in the codes are appreciated Thanks.

*Using RTP

code:




import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class mbassignment2 extends JApplet implements ActionListener

{
    JButton[] buttons1 = new JButton [6];
    JLabel[] empty1 = new JLabel [4];
    JButton[] buttons2 = new JButton [6];
    JLabel[] empty2 = new JLabel [4];
    JButton[] buttons3 = new JButton [6];
    JLabel[] empty3 = new JLabel [4];
    JButton[] buttons4 = new JButton [6];
    JLabel[] empty4 = new JLabel [4];
    JButton[] buttons5 = new JButton [6];
    JLabel[] empty5 = new JLabel [4];
    JButton[] buttons6 = new JButton [6];
    JLabel[] empty6 = new JLabel [4];
    JButton[] buttons7 = new JButton [6];
    JLabel[] empty7 = new JLabel [4];
    Container contents1 = new Container ();
    Container contents2 = new Container ();
    Container contents3 = new Container ();
    Container contents4 = new Container ();
    Container contents5 = new Container ();
    Container contents6 = new Container ();
    Container contents7 = new Container ();

    static int score = 0;
    JFrame frame1 = new JFrame ("The Ultimate Java Quiz");
    JFrame frame2 = new JFrame ("Question 1");
    JFrame frame3 = new JFrame ("Question 2");
    JFrame frame4 = new JFrame ("Question 3");
    JFrame frame5 = new JFrame ("Question 4");
    JFrame frame6 = new JFrame ("Question 5");
    JFrame frame7 = new JFrame ("Display Score");

    public void init ()
    {
        for (int x = 0 ; x < 4 ; x++)
        {
            empty1 [x] = new JLabel ("");
        }

        String[] question1 = {"Question 1", "Question 2", "Question 3", "Question 4", "Question 5", "Display Score"};

        for (int x = 0 ; x < 6 ; x++)
        {
            buttons1 [x] = new JButton (question1 [x]);
        }

        contents1.setLayout (new GridLayout (5, 2));
        contents1.add (buttons1 [0]);
        contents1.add (empty1 [0]);
        contents1.add (buttons1 [1]);
        contents1.add (empty1 [1]);
        contents1.add (buttons1 [2]);
        contents1.add (empty1 [2]);
        contents1.add (buttons1 [3]);
        contents1.add (empty1 [3]);
        contents1.add (buttons1 [4]);
        contents1.add (buttons1 [5]);

        frame1.getContentPane ().add (contents1);
        frame1.setSize (600, 600);
        frame1.setVisible (true);

       
        ////  MENU ///////

        JLabel[] q1 = new JLabel [4];

        String[] question2 = {"Compiled", "Interpreted", "Neither", "both a & b"};
        for (int x = 0 ; x < 4 ; x++)
            buttons2 [x] = new JButton (question2 [x]);


        q1 [0].setText ("What Type");
        q1 [1].setText ("of languge");
        q1 [2].setText ("do Java Applets");
        q1 [3].setText ("run in?");

        contents2.setLayout (new GridLayout (5, 2));
        contents2.add (q1 [0]);
        contents2.add (q1 [1]);
        contents2.add (q1 [2]);
        contents2.add (q1 [3]);
        contents2.add (buttons2 [0]);
        contents2.add (buttons2 [1]);
        contents2.add (buttons2 [2]);
        contents2.add (buttons2 [3]);

        frame2.getContentPane ().add (contents2);
        frame2.setSize (600, 600);
        frame2.setVisible (false);

       
        ////// Question 1 /////////

        JLabel[] q2 = new JLabel [4];

        String[] question3 = {"Nothing will happen", "it will run, no mistake", "crash & restart your computer", "you get a Null Pointer Exception"};
        for (int x = 0 ; x < 4 ; x++)
            buttons3 [x] = new JButton (question3 [x]);

        q2 [0].setText ("What happens when");
        q2 [1].setText ("you use an object");
        q2 [2].setText ("before you");
        q2 [3].setText ("instantiate it?");

        contents3.setLayout (new GridLayout (5, 2));
        contents3.add (q2 [0]);
        contents3.add (q2 [1]);
        contents3.add (q2 [2]);
        contents3.add (q2 [3]);
        contents3.add (buttons3 [0]);
        contents3.add (buttons3 [1]);
        contents3.add (buttons3 [2]);
        contents3.add (buttons3 [3]);

        frame3.getContentPane ().add (contents3);
        frame3.setSize (600, 600);
        frame3.setVisible (false);
        /////// Queston 2 /////////


    }
}

Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sun Oct 24, 2004 10:07 am   Post subject: (No subject)

u'd need to add in an event listener
Crim009




PostPosted: Sun Oct 24, 2004 5:05 pm   Post subject: (No subject)

Ok after adding action listener, i still don't understand how to make the buttons work, anyone with more descriptive help? I need to get this thanks Confused
rizzix




PostPosted: Sun Oct 24, 2004 7:07 pm   Post subject: (No subject)

update ur code will ya.. i'll show u where ur going wrong.
Crim009




PostPosted: Sun Oct 24, 2004 7:20 pm   Post subject: (No subject)

I added 'implements ActionListener' but I don't know how to make the Question 1 frame pop up when you click on it and make the menu invisible. sorry for asking dumb questions but I don't understand how to use counters to do this, my teacher gave me an example but I don't understand it, if you could help me get started, that would be great. thanks pls add comments if u can
rizzix




PostPosted: Sun Oct 24, 2004 7:45 pm   Post subject: (No subject)

hmm think about it logically just implementing Actionlistener is not going to get what u want done automatically.

firstly delete that "implements...." part that u added in there

next u create an inner class that extends the ActionListener interface.. u can learn more on this by reading this tutorial: Abstract Class, Interface and Nested Class

now read the "whole" tutorial carefully (at least 3 times over) u'll be aware of the subtle but important ideas i've tried to convey. and u will also be able to use and implement interfaces correctly. just make sure u dont skip over anything.


this only a hint. and the first step u need to do ofcourse.. now. after reading it. u should try and use it in your code up there.
Crim009




PostPosted: Sun Oct 24, 2004 8:27 pm   Post subject: (No subject)

Hey, why do you have to use a more complicated method? Our teacher gave us an example using getsource, so could you explain how i could use that on my program instead? I'm too newb to handle that innerclass thingy >_< Sad Sad
rizzix




PostPosted: Mon Oct 25, 2004 2:21 pm   Post subject: (No subject)

well an inner class is just an ordinary class declared within the scope of another class.. thats all it is.. but you could have just as well have created an ordinary class that implements the ActionListener interface. and it would work nevertheless.. although, it makes a lot of sense to use an inner class.

here's an example of an inner class :
code:

    class CustomListener implements ActionListener
    {
        JButton source;
       
        CustomListener(JButton btn) {
            source = btn;
        }
   
        public void actionPerformed(ActionEvent e) {
            frame2.show();// something u do here
        }
    }
now u'd have to modify it for use in your code definitely.

of course just creating a class that would handle the event is not good enough.. u need to create objects of this class and link them with the buttons you created. you do this something like this:

code:

somebutton.addActionListener(new CustomListener(somebutton));
Sponsor
Sponsor
Sponsor
sponsor
Crim009




PostPosted: Mon Oct 25, 2004 5:17 pm   Post subject: (No subject)

hey thanks for the help rizzix, I will try it out. If I have more problems I will post....seems like ur the only one answrering my questions lol
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: