Java 为什么不是';当我点击这个特定区域时,长numclick不是增加了吗?
为什么这样不行?他说:“我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们。”Java 为什么不是';当我点击这个特定区域时,长numclick不是增加了吗?,java,long-integer,mouselistener,Java,Long Integer,Mouselistener,为什么这样不行?他说:“我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们。” package rBP import java.applet.Applet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.Mous
package rBP
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Main extends Applet implements MouseListener {
public void paint(Graphics g) {
this.setSize(1010, 360);
long numClicks = 0; //<--Long in question
int autoFingerNum = 0;
int faultyButtonNum = 0;
int slickButtonNum = 0;
int coolButtonNum = 0;
int heatSinkNum = 0;
int superButtonNum = 0;
g.setColor(Color.cyan);
g.fillRect(0, 0, 1010, 360);
g.setColor(Color.black);
g.fillOval(47, 47, 205, 205);
g.setColor(Color.red);
g.fillOval(50, 50, 200, 200);
g.setColor(Color.black);
g.setColor(Color.yellow);
g.fillRect(2, 5, 290, 30);
g.fillRect(300, 25, 100, 250);
g.fillRect(410, 25, 100, 250);
g.fillRect(520, 25, 100, 250);
g.fillRect(630, 25, 100, 250);
g.fillRect(740, 25, 100, 250);
g.fillRect(850, 25, 100, 250);
g.setColor(Color.black);
g.drawLine(300, 100, 400, 100);
g.drawLine(410, 100, 510, 100);
g.drawLine(520, 100, 620, 100);
g.drawLine(630, 100, 730, 100);
g.drawLine(740, 100, 840, 100);
g.drawLine(850, 100, 950, 100);
g.drawLine(300, 190, 400, 190);
g.drawLine(410, 190, 510, 190);
g.drawLine(520, 190, 620, 190);
g.drawLine(630, 190, 730, 190);
g.drawLine(740, 190, 840, 190);
g.drawLine(850, 190, 950, 190);
g.fillRect(307, 197, 86, 71);
g.fillRect(417, 197, 86, 71);
g.fillRect(527, 197, 86, 71);
g.fillRect(637, 197, 86, 71);
g.fillRect(747, 197, 86, 71);
g.fillRect(857, 197, 86, 71);
g.setColor(Color.red);
g.fillRect(310, 200, 80, 65);
g.fillRect(420, 200, 80, 65);
g.fillRect(530, 200, 80, 65);
g.fillRect(640, 200, 80, 65);
g.fillRect(750, 200, 80, 65);
g.fillRect(860, 200, 80, 65);
g.setColor(Color.black);
g.setFont(new Font("default", Font.BOLD, 12));
g.drawString("Number of Points: " + numClicks, 5, 25);
g.setFont(new Font("default", Font.BOLD, 20));
g.drawString("DO", 135, 120);
g.drawString("NOT", 127, 150);
g.drawString("PRESS", 115, 180);
g.setFont(new Font("default", Font.BOLD, 16));
g.drawString("AutoFinger", 310, 50);
g.drawString("FaultyButton", 411, 50);
g.drawString("SlickButton", 524, 50);
g.drawString("CoolButton", 635, 50);
g.drawString("HeatSink", 754, 50);
g.drawString("SuperButton", 851, 50);
g.drawString("+1p/5s", 310, 75);
g.drawString("+3p/5s", 434, 75);
g.drawString("+10p/5s", 540, 75);
g.drawString("+40p/5s", 648, 75);
g.drawString("+100p/5s", 756, 75);
g.drawString("+500p/5s", 866, 75);
g.drawString("Buy: 10P", 315, 240);
g.drawString("Buy: 75P", 425, 240);
g.drawString("Buy: 300P", 530, 240);
g.setFont(new Font("default", Font.BOLD, 15));
g.drawString("Buy: 1000P", 642, 240);
g.drawString("Buy: 5000P", 752, 240);
g.setFont(new Font("default", Font.BOLD, 13));
g.drawString("Buy: 10000P", 862, 240);
g.setFont(new Font("default", Font.BOLD, 12));
g.drawString("Number: " + autoFingerNum, 305, 150);
g.drawString("Number: " + faultyButtonNum, 415, 150);
g.drawString("Number: " + slickButtonNum, 525, 150);
g.drawString("Number: " + coolButtonNum, 635, 150);
g.drawString("Number: " + heatSinkNum, 745, 150);
g.drawString("Number: " + superButtonNum, 855, 150);
}
@Override
public void mouseClicked(MouseEvent e) {
if ((e.getX() < 50 && e.getX() > 250) && (e.getY() < 50 && e.getY() > 250)) {
numClicks++; //<-- this doesn't increase, why?
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
`
package-rBP
导入java.applet.applet;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
公共类Main扩展Applet实现MouseListener{
公共空间涂料(图g){
此.setSize(1010360);
长numClicks=0;//250)&&&(e.getY()<50&&e.getY()>250)){
numClicks++;//您的条件相互矛盾:
e.getX() < 50 && e.getX() > 250 //Less than 50 and more than 250 - Impossible
e.getY() < 50 && e.getY() > 250 //Less than 50 and more than 250 - Impossible
e.getX()<50&&e.getX()>250//小于50但大于250-不可能
e、 getY()<50&&e.getY()>250//小于50但大于250-不可能
您的代码甚至无法编译
您的numClicks
被定义为paint
方法中的局部变量
public void paint(Graphics g) {
this.setSize(1010, 360);
long numClicks = 0; //<--Long in question
//...
public void mouseClicked(MouseEvent e) {
if ((e.getX() < 50 && e.getX() > 250) && (e.getY() < 50 && e.getY() > 250)) {
numClicks++; //<-- this doesn't increase, why?
}
}
首先将numClicks
声明为实例变量
public class Main extends Applet implements MouseListener {
private long numClicks = 0;
public void paint(Graphics g) {
然后从paint
public void paint(Graphics g) {
this.setSize(1010, 360);
//long numClicks = 0; //<--Long in question
x/y不能同时小于50和大于250
您可能还想注册小程序
以接收鼠标事件
public void init() {
super.init();
addMouseListener(this);
}
顺便说一句
在执行自定义绘制之前,您应该先调用super.paint
。。然后我会对JApplet
的Applet
背后的原因提出疑问,但那是我
使用工作示例更新
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Main extends Applet implements MouseListener {
private long numClicks;
@Override
public void init() {
super.init();
addMouseListener(this);
}
@Override
public void paint(Graphics g) {
this.setSize(1010, 360);
int autoFingerNum = 0;
int faultyButtonNum = 0;
int slickButtonNum = 0;
int coolButtonNum = 0;
int heatSinkNum = 0;
int superButtonNum = 0;
g.setColor(Color.cyan);
g.fillRect(0, 0, 1010, 360);
g.setColor(Color.black);
g.fillOval(47, 47, 205, 205);
g.setColor(Color.red);
g.fillOval(50, 50, 200, 200);
g.setColor(Color.black);
g.setColor(Color.yellow);
g.fillRect(2, 5, 290, 30);
g.fillRect(300, 25, 100, 250);
g.fillRect(410, 25, 100, 250);
g.fillRect(520, 25, 100, 250);
g.fillRect(630, 25, 100, 250);
g.fillRect(740, 25, 100, 250);
g.fillRect(850, 25, 100, 250);
g.setColor(Color.black);
g.drawLine(300, 100, 400, 100);
g.drawLine(410, 100, 510, 100);
g.drawLine(520, 100, 620, 100);
g.drawLine(630, 100, 730, 100);
g.drawLine(740, 100, 840, 100);
g.drawLine(850, 100, 950, 100);
g.drawLine(300, 190, 400, 190);
g.drawLine(410, 190, 510, 190);
g.drawLine(520, 190, 620, 190);
g.drawLine(630, 190, 730, 190);
g.drawLine(740, 190, 840, 190);
g.drawLine(850, 190, 950, 190);
g.fillRect(307, 197, 86, 71);
g.fillRect(417, 197, 86, 71);
g.fillRect(527, 197, 86, 71);
g.fillRect(637, 197, 86, 71);
g.fillRect(747, 197, 86, 71);
g.fillRect(857, 197, 86, 71);
g.setColor(Color.red);
g.fillRect(310, 200, 80, 65);
g.fillRect(420, 200, 80, 65);
g.fillRect(530, 200, 80, 65);
g.fillRect(640, 200, 80, 65);
g.fillRect(750, 200, 80, 65);
g.fillRect(860, 200, 80, 65);
g.setColor(Color.black);
g.setFont(new Font("default", Font.BOLD, 12));
g.drawString("Number of Points: " + numClicks, 5, 25);
g.setFont(new Font("default", Font.BOLD, 20));
g.drawString("DO", 135, 120);
g.drawString("NOT", 127, 150);
g.drawString("PRESS", 115, 180);
g.setFont(new Font("default", Font.BOLD, 16));
g.drawString("AutoFinger", 310, 50);
g.drawString("FaultyButton", 411, 50);
g.drawString("SlickButton", 524, 50);
g.drawString("CoolButton", 635, 50);
g.drawString("HeatSink", 754, 50);
g.drawString("SuperButton", 851, 50);
g.drawString("+1p/5s", 310, 75);
g.drawString("+3p/5s", 434, 75);
g.drawString("+10p/5s", 540, 75);
g.drawString("+40p/5s", 648, 75);
g.drawString("+100p/5s", 756, 75);
g.drawString("+500p/5s", 866, 75);
g.drawString("Buy: 10P", 315, 240);
g.drawString("Buy: 75P", 425, 240);
g.drawString("Buy: 300P", 530, 240);
g.setFont(new Font("default", Font.BOLD, 15));
g.drawString("Buy: 1000P", 642, 240);
g.drawString("Buy: 5000P", 752, 240);
g.setFont(new Font("default", Font.BOLD, 13));
g.drawString("Buy: 10000P", 862, 240);
g.setFont(new Font("default", Font.BOLD, 12));
g.drawString("Number: " + autoFingerNum, 305, 150);
g.drawString("Number: " + faultyButtonNum, 415, 150);
g.drawString("Number: " + slickButtonNum, 525, 150);
g.drawString("Number: " + coolButtonNum, 635, 150);
g.drawString("Number: " + heatSinkNum, 745, 150);
g.drawString("Number: " + superButtonNum, 855, 150);
g.setColor(Color.RED);
g.drawRect(50, 50, 200, 200);
}
@Override
public void mouseClicked(MouseEvent e) {
if ((e.getX() > 50 && e.getX() < 250) && (e.getY() > 50 && e.getY() < 250)) {
numClicks++;
repaint();
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}
import java.applet.applet;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
公共类Main扩展Applet实现MouseListener{
私人长吻;
@凌驾
公共void init(){
super.init();
addMouseListener(这个);
}
@凌驾
公共空间涂料(图g){
此.setSize(1010360);
int autoFingerNum=0;
int faultyButtonNum=0;
int=nnum=0;
int coolButtonNum=0;
int heatSinkNum=0;
int superButtonNum=0;
g、 setColor(Color.cyan);
g、 fillRect(0,0,1010,360);
g、 设置颜色(颜色为黑色);
g、 椭圆形(47,47,205,205);
g、 setColor(Color.red);
g、 圆形(50,50,200,200);
g、 设置颜色(颜色为黑色);
g、 setColor(颜色为黄色);
g、 fillRect(2,5290,30);
g、 fillRect(300、25、100、250);
g、 fillRect(410,25100250);
g、 fillRect(520,25100250);
g、 fillRect(63025100250);
g、 fillRect(74025100250);
g、 fillRect(850、25、100、250);
g、 设置颜色(颜色为黑色);
g、 抽绳(300100400100);
g、 抽绳(410100510100);
g、 抽绳(520、100、620、100);
g、 抽绳(630100730100);
g、 抽绳(740100840100);
g、 抽绳(850、100、950、100);
g、 抽绳(300190400190);
g、 抽绳(410190510190);
g、 抽绳(520190620190);
g、 抽绳(630190730190);
g、 抽绳(740190840190);
g、 抽绳(850、190、950、190);
g、 fillRect(307197,86,71);
g、 fillRect(417197,86,71);
g、 fillRect(527197,86,71);
g、 fillRect(637197,86,71);
g、 fillRect(747197,86,71);
g、 fillRect(857197,86,71);
g、 setColor(Color.red);
g、 fillRect(310、200、80、65);
g、 fillRect(420、200、80、65);
g、 fillRect(530200,80,65);
g、 fillRect(640200,80,65);
g、 fillRect(750、200、80、65);
g、 fillRect(8602008065);
g、 设置颜色(颜色为黑色);
g、 setFont(新字体(“默认”,Font.BOLD,12));
g、 抽绳(“点数:+numClicks,5,25);
g、 setFont(新字体(“默认”,Font.BOLD,20));
g、 抽绳(“DO”,135、120);
g、 抽绳(“非”,127、150);
g、 抽绳(按115、180);
g、 setFont(新字体(“默认”,Font.BOLD,16));
g、 抽绳(“自动指”,310,50);
g、 抽绳(“FaultyButton”,411,50);
g、 抽绳(“滑扣”,524,50);
g、 拉丝(“酷扣”,635,50);
g、 抽绳(“散热器”,754,50);
g、 抽绳(“超级按钮”,851,50);
g、 抽绳(“+1p/5s”,310,75);
g、 抽绳(“+3p/5s”,434,75);
g、 抽绳(“+10p/5s”,540,75);
g、 抽绳(“+40p/5s”,648,75);
g、 拉丝(“+100p/5s”,756,75);
g、 拉丝(“+500p/5s”,866,75);
g、 抽绳(“买:10便士”,315240);
g、 抽绳(“买:75便士”,425240);
g、 抽绳(“购买:300P”,530240);
g、 setFont(新字体(“默认”,Font.BOLD,15));
g、 抽绳(“买:1000便士”,642240);
g、 抽绳(“购买:5000P”,752240);
g、 setFont(新字体(“默认”,Font.BOLD,13));
g、 抽绳(“买:10000便士”,862240);
g、 setFont(新字体(“默认”,Font.BOLD,12));
g、 抽绳(“编号:”+autoFingerNum,305150);
g、 抽绳(“编号:”+faultyButtonNum,415150);
g、 抽绳(编号:+525150);
g、 抽绳(编号:+coolButtonNum,635150);
g、 抽绳(编号:+heatSinkNum,745150);
g、 抽绳(“编号:”+superButtonNum,855150);
g、 setColor(Color.RED);
g、 drawRect(50,50,200,200);
}
@凌驾
公共无效mouseClicked(MouseEvent e){
如果((e.getX()>50&&e.getX()<250)和(&e.getY()>50&&e.getY()<250)){
numClicks++;
重新油漆();
}
}
@凌驾
公共无效鼠标事件(鼠标事件arg0){
}
@凌驾
public void mouseexitted(MouseEvent arg0){
}
@O
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Main extends Applet implements MouseListener {
private long numClicks;
@Override
public void init() {
super.init();
addMouseListener(this);
}
@Override
public void paint(Graphics g) {
this.setSize(1010, 360);
int autoFingerNum = 0;
int faultyButtonNum = 0;
int slickButtonNum = 0;
int coolButtonNum = 0;
int heatSinkNum = 0;
int superButtonNum = 0;
g.setColor(Color.cyan);
g.fillRect(0, 0, 1010, 360);
g.setColor(Color.black);
g.fillOval(47, 47, 205, 205);
g.setColor(Color.red);
g.fillOval(50, 50, 200, 200);
g.setColor(Color.black);
g.setColor(Color.yellow);
g.fillRect(2, 5, 290, 30);
g.fillRect(300, 25, 100, 250);
g.fillRect(410, 25, 100, 250);
g.fillRect(520, 25, 100, 250);
g.fillRect(630, 25, 100, 250);
g.fillRect(740, 25, 100, 250);
g.fillRect(850, 25, 100, 250);
g.setColor(Color.black);
g.drawLine(300, 100, 400, 100);
g.drawLine(410, 100, 510, 100);
g.drawLine(520, 100, 620, 100);
g.drawLine(630, 100, 730, 100);
g.drawLine(740, 100, 840, 100);
g.drawLine(850, 100, 950, 100);
g.drawLine(300, 190, 400, 190);
g.drawLine(410, 190, 510, 190);
g.drawLine(520, 190, 620, 190);
g.drawLine(630, 190, 730, 190);
g.drawLine(740, 190, 840, 190);
g.drawLine(850, 190, 950, 190);
g.fillRect(307, 197, 86, 71);
g.fillRect(417, 197, 86, 71);
g.fillRect(527, 197, 86, 71);
g.fillRect(637, 197, 86, 71);
g.fillRect(747, 197, 86, 71);
g.fillRect(857, 197, 86, 71);
g.setColor(Color.red);
g.fillRect(310, 200, 80, 65);
g.fillRect(420, 200, 80, 65);
g.fillRect(530, 200, 80, 65);
g.fillRect(640, 200, 80, 65);
g.fillRect(750, 200, 80, 65);
g.fillRect(860, 200, 80, 65);
g.setColor(Color.black);
g.setFont(new Font("default", Font.BOLD, 12));
g.drawString("Number of Points: " + numClicks, 5, 25);
g.setFont(new Font("default", Font.BOLD, 20));
g.drawString("DO", 135, 120);
g.drawString("NOT", 127, 150);
g.drawString("PRESS", 115, 180);
g.setFont(new Font("default", Font.BOLD, 16));
g.drawString("AutoFinger", 310, 50);
g.drawString("FaultyButton", 411, 50);
g.drawString("SlickButton", 524, 50);
g.drawString("CoolButton", 635, 50);
g.drawString("HeatSink", 754, 50);
g.drawString("SuperButton", 851, 50);
g.drawString("+1p/5s", 310, 75);
g.drawString("+3p/5s", 434, 75);
g.drawString("+10p/5s", 540, 75);
g.drawString("+40p/5s", 648, 75);
g.drawString("+100p/5s", 756, 75);
g.drawString("+500p/5s", 866, 75);
g.drawString("Buy: 10P", 315, 240);
g.drawString("Buy: 75P", 425, 240);
g.drawString("Buy: 300P", 530, 240);
g.setFont(new Font("default", Font.BOLD, 15));
g.drawString("Buy: 1000P", 642, 240);
g.drawString("Buy: 5000P", 752, 240);
g.setFont(new Font("default", Font.BOLD, 13));
g.drawString("Buy: 10000P", 862, 240);
g.setFont(new Font("default", Font.BOLD, 12));
g.drawString("Number: " + autoFingerNum, 305, 150);
g.drawString("Number: " + faultyButtonNum, 415, 150);
g.drawString("Number: " + slickButtonNum, 525, 150);
g.drawString("Number: " + coolButtonNum, 635, 150);
g.drawString("Number: " + heatSinkNum, 745, 150);
g.drawString("Number: " + superButtonNum, 855, 150);
g.setColor(Color.RED);
g.drawRect(50, 50, 200, 200);
}
@Override
public void mouseClicked(MouseEvent e) {
if ((e.getX() > 50 && e.getX() < 250) && (e.getY() > 50 && e.getY() < 250)) {
numClicks++;
repaint();
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}
private static long numClicks = 0;