为什么我的if / else语句总是跳到else

时间:2015-05-31 10:11:56

标签: java swing if-statement string-comparison

我试过以我能想到的每一种方式阅读这一点,并且对于我的生活,我无法弄清楚为什么actionlistener中的if / else语句永远不会将if语句评估为true。该程序旨在打开一个包含3个面板的JFrame。其中2个有8个随机按钮,上面有字母。当点击一个按钮时,应该说它是元音还是辅音。但是我的程序总是将按钮评估为辅音。我在哪里看不到程序中信息的逻辑流程?

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;


public class JVowelConsenant extends JFrame {

private JPanel contentPane;
ArrayList list = new ArrayList(0);



/**
 * Launch the application.
 */
public static void main(String[] args) {

            try {
                JVowelConsenant frame = new JVowelConsenant();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


/**
 * Create the frame.
 */
public JVowelConsenant() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 585, 371);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

    JPanel panel = new JPanel();
    contentPane.add(panel);
    panel.setLayout(new GridLayout(2, 2, 0, 0));

    JPanel panel_1 = new JPanel();
    contentPane.add(panel_1);
    panel_1.setLayout(new GridLayout(2, 2, 0, 0));

    JPanel panel_2 = new JPanel();
    contentPane.add(panel_2);
    panel_2.setLayout(new GridLayout(2, 2, 0, 0));

    String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    JButton[] letters = new JButton[26];
    for (int i 0; i<8; i++) {       
        random_Letters();
    }



    for(int i=0; i<26; ++i) {
        letters[i] = new JButton();
        letters[i].setText(alphabet[i]);


        letters[i].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) { 
                    if(arg0.getSource().equals("A")||arg0.getSource().equals("E")|| arg0.getSource().equals("I")||arg0.getSource().equals("O")||arg0.getSource().equals("U")){
                        JLabel letter_identity = new JLabel("This letter is a Vowel");
                        panel_2.removeAll();
                        panel_2.add(letter_identity);
                        panel_2.revalidate();
                    }
                    else{
                        JLabel letter_identity = new JLabel("This letter is a Consenant");
                        panel_2.removeAll();
                        panel_2.add(letter_identity);
                        panel_2.revalidate();
                    }

            }
        });
    }


    for(int i = 0; i<4;++i){
    panel.add(letters[(int) list.get(i)]);
    panel_1.add(letters[(int) list.get(i+4)]);
    }

}

void random_Letters(){
    Random random = new Random();
    int random_1 = random.nextInt(26);
    int check_point =exclusion(random_1);
    while(check_point == 0){
        random_1= random.nextInt(26);
        check_point = exclusion(random_1);
    }
}

int exclusion(int x){
    int marker = 0;
    if(!list.contains(x)){
        list.add(x);
        marker = 1;
    }
    return marker;


}


}

2 个答案:

答案 0 :(得分:4)

arg0.getSource()将返回对触发事件的对象的引用,在本例中为JButton

相反,您应该可以使用ActionEvent#getActionCommand代替。

String cmd = agr0.getActionCommmand();
//...

如果您使用的是Java 7+,您可能会发现使用switch声明更容易...

switch (cmd) {
    case "A":
    case "E":
    case "I":
    case "O":
    case "U":
        // Vowel
        break;
    default:
        // Consenant
        break;
}

有关详细信息,请参阅How to Write an Action Listeners

答案 1 :(得分:0)

arg0.getSource()返回永远不等于A,E,I,O或U的事件对象非常简单,因此控件将转到else。

尝试使用     的System.out.println(arg0.getSource())

查看它返回的内容