我的代码需要一点帮助。我遵循了创建键盘事件的过程,如果按下箭头键,则将椭圆形向下移动;如果按下箭头键,则向上移动椭圆形,依此类推。但是,我还没有做到。有什么我可以在这里的代码中完善的吗?还计划在此处通过两个按钮实现右移和左移按钮。
先谢谢您! Here is the example UI that I've made so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class page309_4 extends JFrame { //implements ActionListener
private JButton btnLeftMvmt, btnRightMvmt;
int oval_x = 150;
int oval_y = 150;
public page309_4(){
setTitle("Oval Mover");
setSize(600, 150);
setLayout(new BorderLayout());
JPanel panel1, panel2;
panel1 = new JPanel();
panel2 = new JPanel();
panel1.setSize(500,300);
panel1.add(new MyPanel());
panel1.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
switch (keycode) {
case KeyEvent.VK_UP: oval_y += 10; break;
case KeyEvent.VK_DOWN: oval_y -= 10; break;
case KeyEvent.VK_LEFT: oval_x -= 10; break;
case KeyEvent.VK_RIGHT: oval_x += 10; break;
}
}
});
panel1.setBackground(Color.YELLOW);
btnLeftMvmt = new JButton("Left Translation");
btnRightMvmt = new JButton("Right Translation");
btnLeftMvmt.addActionListener(bleft -> {
});
btnRightMvmt.addActionListener(bright -> {
});
panel2.add(btnLeftMvmt);
panel2.add(btnRightMvmt);
add(panel1);
add(panel2, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
class MyPanel extends JPanel {
public MyPanel() {
setPreferredSize(new Dimension(600, 150));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
int oval_x = (getWidth() - 150) / 2;
int oval_y = (getHeight() - 150) / 2;
g.fillOval(oval_x, oval_y, 150, 150);
}
}
public static void main (String[] args) {
// TODO Auto-generated method stub
page309_4 f = new page309_4();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:3)
让我们开始...
@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
int oval_x = (getWidth() - 150) / 2;
int oval_y = (getHeight() - 150) / 2;
g.fillOval(oval_x, oval_y, 150, 150);
}
}
x / y位置是在方法中定义的,因此无论您做什么都不会改变。
已经说过,MyPanel
应该直接负责管理x / y位置,这意味着应该在其中定义oval_x
和oval_y
变量。然后,您应该定义可以更改这些值的功能,例如...
class MyPanel extends JPanel {
private int oval_x = 150;
private int oval_y = 150;
public MyPanel() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 150);
}
public void moveVerticallyBy(int delta) {
oval_y += delta;
repaint();
}
public void moveHorizontallyBy(int delta) {
oval_x += delta;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
g.fillOval(oval_x, oval_y, 150, 150);
}
}
这是封装的基本概念
接下来,我强烈建议在KeyListener
上使用key bindings API,因为它将解决与焦点相关的问题。
例如...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Page309_4 extends JFrame { //implements ActionListener
private JButton btnLeftMvmt, btnRightMvmt;
private MyPanel myPanel;
public Page309_4() {
setTitle("Oval Mover");
setSize(600, 150);
setLayout(new BorderLayout());
JPanel panel1, panel2;
panel2 = new JPanel();
myPanel = new MyPanel();
InputMap im = myPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = myPanel.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Pressed.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Pressed.down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Pressed.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Pressed.right");
am.put("Pressed.up", new VerticalMovementAction(-10, myPanel));
am.put("Pressed.down", new VerticalMovementAction(10, myPanel));
am.put("Pressed.left", new HorizontalMovementAction(-10, myPanel));
am.put("Pressed.right", new HorizontalMovementAction(10, myPanel));
btnLeftMvmt = new JButton("Left Translation");
btnRightMvmt = new JButton("Right Translation");
btnLeftMvmt.addActionListener(bleft -> {
});
btnRightMvmt.addActionListener(bright -> {
});
panel2.add(btnLeftMvmt);
panel2.add(btnRightMvmt);
add(myPanel);
add(panel2, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public class VerticalMovementAction extends AbstractAction {
private int delta;
private MyPanel myPanel;
public VerticalMovementAction(int delta, MyPanel myPanel) {
this.delta = delta;
this.myPanel = myPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
myPanel.moveVerticallyBy(delta);
}
}
public class HorizontalMovementAction extends AbstractAction {
private int delta;
private MyPanel myPanel;
public HorizontalMovementAction(int delta, MyPanel myPanel) {
this.delta = delta;
this.myPanel = myPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
myPanel.moveHorizontallyBy(delta);
}
}
class MyPanel extends JPanel {
private int oval_x = (600 - 150) / 2;
private int oval_y = (300 - 150) / 2;
public MyPanel() {
setBackground(Color.YELLOW);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 300);
}
public void moveVerticallyBy(int delta) {
oval_y += delta;
repaint();
}
public void moveHorizontallyBy(int delta) {
oval_x += delta;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
g.fillOval(oval_x, oval_y, 150, 150);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Page309_4 f = new Page309_4();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
答案 1 :(得分:0)
您不会在按键后重新粉刷panel1,所以您永远看不到更改。要解决此问题,请在keyPressed()处理程序的末尾添加panel1.repaint()。