java中的setfocusable方法
时间: 2023-04-25 21:04:07 浏览: 92
setFocusable方法是Java中的一个方法,用于设置组件是否可以获得焦点。如果设置为true,则组件可以获得焦点,否则不能。该方法适用于所有继承自java.awt.Component类的组件,如JButton、JTextField等。通过调用setFocusable方法,可以控制组件的焦点状态,从而实现一些交互效果。例如,当用户点击某个按钮时,可以将焦点设置到另一个组件上,以便用户继续操作。
相关问题
Java中setfocusable用法
setFocusable() 是Java中的一个方法,用于设置组件是否可以获取焦点。当组件被设置为可获取焦点时,用户可以使用键盘或鼠标来选择该组件并与之交互。
该方法的语法如下:
```
public void setFocusable(boolean focusable)
```
其中,参数 focusable 表示是否可以获取焦点,如果为 true,则该组件可以获取焦点,否则不能。
例如,以下代码将一个文本框设置为可以获取焦点:
```
JTextField textField = new JTextField();
textField.setFocusable(true);
```
需要注意的是,对于某些组件(如 JLabel),默认是无法获取焦点的,因此如果要使其能够获取焦点,必须先设置其为可获取焦点。
在Java中做一个弹球游戏
以下是一个简单的弹球游戏的 Java 代码,实现了基本的移动、碰撞检测和反弹功能。你可以根据需要进行修改和扩展,添加更多的游戏元素和功能。
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BallGame extends JPanel implements ActionListener, KeyListener {
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
private static final int BALL_SIZE = 20;
private static final int PADDLE_WIDTH = 80;
private static final int PADDLE_HEIGHT = 10;
private static final int PADDLE_SPEED = 5;
private static final int TIMER_DELAY = 10;
private static final Color BALL_COLOR = Color.RED;
private static final Color PADDLE_COLOR = Color.BLUE;
private static final Color BACKGROUND_COLOR = Color.WHITE;
private static final int MAX_ANGLE = 150;
private static final int MIN_ANGLE = 30;
private static final int MAX_SPEED = 5;
private static final int MIN_SPEED = 1;
private int ballX, ballY, ballSpeedX, ballSpeedY;
private int paddleX, paddleY;
private boolean leftPressed, rightPressed;
private Timer timer;
public BallGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(BACKGROUND_COLOR);
addKeyListener(this);
setFocusable(true);
ballX = WIDTH / 2 - BALL_SIZE / 2;
ballY = HEIGHT / 2 - BALL_SIZE / 2;
ballSpeedX = 1;
ballSpeedY = 1;
paddleX = WIDTH / 2 - PADDLE_WIDTH / 2;
paddleY = HEIGHT - PADDLE_HEIGHT - 10;
timer = new Timer(TIMER_DELAY, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(BALL_COLOR);
g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
g.setColor(PADDLE_COLOR);
g.fillRect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
}
public void actionPerformed(ActionEvent e) {
moveBall();
movePaddle();
checkCollisions();
repaint();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
}
public void keyTyped(KeyEvent e) {
}
private void moveBall() {
ballX += ballSpeedX;
ballY += ballSpeedY;
if (ballX < 0 || ballX > WIDTH - BALL_SIZE) {
ballSpeedX = -ballSpeedX;
}
if (ballY < 0 || ballY > HEIGHT - BALL_SIZE) {
ballSpeedY = -ballSpeedY;
}
}
private void movePaddle() {
if (leftPressed && paddleX > 0) {
paddleX -= PADDLE_SPEED;
}
if (rightPressed && paddleX < WIDTH - PADDLE_WIDTH) {
paddleX += PADDLE_SPEED;
}
}
private void checkCollisions() {
if (ballY + BALL_SIZE >= paddleY && ballX + BALL_SIZE >= paddleX && ballX <= paddleX + PADDLE_WIDTH) {
ballSpeedY = -ballSpeedY;
int angle = (int) (MAX_ANGLE - (MAX_ANGLE - MIN_ANGLE) * ((double) (ballX + BALL_SIZE / 2 - paddleX) / PADDLE_WIDTH));
ballSpeedX = (int) (ballSpeedY * Math.tan(Math.toRadians(angle)));
if (ballSpeedX > MAX_SPEED) {
ballSpeedX = MAX_SPEED;
} else if (ballSpeedX < -MAX_SPEED) {
ballSpeedX = -MAX_SPEED;
}
if (ballSpeedX > 0 && ballSpeedX < MIN_SPEED) {
ballSpeedX = MIN_SPEED;
} else if (ballSpeedX < 0 && ballSpeedX > -MIN_SPEED) {
ballSpeedX = -MIN_SPEED;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Ball Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new BallGame());
frame.pack();
frame.setVisible(true);
}
}
```
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)