2016-11-15 3 views
1

このアプリケーションは、5つのグリッドで25個のボタンが配置されたJFrameを起動します。キャンディークラッシックのようなゲームでなければならないが、行や列に3つの同様の色のボタンを表示すると勝つ。ボタンを選択すると、境界線の色が青に変わります。境界の色を赤に変える方法はありますか?私は各ボタンの名前を使わずに現在選択されているボタンの境界線をどのように変更するのか分かりません。 forループを使用してボタンが作成されているため、名前はありません。また、コードは以前のプロジェクトのものであり、このコードに適合しているため、このために必要でない、または変更が必要な部分があるかもしれません。すべてを無視して、今は選択したボタンの境界線の色を変更する方法を知りたいだけです。ボタンの名前を付けずに、選択したjbuttonの境界線の色を変更する

これは、ゲームの初期状態の設定を処理するクラスです。

package code.model; 

import java.util.ArrayList; 
import java.util.Random; 

import code.ui.UI; 

public class Model { 

private UI _observer; // initialized by setObserver method 
private Random _rand; // for randomization 
private ArrayList<String> _imageFileNames; // the names of the image files 
private ArrayList<String> _spinnerCurrentValues; // the image files to display in the UI 

public Model() { 
    _rand = new Random(); 

    _imageFileNames = new ArrayList<String>(); 
    _imageFileNames.add("Tile-0.png"); 
    _imageFileNames.add("Tile-1.png"); 
    _imageFileNames.add("Tile-2.png"); 
    _imageFileNames.add("Tile-3.png"); 
    _imageFileNames.add("Tile-4.png"); 
    _imageFileNames.add("Tile-5.png"); 

    _spinnerCurrentValues = new ArrayList<String>(); 
    for(int i=0; i<25; i=i+1) { 
     _spinnerCurrentValues.add(i,null); 
    } 
    System.out.println(_spinnerCurrentValues); 

    spin(); // randomly select which images to display 
} 

public void spin() { 
    for(int i=0; i<25; i=i+1) { 
     _spinnerCurrentValues.set(i, _imageFileNames.get(_rand.nextInt(_imageFileNames.size()))); 
    } 
    stateChanged(); // tell the UI that the model's state has changed 
} 

public boolean jackpot() { 
    for (int i=1; i<_spinnerCurrentValues.size(); i=i+1) { 
     if (! _spinnerCurrentValues.get(i-1).equals(_spinnerCurrentValues.get(i))) { 
      return false; 
     } 
    } 
    return true; // all three spinners are displaying the same image (based on image file name) 
} 

public void addObserver(UI ui) { 
    _observer = ui; 
} 

public void stateChanged() { 
    if (_observer != null) { 
     _observer.setIcon(); // tell the UI to update 
    } 
} 

public String getImageFileName(int i) { 
    return _spinnerCurrentValues.get(i); 
} 

}


このクラスは、ユーザインタフェースを設定します。

package code.ui; 

import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseListener; 
import java.util.ArrayList; 

import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

import code.model.Model; 

public class UI implements Runnable { 

private JFrame _frame; 
private Model _model; 
private JButton _currentButton; 

private ArrayList<JButton> _buttons; 
private JButton _spin; 

@Override public void run() { 
    _frame = new JFrame("Sanchit Batra's Lab 8"); 
    _frame.getContentPane().setLayout(new GridLayout(5, 5, 10, 10)); 

    _buttons = new ArrayList<JButton>(); 
    for (int i=0; i<25; i++) { 
     JButton button = new JButton(); 
     ActionListener x = new EventHandler(_model); 
     button.addActionListener(x); 
     _buttons.add(button); 
     _frame.getContentPane().add(button); 
    } 

    _model = new Model(); // create the model for this UI 
    _model.addObserver(this); 


    // standard JFrame method calls 
    _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    _frame.pack(); 
    _frame.setVisible(true); 
    _model.spin(); 
} 

public void setIcon() { 
    // update the icon on each label 
    for (int i=0; i<25; i=i+1) { 
     _buttons.get(i).setIcon(new ImageIcon("Images/"+_model.getImageFileName(i))); 
    } 

    // make sure JFrame is appropriately sized (needed when _spin text changes) 
    _frame.pack(); 
} 

public JButton getCurrentButton(){ 
    return _currentButton; 
} 

}

このクラスは、イベントを処理します。

package code.ui; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import code.model.Model; 

public class EventHandler implements ActionListener { 

private Model _model; 

public EventHandler(Model m) { 
    _model = m; 
} 

@Override public void actionPerformed(ActionEvent e) { 



} 

}

このクラスのメインメソッドを有します。

package code; 

public class Driver { 

public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new code.ui.UI()); 
} 

}

+2

'ボタンを選択したとき'選択を定義...クリックするとどういう意味ですか? – copeg

+0

正確に重複している可能性が最も高いhttp://stackoverflow.com/questions/6444722/java-jbutton-change-rollover-border-color?rq=1 –

+0

私はそのリンクをチェックしたが、あまり役に立たなかった。 –

答えて

1

Iボタン、青に境界線の色の変更を選択します。

ボタンを選択することはできません。マウスがボタンの上に乗ったときを意味すると思います。

境界線の色を赤に変更する方法はありますか?私はマウス入りのために耳を傾け、あなたがMouseListenerを使用する必要があるコンポーネントを終了し、実装するために、現在、各ボタンの名称を使用せずに選択されたボタンの境界線を変更する方法

を知りませんmouseEntered(...)およびmouseExited()の方法。

は、その後、使用することができ、リスナーのコードに:あなたは、このアプローチを使用する場合

JButton button = (JButton)event.getSource(); 
button.setBorder(...); 

あなたはすべてのボタンによって共有される単一のMouseListenerを作成することができます。

+0

試してみるgetSource()メソッドを使用すると、私が探していたものと正確に一致する可能性があります。そしてselectによって、私はクリックされたことを意味しました。 –

+0

それは働いた!ありがとう、トン! –

+0

私はこれを投票することができますが、評判が十分でないことを望みますxD –

関連する問題