简体   繁体   English

Java Applet程序

[英]Java Applet Program

EDIT: I have fixed the problem. 编辑:我已经解决了问题。 Thank you all for your help. 谢谢大家的帮助。

I am pretty new to programming, and have been struggling through an online course. 我对编程非常陌生,并且一直在努力学习在线课程。 I am now working on my final project, which is to: Write a program to count the number of mouse clicks on a button within a frame. 我现在正在完成我的最终项目,该项目是:编写一个程序来计算框架中某个按钮上的鼠标单击次数。 The code I have seems to be WAY off. 我拥有的代码似乎还遥不可及。 Keep in mind that this is an applet. 请记住,这是一个小程序。 Here is the program: 这是程序:

import java.awt.*;
import java.awt.event.*;
import java.awt.MouseAdapter;

public class finalproject1
{
    TextField objTextField1;

    public static void main(String[] args)
    {
        finalproject1 p1 = new finalproject1();
    }

    public finalproject1
    {
        Frame f = new Frame("Mouse Clicks");
        objTextField1 = new TextField("Click the button",200);
        objTextField1.setBounds(220,140,200,40);
        Button button1 = new Button("Click here");
        button1.setBounds(200,200,140,140);
        button1.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent evt)
            {
                if(evt.getClickCount() == 3)
                {
                    objTextField1TextField1.setText("Triple click");
                }
                else if(evt.getClickCount() ==2)
                {   
                    objTextField1.setText("Double click");
                }
            });
        }
        f.add(button1); 
        f.add(objTextField1);
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
        f.setSize(800,800);     
        f.setVisible(true);     
    }
}

First of all, I'd recommend you fix your tabbing to make sure you're looking at scoping correctly. 首先,我建议您修复标签以确保您正在正确地确定作用域。 In addition, there are some other quirks here that could be messing things up -- this really ought not to even compile. 此外,这里还有其他一些怪癖可能会使事情搞砸了-这真的不应该编译。

It looks like you're trying to define a constructor, but you haven't added parentheses. 似乎您正在尝试定义一个构造函数,但尚未添加括号。 This: 这个:

public finalproject1 { ... }

should become this: 应该变成这个:

public finalproject1() { ... }

It's also convention to name classes in camelcase, so FinalProject1 would be a better name. 在驼峰中命名类也是惯例,因此FinalProject1将是一个更好的名称。

Your paren placement is also off in this code: 此代码中的父母位置也处于关闭状态:

button1.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent evt) {
        if(evt.getClickCount() == 3) {
            objTextField1TextField1.setText("Triple click");
        } else if(evt.getClickCount() ==2) {   
            objTextField1.setText("Double click");
        }
    }); // This ");" should be one brace down from where it is.
}

I can't help much more without knowing what you mean by "WAY off." 如果您不了解“关闭”的意思,我无能为力。 Can you elaborate? 你能详细说明吗?

  • parentheses are missing to the constructor : public finalproject1() { ... } 构造函数缺少括号:public finalproject1(){...}
  • ");" “);” at the end of method mouseClicked must be at the end of method addMouseListener. 方法mouseClicked的末尾必须位于方法addMouseListener的末尾。
  • object objTextField1TextField1 is not declared. 未声明对象objTextField1TextField1。 It should be objTextField1 instead. 它应该是objTextField1。
  • class name must start with upper case (Java convention) 类名必须以大写字母开头(Java约定)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM