public void go(Person man, JButton direction) { Rectangle manRect = man.getBounds(); int x = manRect.x; int y = manRect.y; if (direction == above) { y -= 50; } else if (direction == below) { y += 50; } else if (direction == left) { x -= 50; } else if (direction == right) { x += 50; } manRect.setLocation(x, y); boolean move = true; for (int i = 0; i < 10; i++) { Rectangle personRect = people[i].getBounds(); if ((manRect.intersects(personRect) && (man.number != i))) { move = false; } } Rectangle directionRect = direction.getBounds(); if (manRect.intersects(directionRect)) { move = false; } if (move) { man.setLocation(x, y); } }
时间: 2024-02-15 07:27:57 浏览: 29
这是一个 Java 方法,它接受两个参数:一个类型为 Person 的 man 和一个类型为 JButton 的 direction。该方法通过检查 direction 的方向来确定 man 的行动方向。如果 direction 为 above,则 man 向上移动 50 个像素;如果 direction 为 below,则 man 向下移动 50 个像素;如果 direction 为 left,则 man 向左移动 50 个像素;如果 direction 为 right,则 man 向右移动 50 个像素。
在移动 man 前,该方法会获取 man 的位置和大小,并根据方向计算新的位置。然后,该方法会检查是否有其他人物或者 direction 按钮在新位置上。如果有,则 move 变量会被设置为 false,表示 man 不能移动到该位置。如果没有碰撞,则 man 会被移动到新位置。
相关问题
package Shixun4_8; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; class FrameWithKeyEvent extends JFrame implements KeyListener { JButton b[]=new JButton[3]; int x,y; FrameWithKeyEvent(){ setSize(300,300); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); for(int i=0;i<=2;i++) { b[i]=new JButton(" "+i); b[i].addKeyListener(this); add(b[i]); } validate(); } public void keyPressed(KeyEvent e) { JButton button=(JButton)e.getSource(); x=button.getBounds().x; y=button.getBounds().y; if(e.getKeyCode()==KeyEvent.VK_UP) { y=y-2; if(y<=0)y=300; button.setLocation(x,y); } else if(e.getKeyCode()== KeyEvent.VK_DOWN) { y=y+2; if(y>=300)y=0; button.setLocation(x,y); } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { x=x-2; if(x<=0)x=300; button.setLocation(x,y); } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { x=x+2; if(x>=300)x=0; button.setLocation(x,y); } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} } public class Shixun4_8 { public static void main(String[] args) { new FrameWithKeyEvent(); } }逐行代码解释
首先,这是一个Java程序,包含一个名为Shixun4_8的public class。
第1-2行导入了javax.swing和java.awt包。
第4-23行定义了一个名为FrameWithKeyEvent的类,它扩展了JFrame并实现了KeyListener接口。该类包含三个按钮和一个构造函数。
在构造函数中,首先调用setSize()方法来设置窗口的大小为300x300像素。接着,调用setVisible()方法使窗口可见,调用setDefaultCloseOperation()方法以便在关闭窗口时退出程序,调用setLayout()方法来设置按钮的布局方式为FlowLayout。然后,使用for循环创建三个按钮,并将每个按钮添加到窗口中。最后,调用validate()方法以确保窗口的内容被正确布局。
在FrameWithKeyEvent类中,实现了三个方法:keyPressed()、keyReleased()和keyTyped(),这些方法都是从KeyListener接口继承而来的。
在keyPressed()方法中,首先使用getSource()方法获取触发事件的组件,然后获取该组件的位置坐标x和y。根据按下的键的不同,分别对x和y进行加减操作,以实现按钮的移动。如果移动到了边缘,就将位置坐标重置为相反的边缘位置。最后,使用setLocation()方法将按钮移动到新的位置。
在keyReleased()和keyTyped()方法中,由于我们不需要响应这两个事件,因此这两个方法的实现为空。
最后,在main()方法中,创建了一个FrameWithKeyEvent对象,以启动程序。
JButton addMemberButton = new JButton("Add Member"); addMemberButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int selectedRow = clubTable.getSelectedRow(); if (selectedRow != -1) { int clubId = (int) clubTable.getValueAt(selectedRow, 0); showAddMemberDialog(clubId); } } });
这段代码创建了一个 JButton 对象 addMemberButton,其文本为 "Add Member"。接着,它使用 addActionListener 方法添加了一个 ActionListener,当用户点击该按钮时,会执行该 ActionListener 中的 actionPerformed 方法。在 actionPerformed 方法中,首先获取表格 clubTable 中当前选中的行 selectedRow,如果 selectedRow 不为 -1,说明用户选中了某个俱乐部,则从表格中获取该俱乐部的 ID(位于第一列,索引为 0),然后调用 showAddMemberDialog 方法打开一个对话框,让用户输入新成员的信息。这段代码可能是用于创建一个包含了 "Add Member" 按钮的用户界面的一部分,该按钮可以让用户向选中的俱乐部添加新成员。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)