我创建了这个获取鼠标坐标的程序,现在我想添加一个mouseClicked,当按下鼠标时,它会在某些坐标处创建一个矩形。但它不起作用。 这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class GameSetup extends JPanel implements MouseMotionListener {
public static JFrame njf = new JFrame("Test");
public static int x = 0, y = 0;
public static boolean c = false;
public static void main(String[] args) {
GameSetup gs = new GameSetup();
gs.addMouseMotionListener(gs);
njf.add(gs);
njf.setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
g.setColor(Color.GREEN);
g.drawRect(150, 75, 200, 100);
g.setColor(Color.ORANGE);
g.drawString("Play", 239, 123);
if (x > 150 && y > 75 && x < 350 && y < 175){
g.drawRect(150, 75, 200, 100);
}
if(x > 150 && y > 75 && x < 350 && y < 175 && c){
g.fillRect(10 , 10 ,100 ,100);
}
}
public GameSetup() {
super();
setSize(500, 500);
njf.setSize(500,500);
njf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
njf.setResizable(false);
njf.setLocationRelativeTo(null);
}
@Override
public void mouseDragged(MouseEvent arg0) {
}
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
if (x > 0 && y > 0) repaint(); else repaint();
}
public void mouseClicked(MouseEvent ea){
c = true;
}
}
我该如何使这项工作? 感谢
答案 0 :(得分:1)
你不能@Override
超过mouseClicked(...)
吗?那是因为你的类没有实现正确的接口:MouseListener。同样实现此接口,将对象也添加为MouseListener,您将可以访问MouseListener事件。还可以考虑使用侦听器方法调用repaint()
调用,以便GUI在必要时重新绘制自己。
我自己的偏好不是让我的视图类也实现我的listenener接口,所以如果这是我的程序,我会创建一个扩展MouseAdapter
的内部类,并将其用作MouseListener和MouseMotionListener。
其他问题:
setBackground
。而是在构造函数中调用它。例如没有JLabel ......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class GameSetup2 extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static final Color BG = Color.BLACK;
private static final Color PASSIVE_COLOR = Color.GREEN;
private static final Color ACTIVE_COLOR = Color.ORANGE;
private static final String PLAY = "Play";
private static final int PLAY_X = 239;
private static final int PLAY_Y = 123;
private Rectangle rectangle = new Rectangle(150, 75, 200, 100);
private Color rectColor = PASSIVE_COLOR;
private Color playColor = PASSIVE_COLOR;
public GameSetup2() {
setBackground(BG);
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
// TODO Auto-generated constructor stub
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(playColor);
g2.drawString(PLAY, PLAY_X, PLAY_Y);
g2.setColor(rectColor);
g2.draw(rectangle);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class MyMouse extends MouseAdapter {
@Override
public void mouseMoved(MouseEvent e) {
if (rectangle.contains(e.getPoint())) {
rectColor = ACTIVE_COLOR;
} else {
rectColor = PASSIVE_COLOR;
}
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
if (rectangle.contains(e.getPoint())) {
playColor = ACTIVE_COLOR;
}
repaint();
}
public void mouseReleased(MouseEvent e) {
playColor = PASSIVE_COLOR;
repaint();
};
}
private static void createAndShowGui() {
GameSetup2 mainPanel = new GameSetup2();
JFrame frame = new JFrame("GameSetup2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
答案 1 :(得分:1)
与Hovercraft Full Of Eels所说的一样,我重构了一下。注意:
addMouseListener
)repaint()
调用添加到mouseClicked c
更改为clicked
,并将其在true和false之间切换,以便您可以看到它发生变化试试这个:
public class Game {
public static void main(String[] args) {
final JFrame frame = new JFrame("Test");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(new GamePanel());
frame.setVisible(true);
}
@SuppressWarnings("serial")
private static class GamePanel extends JPanel {
private int x = 0;
private int y = 0;
private boolean clicked = false;
public GamePanel() {
super();
setSize(500, 500);
setBackground(Color.BLACK);
addMouseListener(new MouseListenerImpl());
addMouseMotionListener(new MouseMotionListenerImpl());
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.drawRect(150, 75, 200, 100);
g.setColor(Color.ORANGE);
g.drawString("Play", 239, 123);
if (x > 150 && y > 75 && x < 350 && y < 175) {
g.drawRect(150, 75, 200, 100);
if (clicked) {
g.fillRect(10, 10, 100, 100);
}
}
}
private class MouseListenerImpl extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
clicked = !clicked;
repaint();
}
}
private class MouseMotionListenerImpl extends MouseMotionAdapter {
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
}
}
}