Java-如何使用gettext()

时间:2019-02-18 20:52:51

标签: java

import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class batsmen {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    batsmen window = new batsmen();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public batsmen() {
        initialize();
    }

    /**
     * Initialise the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblClue = new JLabel("Clue 1:");
        lblClue.setBounds(20, 6, 61, 16);
        frame.getContentPane().add(lblClue);

        JLabel lblNewLabel = new JLabel("He is the leading international run scorer of all time");
        lblNewLabel.setBounds(48, 30, 375, 16);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblClue_1 = new JLabel("Clue 2");
        lblClue_1.setBounds(20, 69, 61, 16);
        frame.getContentPane().add(lblClue_1);

        JLabel lblHePlayedFor = new JLabel("He played for India");
        lblHePlayedFor.setBounds(48, 94, 375, 16);
        frame.getContentPane().add(lblHePlayedFor);

        JLabel label = new JLabel("");
        label.setBounds(20, 229, 403, 16);
        frame.getContentPane().add(label);
        frame.setVisible(true);

        textField = new JTextField();


        textField.setBounds(20, 191, 290, 26);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblTakeAGuess = new JLabel("Take a Guess:");
        lblTakeAGuess.setBounds(20, 163, 119, 16);
        frame.getContentPane().add(lblTakeAGuess);

        JButton btnNewButton = new JButton("Enter");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String answer = textField.getText();
                answer = answer.toLowerCase();
                if(answer == "sachin tendulkar") {
                    label.setText("Correct");
                }else if (answer != "sachin tendulkar"){
                    label.setText("Incorrect. The answer was Sachin Tendulkar");
                }
            }
        });
        btnNewButton.setBounds(322, 191, 117, 29);
        frame.getContentPane().add(btnNewButton);


    }

}

即使输入了正确的答案,if语句似乎也不起作用,它显示“错误。答案是Sachin Tendulkar”。我将不胜感激。我已经检查过,并且字符串答案包含正确答案,但是程序输出“ Incorrect!”。这是非常令人困惑和烦人的。再次感谢您能给我的任何帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试

if(answer.equals("sachin tendulkar")) {
    label.setText("Correct");
}

通常最好使用.equals()进行字符串比较。您可以阅读为什么是here