Java输入错误
好吧,我放弃。我已经是C++程序员几年了,但我尝试学习java,因为它是一种流行的语言。在我学习的过程中,我学到了很多东西,但最终我开始尝试使用输入系统,这样当我点击这个红色菱形多边形时,它会变成绿色,但经过几天的沮丧之后。。。娜达。我仍然只有一颗红钻石。它可能很小,但我就是找不到Java输入错误,java,awt,Java,Awt,好吧,我放弃。我已经是C++程序员几年了,但我尝试学习java,因为它是一种流行的语言。在我学习的过程中,我学到了很多东西,但最终我开始尝试使用输入系统,这样当我点击这个红色菱形多边形时,它会变成绿色,但经过几天的沮丧之后。。。娜达。我仍然只有一颗红钻石。它可能很小,但我就是找不到 import java.awt.*; import java.awt.event.*; import java.applet.*; public class Vici extends Applet { /*
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”类,并将鼠标侦听器添加到applet(而不是“castle”)。一切都成功了:) @查兹·贝尔蒂诺- 我认为整个问题在于使用“Canvas”(自Java1.2以来就过时了)与“JPanel”(取代了自J2SE2.0以来结合使用的旧“Panel”和“Canvas”) 下面是另一个示例,其行为与您预期的一样。主要区别在于它使用Swing JPanel而不是AWT画布:
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class HelloAWT extends Applet {
private Diamond diamond;
private static final int height = 350;
private static final int width = 350;
public void init () {
setBackground(Color.green);
resize (width, height);
diamond = new Diamond ();
diamond.init (Color.green);
add(diamond);
}
public void paint(Graphics g)
{
System.out.println ("HelloAWT@paint()...");
String msg = "Hello AWT!";
g.setFont(new Font ("Arial", Font.BOLD, 18));
FontMetrics fm = g.getFontMetrics ();
int x = (getWidth() - fm.stringWidth(msg)) / 2;
int y = (getHeight() / 2) + 50;
g.drawString(msg, x, y);
diamond.repaint ();
}
}
class Diamond extends JPanel implements MouseListener {
private static final int height = 100;
private static final int width = 100;
private Color c = Color.red;
private Color bkgd;
private int[] polyX;
private int[] polyY;
private Polygon poly;
public void init (Color bkgd) {
System.out.println ("Diamond@init(): bkgd=" + bkgd + "...");
setPreferredSize (new Dimension(width, height));
this.bkgd = bkgd;
polyX = new int[] { width/2, width, width/2, 0 };
polyY= new int[] { 0, height/2, height, height/2 };
System.out.println ("polyX=" + polyX[0] + "," + polyX[1] + "," + polyX[2] + "," + polyX[3] + "...");
System.out.println ("polyY=" + polyY[0] + "," + polyY[1] + "," + polyY[2] + "," + polyY[3] + "...");
poly = new Polygon( polyX, polyY, polyX.length);
addMouseListener (this);
}
public void paint(Graphics g)
{
System.out.println ("Diamond()@paint(), bounds=" + getHeight() + "," + getWidth () + "...");
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(bkgd);
g2d.fillRect(0, 0, width, height);
g2d.setColor(c);
g2d.fill(poly);
}
public void mouseEntered(MouseEvent m) {
System.out.println ("mouseEntered()...");
}
public void mouseExited(MouseEvent m) {
System.out.println ("mouseExited()...");
}
public void mouseReleased(MouseEvent m) {
System.out.println ("mouseReleased()...");
}
public void mouseClicked(MouseEvent m) {
System.out.println ("mouseClicked(), current color=" + c + "...");
if (c == Color.red)
c = Color.blue;
else
c = Color.red;
repaint ();
}
public void mousePressed(MouseEvent m) {
System.out.println ("mousePressed()...");
}
}
错误是什么?我们不会检查您的代码并发现错误@Hemant Metalia-问题是鼠标侦听器从未被调用,因此颜色从未改变。这是一个合理的问题……小程序不是学习Java GUI编程的好方法。制作应用程序。第一。不要使用AWT,使用Swing。大多数曾经知道如何使用AWT的人已经忘记了细节。Paulsm4我正在使用eclipse,您的代码在单击时会使其变为绿色,即使单击的不是菱形。你能改变你的代码吗?这样它只会在只点击钻石时改变颜色。如果是这样的话,试试他的代码,但是把
addMouseListener(this)
改成castle。addMouseListener(this)
Gobernador,这和我的问题一样。好的,更近一点,但是点击空间是正方形的,不是菱形。1)鼠标侦听器应用于整个面板(面板的整个矩形),2)您可以编写一个函数,以获取鼠标x/y并确定您是否在面板的特定区域内,如果您需要3)主要问题-为什么没有检测到鼠标单击-已解决。对的
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class HelloAWT extends Applet {
private Diamond diamond;
private static final int height = 350;
private static final int width = 350;
public void init () {
setBackground(Color.green);
resize (width, height);
diamond = new Diamond ();
diamond.init (Color.green);
add(diamond);
}
public void paint(Graphics g)
{
System.out.println ("HelloAWT@paint()...");
String msg = "Hello AWT!";
g.setFont(new Font ("Arial", Font.BOLD, 18));
FontMetrics fm = g.getFontMetrics ();
int x = (getWidth() - fm.stringWidth(msg)) / 2;
int y = (getHeight() / 2) + 50;
g.drawString(msg, x, y);
diamond.repaint ();
}
}
class Diamond extends JPanel implements MouseListener {
private static final int height = 100;
private static final int width = 100;
private Color c = Color.red;
private Color bkgd;
private int[] polyX;
private int[] polyY;
private Polygon poly;
public void init (Color bkgd) {
System.out.println ("Diamond@init(): bkgd=" + bkgd + "...");
setPreferredSize (new Dimension(width, height));
this.bkgd = bkgd;
polyX = new int[] { width/2, width, width/2, 0 };
polyY= new int[] { 0, height/2, height, height/2 };
System.out.println ("polyX=" + polyX[0] + "," + polyX[1] + "," + polyX[2] + "," + polyX[3] + "...");
System.out.println ("polyY=" + polyY[0] + "," + polyY[1] + "," + polyY[2] + "," + polyY[3] + "...");
poly = new Polygon( polyX, polyY, polyX.length);
addMouseListener (this);
}
public void paint(Graphics g)
{
System.out.println ("Diamond()@paint(), bounds=" + getHeight() + "," + getWidth () + "...");
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(bkgd);
g2d.fillRect(0, 0, width, height);
g2d.setColor(c);
g2d.fill(poly);
}
public void mouseEntered(MouseEvent m) {
System.out.println ("mouseEntered()...");
}
public void mouseExited(MouseEvent m) {
System.out.println ("mouseExited()...");
}
public void mouseReleased(MouseEvent m) {
System.out.println ("mouseReleased()...");
}
public void mouseClicked(MouseEvent m) {
System.out.println ("mouseClicked(), current color=" + c + "...");
if (c == Color.red)
c = Color.blue;
else
c = Color.red;
repaint ();
}
public void mousePressed(MouseEvent m) {
System.out.println ("mousePressed()...");
}
}