2016-11-24 54 views
0

我想通过选择一个然后下一个交换图标来更改2个Jbuttons的图标。像糖果粉碎或珠光宝气。 我想使用动作监听器来完成这个任务,我该如何去做呢?交换图标2 jbutton

这是我的程序的GUI:做像这样

public class ButtonSwapHandler implements ActionListener{ 

JButton _button1; 
JButton _button2; 
Model _model; 
UI _ui; 

public ButtonSwapHandler(UI u,Model m, JButton b1, JButton b2){ 
    _model=m; 
    _button1=b1; 
    _button2=b2; 
    _ui =u; 
} 

@Override 
public void actionPerformed(ActionEvent e) { 

//this line should give me the position of the first button i press 
    int i = _ui.getTiles().indexOf(e.getSource()); 

//this is the part where i dont know how to keep going 
//i want to know where is the 2nd button that i clicked 
    int j = _ui.getTiles().indexOf(e. 

// this is the method i wrote to make the swap 
// it just the Collections.swap(tile,positionOfButton1,postionOfButton2) 
    _model.swap(ui._tile,i,j); 
} 
+0

让你的尝试重绘()或重新验证()掉后? – Definity

+0

问题是我不知道如何交换,因为我不知道哪个按钮被按下第二个 – john

回答

0

你的逻辑需要一点清晰度

public class UI implements Runnable { 

private JFrame _frame; 
private Model _model; 
private ArrayList<JButton> _tiles; 

public void run() { 
    _model = new Model(); 
    _frame = new JFrame(); 
    _frame.setLayout(new GridLayout(5,5)); 

    _tiles = new ArrayList<JButton>(); 
    for (int i=0; i<25; i++) { 
     JButton tile = new JButton(); 
     tile.setBackground(Color.white); 
    //this just pick out random icon file from a folder 
     tile.setIcon(_model.randomIcon()); 
     tile.addActionListener(new ButtonBorderHandler(_model,tile)); 
    //this is the actionlistener that i want to implement the swap on 
     tile.addActionListener(new ButtonSwapHandler(); 
     _tiles.add(tile); 
    } 

我试过。您正在创建一个5x5网格,您正在填充这些按钮。

例如像这样:

0 1 2 3 4 
5 6 7 8 9 
10 11 12 13 14 
15 16 17 18 19 
20 21 22 23 24 

但是,当你点击一个按钮,你如何确保交换的方向是什么?说,如果用户点击按钮7,应该选择哪个相邻的按钮7进行交换?

所以,你的逻辑缺失的一部分是方向。

例如:

如果交换总是立即左键发生,你可以计算像indexOf(selectedButton)-1交换按钮。同样,只有在该二维网格中的那一行具有即时左键时,交换才可以有条件地发生。

更新:

如果操作应该仅在按下两个按钮后发生的,那么你需要创建一个多类实际跟踪按钮的数量,然后点击交换时计数== 2。

下面是一个变形例:

public class ButtonClicksCounter { 
    static ArrayList<JButton> _buttonsClicked = new ArrayList<JButton>(); 
    public static void addButton(JButton btn) { 
     _buttonsClicked.add(btn); 
    } 

    public static int getButtonClicksCount() { 
     return _buttonsClicked.size(); 
    } 

    public static void clearButtonClicksCount() { 
     _buttonsClicked.clear(); 
    } 

    public ArrayList<JButton> getButtonsClicked() { 
     return _buttonsClicked; 
    } 

} 

public class ButtonSwapHandler implements ActionListener{ 


JButton _button; 
Model _model; 
UI _ui; 

public ButtonSwapHandler(UI u, Model m, JButton b1){ 
    _model=m; 
    _button=b1; 
    _ui =u; 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    //Add the button 
    ButtonClicksCounter.addButton((JButton)e.getSource()); 

    //Check if count==2 
    if(ButtonClicksCounter.getButtonClicksCount()==2) { 
     ArrayList<JButton> buttonsToSwap = ButtonClicksCounter.getButtonsClicked(); 

     //Get positions 
     int i = _ui.getTiles().indexOf(buttonsToSwap[0]); 
     int j = _ui.getTiles().indexOf(buttonsToSwap[1]); 

     //Swap 
     _model.swap(ui._tile,i,j); 

     //Clear selection 
     ButtonClicksCounter.clearButtonClicksCount(); 
    } 

} 
+0

我希望能够水平和垂直交换。用你的按钮7的例子,我知道我可以用indexOf(e.getSource)对它执行操作。但是,我不知道如何找出哪个按钮被按下第二个 – john

+0

谢谢!这工作 – john