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

Username:   Password: 
 RegisterRegister   
 Another Java button question
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nezura




PostPosted: Wed Oct 13, 2004 7:51 pm   Post subject: Another Java button question

code:
/**

 * Title:        Using Multiple Buttons<p>

 * Description:  Push a button to answer a question<p>

 * Copyright:    Copyright (c) Howard Tse<p>

 * Company:      Bayview Secondary<p>

 * @author Howard Tse

 * @version 1.0

 */





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



public class multiple_buttons2 extends JApplet implements ActionListener

{
    JLabel[] pictures = new JLabel [3];
    JButton[] buttons = new JButton [3];
    JLabel text = new JLabel ("Press a button for the answer: ");
    public void init ()
    {
        Icon picture1 = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/flag.gif");
        Icon picture2 = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/monkey.gif");
        Icon picture3 = new ImageIcon ("C:/Documents and Settings/Howard/Desktop/book.jpg");

        pictures [0].setIcon (picture1);
        pictures [1].setIcon (picture2);
        pictures [2].setIcon (picture3);


        getContentPane ().setLayout (new GridLayout (3, 2));
        getContentPane ().add (text);


        String[] buttonLabels = {"Button 1: What is the Capital of Zaire?", " Button 2: What was the name of the first monkey in space?", "Button 3: What is one of ms ketter's favorite books?"};


        for (int i = 0 ; i < 3 ; i++)
        {
            buttons [i] = new JButton (buttonLabels [i]);
            buttons [i].addActionListener (this);
            getContentPane ().add (buttons [i]);
            getContentPane ().add (pictures [i]);
        }

    }


    public void actionPerformed (ActionEvent e)

    {

        if (e.getSource () == buttons [0])
        {
            text.setText ("Capital: Kinshasa.");
        }

        else if (e.getSource () == buttons [1])
        {
            text.setText ("First monkey in space: Gordo");
        }
        else if (e.getSource () == buttons [2])
        {
            text.setText ("Ms Ketter's Favorite book: James and the Giant Peach");
        }


    }
}



Um..... you guys could just use any picture and rename the location, but i keep getting an error when i run "Null Pointer Exception" i think this means i have placed an object before instantiating it..but where?

thanks in advance
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Oct 13, 2004 7:57 pm   Post subject: (No subject)

Consider this, just because you declare an array of objects, like you do with "pictures", doesn't mean you've created the objects. You need to create an object before you can do something like:

code:
        pictures[0].setIcon (picture1);
        pictures[1].setIcon (picture2);
        pictures[2].setIcon (picture3);
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  [ 2 Posts ]
Jump to:   


Style:  
Search: