- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JPanel 将形状添加到窗口上,然后能够在窗口周围单击并拖动它们。如果我只有一种形状,这就有效;但当我添加更多形状时,单击和拖动非常时髦。它确实拖动但不使用鼠标拖动,它不成比例并且不使用鼠标拖动。
感谢任何帮助。谢谢!
public class SimpleDraw {
public static void main(String[] args) {
JFrame frame = new UMLWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(30, 30, 1000, 700);
frame.getContentPane().setBackground(Color.white);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
// Display the window.
frame.setVisible(true);
}
}
class UMLWindow extends JFrame {
Squares squares = new Squares();
private static final long serialVersionUID = 1L;
public UMLWindow() {
addMenus();
}
public void addMenus() {
getContentPane().add(squares);
JMenuBar menubar = new JMenuBar();
JMenu shapes = new JMenu("Shapes");
JMenuItem rectangleMenuItem = new JMenuItem("New Rectangle");
rectangleMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
squares.addSquare(10, 10, 100, 100);
}
});
shapes.add(rectangleMenuItem);
menubar.add(shapes);
setJMenuBar(menubar);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class Squares extends JPanel {
private static final long serialVersionUID = 1L;
private List<Path2D> squares = new ArrayList<Path2D>();
// private Path2D rect = new Path2D.Double();
int currentIndex;
public void addSquare(int x, int y, int width, int height) {
Path2D rect2 = new Path2D.Double();
rect2.append(new Rectangle(getWidth() / 2 - width / 2, getHeight() / 2
- height / 2, width, height), true);
squares.add(rect2);
// rect = rect2;
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setOpaque(true);
this.setBackground(Color.WHITE);
Graphics2D g2 = (Graphics2D) g;
for (Path2D rect : squares) {
g2.draw(rect);
}
repaint();
}
class MyMouseAdapter extends MouseAdapter {
private boolean pressed = false;
private Point point;
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() != MouseEvent.BUTTON1) {
return;
}
for (int i = 0; i < squares.size(); i++) {
if (squares.get(i) != null
&& squares.get(i).contains(e.getPoint())) {
currentIndex = i;
pressed = true;
this.point = e.getPoint();
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (pressed) {
int deltaX = e.getX() - point.x;
int deltaY = e.getY() - point.y;
squares.get(currentIndex).transform(
AffineTransform.getTranslateInstance(deltaX, deltaY));
point = e.getPoint();
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
pressed = false;
}
}
}
最佳答案
问题很多...
repaint()
,因为这是尝试创建动画循环(您完全无法控制的循环)的糟糕方法。还有就是没必要。 MouseAdapter 应自行驱动所有动画。class Squares extends JPanel {
private static final long serialVersionUID = 1L;
public Squares() {
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
private List<Path2D> squares = new ArrayList<Path2D>();
// private Path2D rect = new Path2D.Double();
int currentIndex;
public void addSquare(int x, int y, int width, int height) {
Path2D rect2 = new Path2D.Double();
rect2.append(new Rectangle(getWidth() / 2 - width / 2, getHeight() / 2
- height / 2, width, height), true);
squares.add(rect2);
repaint(); // !!
// rect = rect2;
// !! MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
// addMouseListener(myMouseAdapter);
// addMouseMotionListener(myMouseAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setOpaque(true);
this.setBackground(Color.WHITE);
Graphics2D g2 = (Graphics2D) g;
for (Path2D rect : squares) {
g2.draw(rect);
}
// !! repaint();
}
关于java - 动态添加多个 MouseListener JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26080417/
我有一个程序,我必须在其中更改对象的状态,我想知道是否有人能够在外部创建 MouseListener 类或监听器类,该类仍然具有与对象交互的代码,允许他们更改字段从原始类中取出,然后将它们交换出来,如
我有一个 JPanel parent 和一个 JPanel child。我在父级和子级中添加了一个 MouseListener。这是代码: public void init(MouseListener
我需要先说明一下,我的导师不允许我们使用 IDE。我们使用文本板。我想单击此标签,然后它从“H”更改为“T”。目前,当我单击标签时什么也不做。我忘记了什么? import javax.swing.*;
我试图在屏幕上出现一个圆圈并跟随鼠标移动。 (最终我将把它变成一个带有光线转换的游戏)我正在使用 MouseMotionListener 并尝试使用 mouseMoved 方法在我的 JPanel 中
我有一个类,它实现 MouseListner。 如果我将此 class 实例设置为 null ,它会从 JPanel 中删除 MouseListener 吗?或者我是否必须使用 removeMouse
所以我尝试使用 Java 图形创建一个视频游戏,并且我需要使用 MouseListener 作为菜单屏幕上的“播放”按钮。当我按下按钮时,只要我按住按钮,它就会重复打印测试文本。我认为这就像编写只允许
我是 Java 新手,希望实现以下目标。 我有各种组合框。对于每个组合框,我都有一个按钮可以清除组合框的内容。清除按钮是从我创建的通用 ClearComboBoxButton 类创建的。作为此 Cle
我有 4 JLabel s。 首次点击时: 我将背景颜色更改为红色并删除了 JLabel的MouseListener我点击过的。 第二次点击时: 我将背景颜色更改为绿色,但是JLabel我之前单击的不
我的游戏中处理射击的 MouseListener 在使用单独的鼠标时没有响应,但在使用笔记本电脑上的内置鼠标时却没有响应。在我的播放器类中,mouseClicked 方法如下所示: public vo
在我目前制作的游戏中,我有三种不同的 mousePressed 方法,一种用于单次射击,一种用于自动射击,一种用于近战攻击。因为自动开火的方法使用了 Swing Timer,所以我可以在其他 mous
我有堆叠条形图,我希望能够在其中选择堆栈中的单个条形图。但是 ChartMouseListener 不会将 ChartMouseEvent 解析为对应的 ChartEntity。这是监听器片段:
对于我正在制作的这款游戏,我真的很想得到一些帮助。我已经尝试了最长的时间,但我却找不到解决方案。目前它的作用不大。基本上,我需要鼠标监听器寻找左键单击并以一种方式改变蛇的方向,另一种方式用于右键单击。
(注意:我知道 MouseAdapter 类的存在,但由于我稍后可能会覆盖所有方法,所以它的优势就消失了吗? ) 我有一个 MainProgram.java 类,我在其中添加了几个组件。他们中的大多数
我有一个 JPanel(网格),嵌套在主 JFrame 中。我正在尝试在网格上设置一个 Mouselistener 并将事件报告给主框架。 App.java (入口点) public class Ap
我有一个 JInternalFrame,我想在双击 JTable 时在其中显示 JOptionPane。我在互联网上查了一下,发现唯一的方法是重写 mousePressed() 方法,这就是我的做法:
我在 java 中使用 swing 创建了一个圆形 strip ,现在我想在鼠标单击 strip 的特定区域(例如 45 度到 135 度中心角之间的区域)时显示一些文本,有人可以帮我吗? impor
有谁知道以下是否是触发 MouseEvent 方面的预期行为?其中一颗似乎在其他地方丢失或被消耗掉。 右键单击JPanel并显示JPopupMenu: MousePressed java.awt.ev
我正在使用 Java 开发一款游戏,并面临以下挑战。 我有 2 个 JPanel,需要以可视方式将形状从一个 JPanel 拖动到另一个。我已经使用 JFrame 中的 GlassPane 完成了这项
我有一个类,它扩展了JPanel并包含一个带有MouseListener的JLabel。我有一个扩展它的子类,我希望它具有相同的 JLabel,但我希望 MouseListener 执行不同的操作。代
我知道在类(class)的开头您可以编写 implements ActionListener 然后您必须创建 public void actionPerformed(ActionEvent e) 才能
我是一名优秀的程序员,十分优秀!