[ad_1]
Why is the && operator not working in the if statement within (ActionEvent e) in this Java Program?
Hi. I am creating a Java program intended for displaying each of the forms of the Polish adjective “szczęśliwy” in a dialog box using JOptionPane. The problem is that && operator is not working as expected in (ActionEvent e). For instance, I have entered the following code
if (e.getSource() == radioButton[0] && (e.getSource() ==
radioButton1) && (e.getSource() == radioButton[9])) {
JOptionPane.showMessageDialog(null, “szczęśliwy”);
in order to display a JOptionPane message with the form “szczęśliwy”, which is the singular nominative animate masculine form of the Polish adjective, under the circumstances that the three corresponding JRadioButtons are checked when running the program. The corresponding Polish words are “liczba pojedyncza”(singular), “mianownik”(nominative) and “rodzaj męskoosobowy”(animate masculine). However, when I run the program and check the three corresponding JRadioButtons, no dialog box will appear, just like in the first image.
When I run the program and check only one JRadioButton listed below the first instance, a dialog box with the corresponding form of the Polish adjective will appear. All the other conditional statements have been created without the && operator and will work just fine, just like in the second image.
I wonder why the && operator is not working in this particular program. I would appreciate it if you could provide any solution to this problem. Thank you!
For reference purpose, the following image is the declension table for all the forms of the Polish adjective “szczęśliwy”.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class MyFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1 L;
ButtonGroup buttonGroup[];
JPanel panel[];
JRadioButton radioButton[];
MyFrame() {
buttonGroup = new ButtonGroup[3];
panel = new JPanel[3];
for (int i = 0; i < 3; i++) {
panel[i] = new JPanel();
}
radioButton = new JRadioButton[15];
radioButton[0] = new JRadioButton("liczba pojedyncza");
radioButton[1] = new JRadioButton("liczba mnoga");
for (int i = 0; i < 3; i++) {
buttonGroup[i] = new ButtonGroup();
}
for (int i = 0; i < 2; i++) {
buttonGroup[0].add(radioButton[i]);
panel[0].add(radioButton[i]);
}
radioButton[2] = new JRadioButton("mianownik");
radioButton[3] = new JRadioButton("dopełniacz");
radioButton[4] = new JRadioButton("celownik");
radioButton[5] = new JRadioButton("biernik");
radioButton[6] = new JRadioButton("narzędnik");
radioButton[7] = new JRadioButton("miejscownik");
radioButton[8] = new JRadioButton("wołacz");
for (int i = 2; i < 9; i++) {
buttonGroup[1].add(radioButton[i]);
panel[1].add(radioButton[i]);
}
radioButton[9] = new JRadioButton("rodzaj męskoosobowy");
radioButton[10] = new JRadioButton("rodzaj męskorzeczowy");
radioButton[11] = new JRadioButton("rodzaj żeński");
radioButton[12] = new JRadioButton("rodzaj nijaki");
radioButton[13] = new JRadioButton("rodzaj niemęskoosobowy");
for (int i = 9; i < 14; i++) {
buttonGroup[2].add(radioButton[i]);
panel[2].add(radioButton[i]);
}
for (int i = 0; i < 14; i++) {
radioButton[i].addActionListener(this);
}
for (int i = 0; i < 3; i++) {
this.add(panel[i]);
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(0, 1));
this.setSize(520, 520);
this.setTitle("odmiana przymiotnika 'szczęśliwy'");
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == radioButton[0] && (e.getSource() == radioButton[2]) && (e.getSource() == radioButton[9])) {
JOptionPane.showMessageDialog(null, "szczęśliwy");
} else if (e.getSource() == radioButton[3]) {
JOptionPane.showMessageDialog(null, "szczęśliwego");
} else if (e.getSource() == radioButton[4]) {
JOptionPane.showMessageDialog(null, "szczęśliwemu");
} else if (e.getSource() == radioButton[5]) {
JOptionPane.showMessageDialog(null, "szczęśliwego");
} else if (e.getSource() == radioButton[6]) {
JOptionPane.showMessageDialog(null, "szczęśliwym");
} else if (e.getSource() == radioButton[7]) {
JOptionPane.showMessageDialog(null, "szczęśliwym");
} else if (e.getSource() == radioButton[8]) {
JOptionPane.showMessageDialog(null, "szczęśliwy");
}
}
}
[ad_2]