Problem with ActionEvent not doing anything
Hi, I've been learning the basics of Java on and off for a few years, and I am currently attempting to learn how to code GUI stuff. I'm brand new to coding GUIs, only having started using it yesterday. I was trying to use action listeners to respond to button presses. I had some success, although some of my code works and some doesn't. I've commented next to the bit I think is wrong, but I'm unsure what I need to do to fix it. Basically, the nextOne ActionEvent doesn't work. I popped in a System.out.println in there to see if it even 'triggered' at all, but nothing happens.
Code Java:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.Dimension;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class simpleCardGame implements ActionListener{
JButton higher, lower, nextOne;
JPanel mainPane, topPane;
public JFrame setupGame(){
Border empty = BorderFactory.createEmptyBorder(10, 10, 10, 10);
JFrame frame = new JFrame("Nic's simple card game");
frame.setPreferredSize(new Dimension(600, 350));
mainPane = new JPanel();
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
topPane = new JPanel();
JLabel cardPile = new JLabel("", new ImageIcon("cardback.jpg"), JLabel.LEADING);
cardPile.setBorder(empty);
cardPile.setVerticalTextPosition(JLabel.TOP);
cardPile.setHorizontalTextPosition(JLabel.CENTER);
JLabel firstCard = new JLabel("",new ImageIcon("card.jpg"), JLabel.LEADING);
topPane.add(firstCard);
topPane.add(cardPile);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
higher = new JButton("Higher");
higher.setSize(400,800);
higher.addActionListener(this);
lower = new JButton("Lower");
lower.setSize(200,800);
lower.addActionListener(this);
buttonPanel.add(higher);
buttonPanel.add(lower);
mainPane.add(topPane);
mainPane.add(buttonPanel);
frame.setContentPane(mainPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
return frame;
}
public void showAnswer(){
mainPane.removeAll();
topPane = new JPanel();
JLabel firstCard = new JLabel("",new ImageIcon("card.jpg"), JLabel.LEADING);
topPane.add(firstCard);
JLabel revealCard = new JLabel("",new ImageIcon("card2.jpg"), JLabel.LEADING);
topPane.add(revealCard);
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new BoxLayout(answerPanel, BoxLayout.X_AXIS));
JLabel answer = new JLabel("Correct! Or incorrect! Please code this properly!", JLabel.LEADING);
JButton nextOne = new JButton("Next");
nextOne.addActionListener(this);
answerPanel.add(answer);
answerPanel.add(nextOne);
mainPane.add(topPane);
mainPane.add(answerPanel);
mainPane.updateUI();
}
String choice = null;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == higher) {
choice = "higher";
showAnswer();
System.out.println("higher clicked");
}
else if(e.getSource() == lower){
choice = "lower";
showAnswer();
System.out.println("lower clicked");
}
/** THIS IS PROBABLY WHERE I'VE GONE WRONG */
else if(e.getSource() == nextOne){
System.out.println("Next clicked");
mainPane.removeAll();
JFrame anotherFrame = setupGame();
showGame(anotherFrame);
}
}
public void showGame(JFrame frame){
frame.setVisible(true);
}
}
And, probably irrelevant here, but this is the code I use to actually run this...
Code Java:
import javax.swing.JFrame;
public class cardTest {
public static void main(String[] args) {
simpleCardGame myGame = new simpleCardGame();
JFrame thisFrame = myGame.setupGame();
myGame.showGame(thisFrame);
}
}
Thanks! :)
Re: Problem with ActionEvent not doing anything
The JButton you're adding the ActionListener to is not the same JButton you're checking against in your ActionListener.
When you do:
That's "hiding" the nextOne variable that's global, that you're using in your ActionListener.
Re: Problem with ActionEvent not doing anything
Ah, awesome, that would have taken me a very long time to figure out myself! Thanks for your help :)