2016-11-15 80 views
1

因此,此應用程序觸發一個JFrame,該按鈕具有排列在5×5網格中的25個按鈕。它應該是一個類似糖果粉碎的遊戲,如果你能夠在一行或一列中獲得三個同樣顏色的按鈕,就可以獲勝。當我選擇一個按鈕時,邊框顏色變爲藍色。有沒有一種方法可以將邊框顏色更改爲紅色?我不知道如何改變當前選中的按鈕的邊框,而不使用每個按鈕的名稱,而且我被要求不要這樣做。這些按鈕是使用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

當我選擇一個按鈕,邊框顏色變爲藍色。

您不能選擇一個按鈕。我認爲你的意思是當鼠標懸停在按鈕上時。

有沒有辦法將邊框顏色更改爲紅色?我不知道如何改變對不使用名稱爲每個按鈕當前選擇的按鈕的邊框,

所以監聽鼠標進入和退出組件,您需要使用MouseListener和落實mouseEntered(...)mouseExited()方法。

然後在監聽器代碼,你可以使用:

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

當您使用此方法,您可以創建一個由所有按鈕共享的單一的MouseListener。

+0

我會嘗試getSource()方法,它可能正是我所期待的。通過選擇,我的意思是點擊。 –

+0

它的工作!萬分感謝! –

+0

我希望我可以對此投票,但沒有足夠的聲望xD –