我知道在stackoverflow中经常会问这个问题。我知道有一个关于LayoutManagers的oracle页面,但我不明白如何使用它?有人能用我的代码帮助我吗?我试图用2个方块做一个试图互相捕捉的游戏。它尚未完成。一旦我解决了图形问题,我将完成它。我需要一个JPanel来做吗?提前谢谢!
JFrame类
package catchmev2;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
/**
*
* @author MertKarakas
*/
public class CatchMeV2 implements ActionListener{
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
frame.setSize(width, height);
frame.setTitle("CatchMe.V2");
frame.getContentPane().setLayout(new FlowLayout());
//Adding square 1
RedSquare r = new RedSquare();
frame.getContentPane().add(r);
r.setFocusable(true);
r.requestFocusInWindow();
//Adding square 2
BlueSquare b = new BlueSquare();
frame.getContentPane().add(b);
b.setFocusable(true);
b.requestFocusInWindow();
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
第一个方格
package catchmev2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author MertKarakas
*/
public class RedSquare extends JPanel implements ActionListener, KeyListener {
int x = 20; int y = 20;
int velX = 0; int velY = 0;
Timer tm = new Timer(5, this);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
public RedSquare(){
addKeyListener(this);
tm.start();
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(x, y, 25, 25);
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
x = x + velX;
y = y + velY;
if (x < 0) {
velX = 0;
x = 0;
}
if (x > width - 50) {
velX = 0;
x = width - 50;
}
if (y < 0) {
velY = 0;
y = 0;
}
if (y > height - 40) {
velY = 0;
y = height - 40;
}
repaint();
}
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT) {
velX = -4;
velY = 0;
System.out.println("sex1");
}
if (code == KeyEvent.VK_UP) {
velX = 0;
velY = -4;
System.out.println("sex2");
}
if (code == KeyEvent.VK_RIGHT) {
velX = 4;
velY = 0;
System.out.println("sex3");
}
if (code == KeyEvent.VK_DOWN) {
velX = 0;
velY = 4;
System.out.println("sex4");
}
}
@Override
public void keyReleased(KeyEvent e) {
velX = 0;
velY = 0;
}
}
第二方格
package catchmev2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author MertKarakas
*/
public class BlueSquare extends JPanel implements ActionListener, KeyListener {
int x = 700;
int y = 600;
int velX = 0;
int velY = 0;
Timer tm = new Timer(5, this);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
public BlueSquare() {
addKeyListener(this);
tm.start();
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(x, y, 25, 25);
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
x = x + velX;
y = y + velY;
if (x < 0) {
velX = 0;
x = 0;
}
if (x > width - 50) {
velX = 0;
x = width - 50;
}
if (y < 0) {
velY = 0;
y = 0;
}
if (y > height - 40) {
velY = 0;
y = height - 40;
}
repaint();
}
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_A) {
velX = -4;
velY = 0;
System.out.println("sex1");
}
if (code == KeyEvent.VK_W) {
velX = 0;
velY = -4;
System.out.println("sex2");
}
if (code == KeyEvent.VK_D) {
velX = 4;
velY = 0;
System.out.println("sex3");
}
if (code == KeyEvent.VK_S) {
velX = 0;
velY = 4;
System.out.println("sex4");
}
}
@Override
public void keyReleased(KeyEvent e) {
velX = 0;
velY = 0;
}
}