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.
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...
Thanks!