gpt4 book ai didi

Java输入错误

转载 作者:行者123 更新时间:2023-12-01 15:19:00 24 4
gpt4 key购买 nike

好吧,我放弃了。我担任 C++ 程序员已有几年,但我尝试学习 Java,因为它是一种流行语言。在我学习的过程中,我学到了很多东西,但最终我开始尝试并尝试使用输入系统,这样当我单击这个红色菱形多边形时,它就会变成绿色,但经过几天令人沮丧的日子后......什么也没有。我仍然只有一颗红钻。它可能很小,但我就是找不到它

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Vici extends Applet
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Space castle;

public Vici()
{
castle = new Space();
castle.addMouseListener(new SpaceInput());
}



public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;


int width = getSize().width;
int height = getSize().height;

g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);


castle.paint(g2d);


}

class SpaceInput implements MouseListener
{

public void mouseEntered(MouseEvent m) { }
public void mouseExited(MouseEvent m) { }
public void mouseReleased(MouseEvent m)
{
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}
public void mouseClicked(MouseEvent m)
{
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}
public void mousePressed(MouseEvent m)
{
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}
}

}








import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Space extends Canvas
{
private Polygon poly;
private Color c;
private int[] polyX = { 0, 24, 0, -24 };
private int[] polyY = { 24, 0, -24, 0 };

public void init()
{
poly = new Polygon( polyX, polyY, polyX.length);
c = Color.red;
}

Space()
{
init();
}

void setColor(Color c)
{
this.c = c;
}

public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;

AffineTransform identity = new AffineTransform();

g2d.setTransform(identity);

g2d.translate(100, 100);

g2d.setColor(c);
g2d.fill(poly);
}

public void update( Graphics g )
{
paint( g );
}
}

最佳答案

我摆脱了无关的“SpaceInput”类,并将鼠标监听器添加到小程序(而不是“城堡”)。一切顺利:)

public class Vici extends Applet implements MouseListener
{

private static final long serialVersionUID = 1L;
private Space castle;

public Vici()
{
castle = new Space();
// castle.addMouseListener(this);
addMouseListener (this);
}



public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;


int width = getSize().width;
int height = getSize().height;

g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);


castle.paint(g2d);


}

public void mouseEntered(MouseEvent m) {
System.out.println ("mouse entered...");
}
public void mouseExited(MouseEvent m) {
System.out.println ("mouse mouseExited...");
}
public void mouseReleased(MouseEvent m)
{
System.out.println ("mouse mouseReleased...");
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}
public void mouseClicked(MouseEvent m)
{
System.out.println ("mouse mouseClicked...");
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}

public void mousePressed(MouseEvent m)
{
System.out.println ("mouse mousePressed...");
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}

}

关于Java输入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11238432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com