Java 执行同一任务的多个jbutton
如何编程多个Java 执行同一任务的多个jbutton,java,eclipse,jbutton,Java,Eclipse,Jbutton,如何编程多个JButton对象来执行相同的任务 我正在编写一个游戏,在游戏板上使用25个按钮。每个按钮都会生成一个随机数并根据该数字更改图标,从而完成完全相同的操作 这是我的密码: Random RG1 = new Random(); level_1_random_block = (RG1.nextInt(6)); final JButton btnNewButton = new JButton(""); btnNewButton.addMouseListene
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
语句。如果根据随机数变化的只是图像路径和分数值,为什么不把它们放入数组或类似的东西中,并通过索引访问它们呢
实际上,如果以后需要不同的行为,可以编写一个类来封装这些字段(现在是image和score),并使用该类实例的数组/向量。这样就很容易扩展。如果根据随机数变化的只是图像路径和分数值,为什么不将它们放入数组或类似的东西中,并通过索引访问它们呢
实际上,如果以后需要不同的行为,可以编写一个类来封装这些字段(现在是image和score),并使用该类实例的数组/向量。这样就很容易扩展。我们可以通过重写
actionPerformed
方法来实现ActionListener
子类,然后创建它的实例,将相同的实例添加到用例的每个jButton
对象中
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// your action to be performed
}
}
注意:MouseListener
是Swing中的低级AWT事件侦听器,其中ActionListener
是更高的标签,最好使用。但是,最好使用它本身就是一个动作监听器
,它不仅提供动作事件
处理,而且还集中处理动作事件触发组件的状态,例如工具栏按钮
,菜单项
,常用按钮
,以及文本字段
查看教程了解更多详细信息。通过重写
actionPerformed
方法来实现ActionListener
子类,然后创建它的实例,将相同的实例添加到用例的每个jButton
对象中
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// your action to be performed
}
}
注意:MouseListener
是Swing中的低级AWT事件侦听器,其中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));
不要添加鼠标侦听器。只需在类中实现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));
一些提示:
- 如果
- 使用
而不是ActionListener
MouseListener
- 在侦听器内部,使用
ActionEvent\getSource()
- 只创建一个
,并将其添加到所有按钮中ActionListener
- 如果
- 使用
而不是ActionListener
MouseListener
- 在侦听器内部,使用
ActionEvent\getSource()
- 只创建一个
,并将其添加到所有按钮中ActionListener
希望有帮助:)创建一个新类,该类将包含有关您的操作的元数据:
class Action{
String image;
int score;
}
为所有操作定义实例,并将它们放在一些数据结构中,如数组或列表,并用适当的索引标记它们。
然后您可以通过ID访问它们。创建新类,该类将包含有关您的操作的元数据:
class Action{
String image;
int score;
}
为所有操作定义实例,并将它们放在一些数据结构中,如数组或列表,并用适当的索引标记它们。
然后您可以通过ID访问它们。我唯一的问题是按钮需要更改其图标,但每个按钮都需要更改图标(新的图像图标(“C:\\Users\\Liam\\Desktop\\BOMB GAME\\Oredmond.png”)代码>特定于它正在更改的按钮。我还能用这个吗?@Hoberfinkle是的!使用
ActionEvent#getSource()
和cast:)Yes。您可以创建实现ActionListener
的分离类。您可以使用for循环在按钮上添加ActionListener
。但不幸的是,您必须将逻辑与if
语句放在单独的类中。@Hoberfinkle我更新了我的答案。不太准确,但它应该可以帮助你如何做。这是完美的。非常感谢大家。我唯一的问题是按钮需要更改其图标,但每个btnNewButton.setIcon(新图像图标(“C:\\Users\\Liam\\Desktop\\BOMB GAME\\oreDiamond.png”)代码>特定于它正在更改的按钮。我还能用这个吗?@Hoberfinkle是的!使用ActionEvent#getSource()
和cast:)Yes。您可以创建实现ActionListener
的分离类。您可以使用for循环在按钮上添加ActionListener
。但不幸的是,您必须将该逻辑与if
语句放在单独的类中。@Hoberfinkle