-
generating 26 buttons
hello
i am tryin to generate 26 buttons but i keep getting the following error hilighted in red, i have tried shifting paragraph of codes in different places but i end up getting more errors. i would really appreciate if you could pint out where i had gone wrong.
thank you for your time and help :)
Code:
import java.awt.Button;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HangmanPanel extends JFrame
{
//this is the used letter array
private boolean usd[] = new boolean[26];
private String guessme;
private int numguesses=0;
private boolean finished = false;
private boolean won = false;
private Button a[][COLOR="Red"];[/COLOR]
public void init() {
int i;
StringBuffer buffer;
a = new Button[26];
new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
// create all 26 buttons
for (i = 0; i <26; i++) {
buffer = new StringBuffer();
buffer.append((char)(i+65));
a[i] = new Button(buffer.toString());
a[i].addActionListener( this );
add(a[i]);
}
// make the guessed word uppercase
guessme=getParameter("wrd").toUpperCase();
}
[COLOR="Red"])[/COLOR];
}
and this is my main method class
Code:
import javax.swing.JFrame;
public class extend {
public static void main(String args[])
{
HangmanPanel hangmanPanel = new HangmanPanel();
hangmanPanel.setVisible(true);
hangmanPanel.setSize(200, 200);
hangmanPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
-
What specific error message(s) are you getting and what line throws it? Also, while this isn't the source of your error, you shouldn't be using AWT components (i.e., Button) in a Swing application. Rather, use Swing components (JButton).
Also, if you clean up your posted code's indentation you'll have a better chance of getting help.
Much luck!
-
On looking at your code, you've got some strange things in there such as a new ActionListener call that doesn't belong where it is. I'm not really sure what you are trying to do here, and suspect that you may be better of to simply scrap this code and start over again.
Best of luck.
-
it is highlighted in red
;
and
)
:o
-
Your problem is not on that line (even if it seems that it is), but rather is with the new ActionListener() call sitting out in no-where. Again, scrap this code, restart.
I recommend that your class contain a private inner ActionListener class and that it use that. Something like:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HangPanel extends JPanel {
private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private JButton[] letterButtons = new JButton[LETTERS.length()];
public HangPanel() {
setLayout(new GridLayout(2, 0, 5, 5));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
LetterBtnListener letterBtnListener = new LetterBtnListener();
for (int i = 0; i < LETTERS.length(); i++) {
String letter = String.valueOf(LETTERS.charAt(i));
// TODO create buttons, add action listener, and add to panel
}
}
private class LetterBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed is: " + e.getActionCommand());
// TODO Finish this
}
}
}