2016-11-15 75 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 –