- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码在这里需要一点帮助。我遵循了创建键盘事件的过程,如果我向下按箭头键则向下移动我的椭圆形,如果我向上按箭头键则向上移动等等。但是,我一直无法这样做。我可以从这里的代码中改进什么吗?还计划在两个按钮中实现它,即此处的右翻译 和左翻译 按钮。
提前致谢! 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);
}
}
最佳答案
让我们从...开始
@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);
}
}
这是封装的基本概念
接下来,我强烈建议使用 key bindings API在 KeyListener
之上,因为它将解决与焦点相关的问题。
例如……
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);
}
});
}
}
关于java - 椭圆形移动程序。椭圆不动,有什么我可以在这里炼化的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53732564/
我有一个 RelativeLayout,我从 drawable 设置了背景。我能够将 RelativeLayout 的背景更改为另一个RadioButton 被选中。但是当它发生变化时我该如何给它一个
我正在尝试在 Google 的 Play 报亭应用中复制此动画: http://i.imgur.com/UuX1PRO.webm 我的布局看起来像这样: ... more
我一直在评估 Airflow 。我有一个用例,我有一个每小时运行一次的工作流,以获得每小时的数据聚合。另一个每天运行以获得相同的每日聚合。是否可以创建一个组合工作流,其中仅当所有小时聚合在过去一天都成
我有下一个结构: Activity 1: Activity 2: Form to add new item to the recycler View. RecyclerView
我只是想知道 JavaFx 中是否有任何简单的动 Canvas 局方法,例如 VBox 和 HBox。我希望我的应用程序在指定时间后更改 VBox 的背景颜色。但我意识到没有任何类似于 FillTra
我正在使用 Angular 4 动画在按钮上测试一个简单的淡入/淡出动画。我遇到的问题是,因为我使用的是 bool 值,所以没有任何东西被触发。从开发工具来看,它看起来像一个 .ng-animatin
有没有人在 SublimeREPL 中使用 irb 交换 pry 有任何运气?我很接近,我想。我没有收到错误,但是当我输入命令时也没有收到响应。每次我点击返回时,它的行为就像缓冲区被重置一样。 我正在
今天要向小伙伴们介绍的是一个能够快速地把数据制作成可视化、交互页面的 Python 框架:Streamlit,分分钟让你的数据动起来! 犹记得我在做机器学习和数据分析方面的毕设时,
简而言之,我想缩放 View - 就像 Android Market 一样,当您单击“更多”按钮时,例如在“描述”上。 我发现,Android Market 具有以下结构的布局: > 64d
我似乎无法让它工作。 我正在尝试每天发送一个给定的文件,其名称类似于“file_{{ds_nodash}}.csv”。 问题是我似乎无法将此名称添加为文件名,因为它似乎无法使用。在电子邮件的正文或主题
当您调整窗口大小时, float 的 div 将按预期换行到下一行。但我真的很希望这种布局变化是动画化的。 编辑:顺便说一句,找到一个不依赖于 JQuery 的解决方案会很好。如果需要,我不介意编写自
我有一个复杂的数据处理管道,目前在单台机器上用 Python 实现。 管道是围绕处理属于一系列实现文档、页面、单词等的自定义类的对象而构建的。该管道中的大多数操作都是令人尴尬地并行的——它们处理单个文
我是一名优秀的程序员,十分优秀!