![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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! ![]()
__________________
Children in the dark cause accidents, and accidents in the dark cause children. http://www.ronincoders.org |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Why are you inheriting from ActionListener?
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
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
}
}Last edited by titaniumdecoy; Mar 10th, 2007 at 8:48 PM. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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?
__________________
Children in the dark cause accidents, and accidents in the dark cause children. http://www.ronincoders.org |
|
|
|
|
|
#5 |
|
Expert Programmer
|
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
}
}public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("0")) {
// The button labeled "0" was pressed
}
} |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
Alright, thanks man.
__________________
Children in the dark cause accidents, and accidents in the dark cause children. http://www.ronincoders.org |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SWING stuff... | dsbabyGurl | Java | 6 | Oct 27th, 2006 10:45 PM |
| Java Swing FocusTraversalPolicy | hemanth.balaji | Java | 0 | May 11th, 2006 11:25 PM |
| Swing || AWT Tutorials | Java_König | Java | 2 | Jan 28th, 2006 11:19 PM |
| ASCII conversion loop difficulty | crmpicco | Visual Basic | 2 | May 3rd, 2005 1:55 PM |
| Swing books? | Mjordan2nd | Java | 0 | Mar 2nd, 2005 8:50 PM |