I am encountering some difficulties in making things happen when I click a button.
This is where the buttons are declared:
public class DoND extends JFrame implements ActionListener { public JButton btnsuit1, btnsuit2, ... , btnsuit26; public static void main(String[] args) { new DoND(); }
Here is detailed information about two of the buttons (there are a total of 26).
JButton btnsuit1 = new JButton(); btnsuit1.setIcon(new ImageIcon("images\\suitcases\\case1.png")); btnsuit1.setPreferredSize(new Dimension(200, 150)); btnsuit1.setHorizontalAlignment(SwingConstants.CENTER); btnsuit1.addActionListener(this); JButton btnsuit2 = new JButton(); btnsuit2.setIcon(new ImageIcon("images\\suitcases\\case2.png")); btnsuit2.setPreferredSize(new Dimension(200, 150)); btnsuit2.setHorizontalAlignment(SwingConstants.CENTER); btnsuit2.addActionListener(this);
This is where the buttons are added to the center panel, which is then added to the main panel, and finally to the frame.
Center Panel
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
centerPanel.setBackground(Color.BLACK);
centerPanel.add(btnsuit1);
centerPanel.add(btnsuit2);
Main Panel
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(Color.BLACK);
mainPanel.add(northPanel, BorderLayout.NORTH);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(eastPanel, BorderLayout.EAST);
mainPanel.add(westPanel, BorderLayout.WEST);
mainPanel.add(southPanel, BorderLayout.SOUTH);
Frame
setContentPane(mainPanel);
setSize(3000, 1000);
setTitle("Deal or No Deal");
setLocationRelativeTo(null);
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
And here is the action listener:
public void actionPerformed(ActionEvent e) { if (e.getSource() == btnsuit1) { btnsuit1.setVisible(false); } if (e.getSource() == btnsuit2) { btnsuit2.setVisible(false); }
I am unsure why nothing is happening, but there are a few possibilities that I have considered:
a) The buttons with ActionListener may differ from what ActionPerformed is looking for.
b) I might need to extend ActionListener to the secondary panel where all my buttons reside.
Your help is greatly appreciated.
--
Grant