简体   繁体   English

切换按钮字幕游戏

[英]Switch button caption game

I am trying to make a java GUI program "game". 我正在尝试制作一个Java GUI程序“游戏”。

There are five buttons, each with a character as the button caption. 有五个按钮,每个按钮都有一个字符作为按钮标题。 When a button is clicked, the caption of that button is exchanged with the right hand neighbor. 单击一个按钮时,该按钮的标题将与右侧邻居交换。 If the far right button is clicked, then the far left button has that caption, so they both switch (it wraps around). 如果单击最右边的按钮,则最左边的按钮具有该标题,因此它们都可以切换(环绕)。

The goal is to have them arranged alphabetically, thus ending the game. 目标是按字母顺序排列它们,从而结束游戏。

I can't think of an intuitive way to have the characters switching without having to make five buttons. 我想不出不用五个按钮就能切换字符的直观方法。

String str = "abcde"; // DEBUG ARGUMENT STRING
setCaptions(str);

Method that takes the string, creates a char array out of them, and creates buttons... 该方法获取字符串,从中创建一个char数组,然后创建按钮...

void setCaptions(String string){
    char[] charArray = string.toCharArray();
    ArrayList<Character> arrList = new ArrayList<Character>();

    for (int x=0; x < charArray.length; x++) {
        String str = Character.toString(charArray[x]);
        btn = new JButton(str);
        btn.setFont(myFont);
        pane.add(btn, "LR");
        btn.addActionListener(new SwitchAction());
        arrList.add(str.charAt(0));
    }


    // check the order...
    System.out.print(arrList);
    if (arrList.get(0) < arrList.get(1) 
            && arrList.get(1) < arrList.get(2) 
            && arrList.get(2) < arrList.get(3) 
            && arrList.get(3) < arrList.get(4)) {
        lbl.setText("SOLVED");
    }
}

ActionListener to switch the captions, which I can't figure out... ActionListener来切换字幕,我不知道...

public class SwitchAction implements ActionListener {

    public void actionPerformed(ActionEvent evt) {
        String a = btn.getText();

        System.out.println(evt.getActionCommand() + " pressed"); // debug

        // something goes here...
    }
}

You should have an array or ArrayList of JButton, ArrayList<JButton> and put your buttons into this list. 您应该具有JButton的数组或ArrayList, ArrayList<JButton>并将按钮放入此列表。

Your ActionListener will need a reference to the original class so it can get a hold of the ArrayList. 您的ActionListener将需要对原始类的引用,以便可以获取ArrayList。 It can then iterate through the array list find out which button was pressed, which is its neighbor, and do its swap. 然后,它可以遍历数组列表,找出哪个按钮被按下,哪个按钮被按下,并进行交换。 So pass that reference in via a constructor parameter, and then in the actionPerformed method, call a getList() or similar "getter" method to get the ArrayList and iterate through it. 因此,通过构造函数参数传递该引用,然后在actionPerformed方法中,调用getList()或类似的“ getter”方法以获取ArrayList并对其进行迭代。

ie,

public class MyListener implements ActionListener {
  private OriginalGui gui;

  public MyListener(OriginalGui gui) {
    this.gui = gui;
  }

  public void actionPerformed(ActionEvent e) {
    JButton pressedButton = (JButton) e.getSource();
    ArrayList<JButton> buttonList = gui.getButtonList();

    // ... iterate through list and find button.
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM