Im having a weird problem with the if statement and the setIcon for the JButton. What I wanted the program to do is, the first button clicked on will show a image on and O then the next button clicked on will have a image of an X. for some reason it only shows the O. It also has the O on the even button clicked on. If say I change the order like have the X show up for the first button clicked then every button I click will just show an X. The while loop and if statement seems to run fine, I ran in debug mode, so idk what could be wrong.
Thanks
while(xxx <=9){ if (xxx % 2 == 0){ a.addActionListener(new EnlargeListener(a)); b.addActionListener(new EnlargeListener(b)); c.addActionListener(new EnlargeListener(c)); d.addActionListener(new EnlargeListener(d)); e.addActionListener(new EnlargeListener(e)); f.addActionListener(new EnlargeListener(f)); g.addActionListener(new EnlargeListener(g)); h.addActionListener(new EnlargeListener(h)); i.addActionListener(new EnlargeListener(i)); } else { a.addActionListener(new o(a)); b.addActionListener(new o(b)); c.addActionListener(new o(c)); d.addActionListener(new o(d)); e.addActionListener(new o(e)); f.addActionListener(new o(f)); g.addActionListener(new o(g)); h.addActionListener(new o(h)); i.addActionListener(new o(i)); } xxx++;
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class mouse extends JFrame { private JButton a = new JButton(); private JButton b = new JButton(); private JButton c = new JButton(); private JButton d = new JButton(); private JButton e = new JButton(); private JButton f = new JButton(); private JButton g = new JButton(); private JButton h = new JButton(); private JButton i = new JButton(); public mouse() { JPanel p1 = new JPanel(); // Use the panel to group buttons p1.setLayout(new GridLayout(3,3)); p1.add(a); p1.add(b); p1.add(c); p1.add(d); p1.add(e); p1.add(f); p1.add(g); p1.add(h); p1.add(i); JPanel p2 = new JPanel(new BorderLayout()); p2.add(new JTextField("test"),BorderLayout.SOUTH); p2.add(p1,BorderLayout.CENTER); add(p2); int xxx = 1; while(xxx <=9){ if (xxx % 2 == 0){ a.addActionListener(new EnlargeListener(a)); b.addActionListener(new EnlargeListener(b)); c.addActionListener(new EnlargeListener(c)); d.addActionListener(new EnlargeListener(d)); e.addActionListener(new EnlargeListener(e)); f.addActionListener(new EnlargeListener(f)); g.addActionListener(new EnlargeListener(g)); h.addActionListener(new EnlargeListener(h)); i.addActionListener(new EnlargeListener(i)); } else { a.addActionListener(new o(a)); b.addActionListener(new o(b)); c.addActionListener(new o(c)); d.addActionListener(new o(d)); e.addActionListener(new o(e)); f.addActionListener(new o(f)); g.addActionListener(new o(g)); h.addActionListener(new o(h)); i.addActionListener(new o(i)); } xxx++; } } /** Main method */ public static void main(String[] args) { JFrame frame = new mouse(); frame.setTitle("ControlCircle2"); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; class o implements ActionListener { private CirclePanel canvas = new CirclePanel(); JButton cc; public o(JButton a) { this.cc=a; } public void actionPerformed(ActionEvent e) { canvas.o(cc); } }import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; class EnlargeListener implements ActionListener { private CirclePanel canvas = new CirclePanel(); JButton x; public EnlargeListener(JButton a) { this.x=a; } public void actionPerformed(ActionEvent e) { canvas.x(x); } }import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JPanel; class CirclePanel extends JPanel { private ImageIcon imagei = new ImageIcon("C:/Users/chris/Desktop/x.JPG"); private ImageIcon imageii = new ImageIcon("C:/Users/chris/Desktop/circle.JPG"); Image image = imagei.getImage(); Image imagee = imageii.getImage(); public void o(JButton x) { x.setIcon(new ImageIcon(imagee)); } public void x(JButton x) { // TODO Auto-generated method stub x.setIcon(new ImageIcon(image)); } }