我如果陈述不起作用,我无法找出原因? “ if(y ==“ 0”)” [重复]

问题描述 投票:0回答:1

这是我的代码,我的方法应该返回true,我尝试使用jtextarea获得2行。“ if(y ==” 0“)”对我来说很好我事件尝试charat,但无法正常工作。

任何人都知道如何解决它?谢谢。


    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JTextPane txt = new JTextPane();
        txt.setEditable(false);
        txt.setText("\r\n0");
        txt.setBounds(10, 11, 414, 57);
        frame.getContentPane().add(txt);

        JButton btn = new JButton("New button");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                txt.setText(str(txt.getText()));
            }
        });
        btn.setBounds(161, 114, 89, 23);
        frame.getContentPane().add(btn);
    }
    public String str(String o){

        String[] nt=o.split("\n");
        String x=nt[0],y=nt[1];


            if (y == "0")
            {   
            o="true";

            }
            else 
            {
                System.out.println(y);
                o="false"+y+"*";
        }
        return o;
        }
java if-statement jtextarea jtextpane applicationwindow
1个回答
0
投票

尝试一下

if (y.equals("0"))

这是我们比较字符串值的方式

© www.soinside.com 2019 - 2024. All rights reserved.