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

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

記錄在 JButton 數(shù)組中選擇了哪個數(shù)組

記錄在 JButton 數(shù)組中選擇了哪個數(shù)組

喵喵時光機(jī) 2021-08-04 10:31:04
我的麻煩來自試圖記錄正在選擇的數(shù)組。例如,如果我有一個包含 5 個元素的數(shù)組,則記錄選擇了第三個元素。放在上下文中,我有 2 個不同的數(shù)組,每個數(shù)組有 10 個元素;一個數(shù)組由整數(shù)(稱為money)組成,另一個是JButton 數(shù)組。public class Game extends JFrame {JPanel cases = new JPanel(new GridLayout(2, 5)); //Section containing game casesJButton[] caseButton = new JButton[10];                                   // Declaration of theString gameCase[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};  //    case buttonsint[] money = {1, 2, 5, 10, 100, 1000, 5000, 10000, 20000, 30000}; //money values}public void StartGame() {    Shuffle(money); //Shuffle the money    //Initialising case buttons    for (int i = 0; i < caseButton.length; i++) {        caseButton[i] = new JButton(gameCase[i]);        cases.add(caseButton[i]);        caseButton[i].setPreferredSize(new Dimension(100, 100));        caseButton[i].setFont(new Font("Dialog", Font.BOLD, 35));        caseButton[i].setForeground(new Color(255, 215, 0));        caseButton[i].setActionCommand(gameCase[i]);    }Money 數(shù)組在程序開始時被打亂,以允許每次運(yùn)行時使用不同的順序。我想知道的是選擇其中一個jbuttons時,如何如何錄制選擇哪個數(shù)組元素?所以我可以用相應(yīng)的錢數(shù)組做一些事情。(例如,選擇了第 7 個 JButton,然后我想將 JButton 的文本更改為第 7 個貨幣數(shù)組中保存的數(shù)字。)將不勝感激。這將在下周到期,還有很多工作要做。
查看完整描述

2 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個贊

我建議創(chuàng)建一個自定義按鈕類,用于保存money數(shù)組中相應(yīng)值的索引。例如:


public class MyButton extends JButton {

    private int moneyIndex;


    public MyButton(String text, int moneyIndex){

        super(text);

        this.moneyIndex = monexIndex;

    }


    public int getMoneyIndex(){

        return moneyIndex;

    }

}

然后,您可以使用與之前相同的方式創(chuàng)建一個按鈕,但將貨幣索引傳遞給它:


for (int i = 0; i < caseButton.length; i++) {

    // I suspect you want the moneyIndex to match the index of the button

    caseButton[i] = new MyButton("?", i);


    cases.add(caseButton[i]);


    // These can be moved to the custom button class if all MyButtons have these customizations

    caseButton[i].setPreferredSize(new Dimension(100, 100));

    caseButton[i].setFont(new Font("Dialog", Font.BOLD, 35));

    caseButton[i].setForeground(new Color(255, 215, 0));


    // Set this class as the action listener

    caseButton[i].setActionListener(this);

}

然后,在你的動作監(jiān)聽器中(我假設(shè)你的主類已經(jīng)擴(kuò)展了ActionListener),你可以訪問 moneyIndex 變量:


public void actionPerformed(ActionEvent e){

    // Get the source of the click as a MyButton

    MyButton source = (MyButton) e.getSource();


    // Get the moneyIndex of the source button

    int moneyIndex = source.getMoneyIndex();


    // Update the button's text according to the moneyIndex

    source.setText(Integer.toString(money[moneyIndex]));

}

這種方法的優(yōu)點(diǎn)是索引由按鈕存儲,因此您不需要搜索所有按鈕來檢查哪個按鈕被按下。隨著您擁有的按鈕數(shù)量的增加,這一點(diǎn)變得更加重要,但無論大小,都需要考慮這一點(diǎn)。


此外,當(dāng)這個項(xiàng)目變得更復(fù)雜時,這種方法會讓你的生活更輕松,因?yàn)槊總€按鈕都可以存儲特定于它的信息,而無需一堆數(shù)組或操作命令。


查看完整回答
反對 回復(fù) 2021-08-04
?
蠱毒傳說

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個贊

作為自定義按鈕類解決方案的替代方案,您可以將按鈕保留在地圖中,及其索引和操作處理程序上,根據(jù)源獲取索引:


Map<JButton, Integer> caseButtons = new HashMap<>(10);

for(int i=0; i<10; i++) {

    JButton button = ...

    // all the other setup goes here

    caseButtons.put(button, i);

}

...


public void actionPerformed(ActionEvent e){

    // Get the source of the click as a MyButton

    JButton source = (JButton) e.getSource();

    int index = caseButtons.get(source);

    ...

}


查看完整回答
反對 回復(fù) 2021-08-04
  • 2 回答
  • 0 關(guān)注
  • 139 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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