2016-11-24 3 views
0

2つのJボタンのアイコンを1つずつ選択して変更したい場合は、アイコンを入れ替えてください。キャンディーのようなものが押しつぶされたり、飾られています。 これを達成するためにアクションリスナーを使用したいのですが、どうすればいいですか?2 jbuttonのスワップアイコン

これは私のプログラムのGUIです:

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); 
    } 

私はあなたのロジックは少し明快さを必要とするので、

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

問題は、どのボタンが2番目に押されたのか分からないためにスワップする方法がわからないということです。 – john

答えて

0

ようにそれをやってみました。 5×5のグリッドを作成しています。そこにボタンを配置します。このような例

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次元グリッド内のその行が直ちに左ボタンを有する場合にのみ、条件に基づいて起こり得る。

更新:アクションは2つのボタンのみをクリックした後に発生することになっている

場合は、あなたが実際にボタンの数のトラックがクリックされたときにカウント== 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)でその上でアクションを実行できることを知っています。しかし、どのボタンが2番目に押されたかを知る方法はわかりません。 – john

+0

ありがとう!これは動作します – john