Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Having difficulty with Swing. (http://www.programmingforums.org/showthread.php?t=12765)

Intimidat0r Mar 10th, 2007 7:03 PM

Having difficulty with Swing.
 
I'm just starting out with learning about Swing, and I'm running into some trouble already. Basically what's happening is I'm trying to make an array of 9 buttons (well a 3x3 array) but, well, let me show you:

:

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

public class guiTest implements ActionListener { //error 1
        private static JButton[] bbox;

        public static void main(String args[]) {
                JPanel panel = new JPanel();
                panel.setLayout(new GridLayout(3,3));
                bbox = new JButton[9];
                for (int h = 0; h < 9; h++) {
                        panel.add(bbox[h]);
                        bbox[h].addActionListener(this); //error 2
                }
                Container c = getContentPane(); //error 3
                c.add(panel, BorderLayout.CENTER);
                JFrame f = new JFrame("GUI Test!!!");
                f.add(c);
                }
        }
}


I've commented the three lines in which the following three errors are reported by the compiler:

:

guiTest is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class guiTest implements ActionListener {
      ^
non-static variable this cannot be referenced from a static context
            bbox[h].addActionListener(this);
                                                  ^
cannot find symbol
symbol  : method getContentPane()
location: class TicTacToe
        Container c = getContentPane();
                              ^


Any help? Thanks in advance! :)

Arevos Mar 10th, 2007 8:27 PM

Why are you inheriting from ActionListener?

titaniumdecoy Mar 10th, 2007 9:29 PM

No offense, but the multitude of fundamental problems with your code most likely indicates that your understanding of the Java language is insufficient to be working with GUIs. Instead of taking the time to write out an explanation for each problem, I rewrote the code for you to study.

:

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

public class GUITest extends JFrame implements ActionListener {
    private static JButton[] bbox;
   
    public GUITest() {
       
        super("GUI Test");
       
        setLayout(new GridLayout(3,3));
       
        bbox = new JButton[9];
       
        for (int i = 0; i < bbox.length; i++) {
            bbox[i] = new JButton(Integer.toString(i));
            bbox[i].addActionListener(this);
            add(bbox[i]);
        }
       
        pack();
       
        setVisible(true);
       
    }

    public static void main(String args[]) {
        new GUITest();
    }

    public void actionPerformed(ActionEvent evt) {
        // This method is called when a button is clicked
    }
   
}


Intimidat0r Mar 10th, 2007 11:36 PM

Thanks. I am pretty new to Java. Let me ask you one more thing? Is there a way (presumably in actionPerformed()) to tell which button of the array was clicked? Or do I have to use a different method for each one?

titaniumdecoy Mar 10th, 2007 11:42 PM

I prefer to use the getSource() method of the ActionEvent object:

:

public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == bbox[0]) {
        // bbox[0] was pressed
    }
}

You can get the name of the button with the getActionCommand() method, but remember that if you change the button's text, that change will be reflected in the value returned by this function.

:

public void actionPerformed(ActionEvent evt) {
    if (evt.getActionCommand().equals("0")) {
        // The button labeled "0" was pressed
    }
}


Intimidat0r Mar 10th, 2007 11:48 PM

Alright, thanks man.


All times are GMT -5. The time now is 2:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC