Click to See Complete Forum and Search --> : ActionListener Problem.


BenGabb
March 25th, 2008, 03:01 PM
Hello All,
Im new to the forum, and quiet new to java. im working on my final year dissertation and ive come up with this problem which ive been trying to get my head around for a few days, but i cant seem to get it working. my problem is when i click on the jmenuitem, nothing is displayed, where it should display 4 button and a jlabel. this is the code:

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


public class Interface implements ActionListener{

public JButton yesButton;
public JButton noButton;
public JButton sometimesButton;
public JButton dontKnowButton;
public JButton createEnter;
public JButton questionEnter;
public JButton objectEnter;
public JButton QandAEnter;
public JButton sorryEnter;
public JTextField createField;
public JTextField questionField;
public JTextField objectField;
public JTextField answerField;
public JLabel questionLabel;
public JLabel createfield;
public JLabel questionfield;
public JMenuItem newGame;


public Interface() {

JFrame frame = new JFrame("20 Questions");
frame.setPreferredSize(new Dimension(1200, 750));

// Add a window listner for close button
frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
ImageIcon icon = new ImageIcon("C:/Users/Ben/workspace/images/heading.gif");
JLabel header = new JLabel(icon);
Color myColor = new Color(30, 210, 44);
header.setPreferredSize(new Dimension(900, 300));
frame.getContentPane().setBackground(myColor);
frame.add(header, BorderLayout.NORTH);
JMenu fileMenu = new JMenu("File");
JMenu gameMenu = new JMenu("Game");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(gameMenu);
menuBar.add(Box.createHorizontalGlue());
menuBar.add(helpMenu);

JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);

JMenu citiesItem = new JMenu("Cities");

JMenuItem newGame = new JMenuItem("New Game");
newGame.setActionCommand("newGame");
newGame.addActionListener(this);


JMenuItem addQuestion = new JMenuItem("Add Question");
addQuestion.setActionCommand("addQuestion");
addQuestion.addActionListener(this);

JMenuItem addAnswer = new JMenuItem("Add Answer");
addAnswer.setActionCommand("addAnswer");
addAnswer.addActionListener(this);

JMenuItem refresh = new JMenuItem("Refresh");
refresh.setActionCommand("refresh");
refresh.addActionListener(this);

JMenuItem createItem = new JMenuItem("Create new game");
createItem.setActionCommand("createItem");
createItem.addActionListener(this);

JMenuItem helpItem = new JMenuItem("Help");

JMenuItem aboutItem = new JMenuItem("About 20 Questions");
aboutItem.setActionCommand("aboutItem");
aboutItem.addActionListener(this);

JPanel questions = new JPanel(new GridLayout(6,4));
questions.setBackground(myColor);
frame.add(questions, BorderLayout.SOUTH);
yesButton = new JButton ("YES");
noButton = new JButton ("NO");
sometimesButton = new JButton ("SOMETIMES");
dontKnowButton = new JButton ("DONT KNOW");


questions.add(new JLabel(""));
questions.add(yesButton);
questions.add(noButton);
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(sometimesButton);
questions.add(dontKnowButton);
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel(""));
questions.add(new JLabel("Created by Ben Allan Gabb"));

yesButton.setBackground (Color.yellow);
noButton.setBackground (Color.yellow);
sometimesButton.setBackground (Color.yellow);
dontKnowButton.setBackground (Color.yellow);

yesButton.setVisible(false);
noButton.setVisible(false);
sometimesButton.setVisible(false);
dontKnowButton.setVisible(false);

JPanel middle = new JPanel(new BorderLayout());
JPanel middle1 = new JPanel();
middle.setBackground(myColor);
frame.add(middle, BorderLayout.CENTER);
middle.add(middle1, BorderLayout.NORTH);

JLabel questionLabel = new JLabel("QUESTION WILL APPEAR HERE!!!");
middle1.add(questionLabel);

middle1.setBackground(myColor);
questionLabel.setForeground(Color.yellow);
Font aaaFont = questionLabel.getFont();
Font font3 = aaaFont.deriveFont(30.0f);
questionLabel.setFont(font3);

JPanel middle2 = new JPanel(new GridLayout(5,5));
middle2.setBackground(myColor);
middle.add(middle2, BorderLayout.CENTER);
JLabel createfield = new JLabel("PLEASE ENTER THE GAMES NAME");
JLabel questionfield = new JLabel("PLEASE ENTER YOUR QUESTION");
JLabel answerfield = new JLabel("PLEASE ENTER YOUR ANSWER");
JLabel QandAfiled = new JLabel("'QUESTION + ANSWER = '");
JLabel sorryfield = new JLabel("SORRY, COULDN'T GET IT THIS TIME.");
createfield.setForeground(Color.yellow);
questionfield.setForeground(Color.yellow);
answerfield.setForeground(Color.yellow);
QandAfiled.setForeground(Color.yellow);
sorryfield.setForeground(Color.yellow);
JTextField createField = new JTextField();
JTextField questionField = new JTextField();
JTextField answerField = new JTextField();
JTextField qandAField = new JTextField();
JTextField sorryField = new JTextField();
createEnter = new JButton ("ENTER");
questionEnter = new JButton ("ENTER");
objectEnter = new JButton ("ENTER");
QandAEnter = new JButton ("ENTER");
sorryEnter = new JButton ("ENTER");
middle2.add(new JLabel(""));
middle2.add(createfield);
middle2.add(createField);
middle2.add(createEnter);
middle2.add(new JLabel(""));
middle2.add(new JLabel(""));
middle2.add(questionfield);
middle2.add(questionField);
middle2.add(questionEnter);
middle2.add(new JLabel(""));
middle2.add(new JLabel(""));
middle2.add(answerfield);
middle2.add(answerField);
middle2.add(objectEnter);
middle2.add(new JLabel(""));
middle2.add(new JLabel(""));
middle2.add(QandAfiled);
middle2.add(qandAField);
middle2.add(QandAEnter);
middle2.add(new JLabel(""));
middle2.add(new JLabel(""));
middle2.add(sorryfield);
middle2.add(sorryField);
middle2.add(sorryEnter);
middle2.add(new JLabel(""));
questionLabel.setVisible(false);
createfield.setVisible(false);
createField.setVisible(false);
createEnter.setVisible(false);
questionfield.setVisible(false);
questionField.setVisible(false);
questionEnter.setVisible(false);
answerfield.setVisible(false);
answerField.setVisible(false);
objectEnter.setVisible(false);
QandAfiled.setVisible(false);
qandAField.setVisible(false);
QandAEnter.setVisible(false);
sorryfield.setVisible(false);
sorryField.setVisible(false);
sorryEnter.setVisible(false);


fileMenu.add(exitItem);
gameMenu.add(citiesItem);
citiesItem.add(newGame);
citiesItem.addSeparator();
citiesItem.add(addQuestion);
citiesItem.addSeparator();
citiesItem.add(addAnswer);
citiesItem.addSeparator();
citiesItem.add(refresh);
gameMenu.addSeparator();
gameMenu.add(createItem);
helpMenu.add(helpItem);
helpMenu.addSeparator();
helpMenu.add(aboutItem);



frame.setJMenuBar(menuBar);
frame.pack();
frame.setVisible(true);


}


public void actionPerformed(ActionEvent e)
{
if (e.getSource() == "newGame")
{
questionLabel.setVisible(true);
yesButton.setVisible(true);
noButton.setVisible(true);
sometimesButton.setVisible(true);
dontKnowButton.setVisible(true);
}

else if (e.getSource() == "aboutItem")
{
JFrame frame1 = new JFrame();
JOptionPane.showMessageDialog(frame1,
"\n 20 Questions. \n Project Disseration of \n Ben Gabb",
"20 QUESTIONS",
JOptionPane.PLAIN_MESSAGE);
}

else if (e.getSource() == "Exit")
{
System.exit(-1);
}
}


public static void main(String[] args) {

new Interface();

}

}

Any advice would be brilliant.
Thanks
Ben Gabb

Londbrok
March 25th, 2008, 03:29 PM
Regardless of weather it works or not, my best advice is to scap it and start over. You practically have nothing but a really huge constructor and and interface method implementation. This approach is never going to serve you.

BenGabb
March 25th, 2008, 04:26 PM
thanks Londbrok, could you give me sum advice about what you would put into each method and so on,
again, any advice is brilliant.
Ben Gabb

Londbrok
March 25th, 2008, 06:20 PM
It just all and all makes little sense. What is the point in having three throusand million JLabels with blank text? Are you trying to have the layout match by pushing the relevant components with such fillers perhaps... or are they actually used for something?

You should do only what you need inside the constructor. Generally it is not ideal to layout anything there, it is better to leave the gui building to an outside method. That way your component can grow in a far more feasable way. You could not possibly subclass this and expect anything sensible out of it. While I dont know if that is your intention at all, still leaving the option open is worth considering.

Would it make more sense to have all your questions in a List of Strings, have a method that provides the next question and display it in a single textarea component? It looks like you are intending to ravage the dialog with all 20 questions at once? How would you know which one the user answers? Do you have also 20 components for answers?

Take what ever you can out of the constructor. Leave only initialization of those you are sure need to be instance methods. Construct the gui building in a set of methods. For instance, build menu in one method. And you can build any menu in the same method, just give it right params and return a new menu. Same goes for buttons, labels etc.

good luck.

BenGabb
March 26th, 2008, 06:27 AM
thanks londbrok,
think i know were im going now,
big help, cheers
Ben

Londbrok
March 26th, 2008, 09:19 AM
And as to your actionlistener not working. You call Event.getSource(), which returns the Object which initiated the event. It does not equal String. So in what you have now, I suppose

if (e.getSource() == newGame) would work, while
if (e.getSource() == "newGame") is soooo not the same thing.