第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將參數(shù)傳遞給動作監(jiān)聽器

將參數(shù)傳遞給動作監(jiān)聽器

達令說 2022-07-06 17:10:36
背景我正在嘗試編寫一個程序,其中用戶輸入格式公式A?B:C?(X-1):D?(Y-3):(Z*3)然后代碼將引導(dǎo)用戶通過一系列是/否問題來確定在某些條件下將返回什么。問題我已經(jīng)編寫了一個findTrue代碼,它將提取這種公式的真實部分。如果用戶輸入的部分中有更多問號,我希望我yesListener的操作問另一個問題。true如果不是,它將返回答案。        ActionListener yesListener = new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                String formula = textArea.getText();                int i = StringUtils.countMatches(formula, "?");               if  (i>1) {                    String newFormula = "";                    newFormula = findTrue(formula);                    questionLabel.setText(askQuestion(newFormula));                }               else {questionLabel.setText("The formula will return" + findTrue(formula));}            }};我第一次運行代碼時它工作正常,但第二次它從原始輸入再次運行 getText()。所以,我認為最好的方法是如果我可以將字符串傳遞到actionPerformed而不是在內(nèi)部評估它。但是,我對 java 還是很陌生,我正在努力了解如何實現(xiàn)這一點。
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經(jīng)驗 獲得超4個贊

據(jù)我了解,您ActionListener是在某個類的范圍內(nèi)創(chuàng)建的。在不查看類的整個代碼的情況下,很難提出重構(gòu)代碼的最佳方法。


但是為了實現(xiàn)您的目標,您可以將 的原始值保存getText()在類實例字段中,然后在偵聽器的每次新調(diào)用中更新它:


public class Main {


    private String formula;



    ActionListener yesListener = new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

            if (formula == null) {

                formula = textArea.getText();

            }

            int i = StringUtils.countMatches(formula, "?");

            if (i > 1) {

                formula = findTrue(formula);

                questionLabel.setText(askQuestion(formula));

            } else {

                questionLabel.setText("The formula will return" + findTrue(formula));

            }

        }


    };


查看完整回答
反對 回復(fù) 2022-07-06
  • 1 回答
  • 0 關(guān)注
  • 99 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號