I am using Jbuttons for a game called who wants to be a zillionaire in an assignment. I am using the buttons for selecting an answer. Here is the code for some of the buttons. Code:
}
});
Answer2.setBounds(220, 105, 188, 25);
panel.add(Answer2);
Answer1 = new JButton("A");
Answer1.setBackground(Color.YELLOW);
Answer1.setHorizontalAlignment(SwingConstants.LEFT);
Answer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Answer1.setBackground(Color.BLUE);
Answer1.setForeground(Color.WHITE);
}
});
Answer1.setBounds(20, 105, 188, 25);
panel.add(Answer1);
Answer4 = new JButton("D");
Answer4.setBackground(Color.YELLOW);
Answer4.setHorizontalAlignment(SwingConstants.LEFT);
Answer4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Answer4.setBackground(Color.BLUE);
Answer4.setForeground(Color.WHITE);
}
});
At the moment multiple buttons can be selected and I wish to prevent this. It would be helpful if you could show me an example of code that could be used to do this.