[英]Multiple JButtons to perform the same task
如何对多个JButton
对象进行编程以执行同一任务?
我正在编写一个游戏,该游戏在游戏板上使用25个按钮。 每个按钮通过生成一个随机数并根据数字更改图标来执行完全相同的操作。
这是我的代码:
Random RG1 = new Random();
level_1_random_block = (RG1.nextInt(6));
final JButton btnNewButton = new JButton("");
btnNewButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
frame2.setVisible(false);
if (level_1_random_block == 0){
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreDiamond.png"));
score += 100;
initialize_score();
}
if (level_1_random_block == 1){
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreGold.png"));
score += 25;
initialize_score();
}
if (level_1_random_block == 2){
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreGold.png"));
score += 25;
initialize_score();
}
if (level_1_random_block == 3){
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreIron.png"));
score += 5;
initialize_score();
}
if (level_1_random_block == 4){
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreIron.png"));
score += 5;
initialize_score();
}
if (level_1_random_block == 5){
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\creeper.png"));
score -= 30;
initialize_score();
}
if (level_1_random_block == 6){
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\creeper.png"));
score -= 30;
initialize_score();
}
btnNewButton.removeMouseListener(this);
level_1_move_on = true;
continue_game();
}
});
btnNewButton.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\grass_side.png"));
btnNewButton.setBounds(0, 0, 87, 87);
frame1.getContentPane().add(btnNewButton);
一切工作正常,但是我想知道是否有一种简单的方法可以对所有按钮进行编程,而无需编写150个不同的if
语句。
通过重写actionPerformed
方法来实现它来创建一个ActionListener
子类,然后创建它的一个实例,并将相同的实例添加到用例的每个jButton
对象中。
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// your action to be performed
}
}
注意: MouseListener
是Swing中的一个低级AWT事件侦听器,其中ActionListener
的标签较高,更可取。 但是,最好使用java.swing.Action本身是一个action listener
,它不仅提供action-event
处理,而且还集中处理动作事件触发组件的状态,例如tool bar buttons
, menu items
, common buttons
和text fields
。
有关更多详细信息,请参见“ 如何使用操作”教程。
一些技巧 :
switch
而不是多个if
ActionListener
而不是MouseListener
ActionEvent#getSource()
访问单击的按钮 ActionListener
并将其添加到所有按钮 希望对您有所帮助:)
不要添加鼠标侦听器。 只需在您的类中实现ActionListener即可:
public SomeClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Do your stuff here
}
}
然后将ActionListener
附加到您的按钮:
btn.addActionListener(this);
或者就像我在单独的类中提到的那样:
public EventHandler implements ActionListener {
int level_1_random_block;
public EventHandler(int level_1_random_block) {
this.level_1_random_block = level_1_random_block;
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
if (level_1_random_block == 0){
source.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreDiamond.png"));
score += 100;
initialize_score();
}
if (level_1_random_block == 1){
source.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreGold.png"));
score += 25;
initialize_score();
}
if (level_1_random_block == 2){
source.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreGold.png"));
score += 25;
initialize_score();
}
if (level_1_random_block == 3){
source.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreIron.png"));
score += 5;
initialize_score();
}
if (level_1_random_block == 4){
source.setIcon(new ImageIcon("C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreIron.png"));
score += 5;
initialize_score();
}
}
}
在按钮上添加此侦听器:
btn.addActionListener(new EventHandler(level_1_random_block));
如果所有随随机数而变化的是图像路径和得分值,为什么不将它们放入数组或类似的东西并按索引访问它们呢?
实际上,如果以后需要其他行为,则可以编写一个类来封装这些字段(现在为图像和得分),并使用该类实例的数组/向量。 这样,将很容易扩展。
制作新类,其中将包含有关您的操作的元数据:
class Action{
String image;
int score;
}
为所有动作定义实例,并将其放入数组或列表的某种数据结构中,并用适当的索引标记它们。 然后,您可以通过ID到达他们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.