简体   繁体   English

Java addWindowListener语法错误

[英]Java addWindowListener syntax error

I have been searching google and this website all morning, but I don't really know what I am doing wrong. 我整个上午都在搜索google和该网站,但我真的不知道自己在做什么错。 I have created a class (Wallboard) and I am trying to call it, but when compiling it keeps giving errors on the "addWindowListener". 我已经创建了一个类(Wallboard),并且正在尝试调用它,但是在编译时,它始终在“ addWindowListener”上给出错误。

This is the error I get: 这是我得到的错误:

error: constructor Wallboard in class Wallboard cannot be applied to given types;
addWindowListener(new Wallboard(this));
                  ^
  required: no arguments
  found: Wallboard
  reason: actual and formal argument lists differ in length

And this is the code: 这是代码:

public class Wallboard
extends JFrame
implements Runnable
{
JLabel WbText;
Thread t = null;
String[] textLine = new String[4];

public Wallboard()
{
setUndecorated(true);
setExtendedState(6);
getContentPane().setBackground(Color.BLACK);
getContentPane().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(16, 16, 2), new Point(0, 0), null));

setVisible(true);
this.WbText = new JLabel("0123456789012345", 0);
this.WbText.setFont(new Font("Monospaced", 1, getWidth() * 18 / 160));
this.WbText.setHorizontalTextPosition(0);
this.WbText.setVerticalTextPosition(0);
add(this.WbText);
addWindowListener(new Wallboard(this));

this.t = new Thread(this);
this.t.start();
}

Could anyone help me out? 有人可以帮我吗? What am I doing wrong? 我究竟做错了什么?

Many thanks for your input. 非常感谢您的投入。

Seems like your constructor has no arguments: 似乎您的构造函数没有参数:

public Wallboard() {
    setUndecorated(true);
    setExtendedState(6);
    ...

And you then try to call it using this asn argument: 然后尝试使用this asn参数调用它:

addWindowListener(new Wallboard(this));

There should be something else that implements WindowListener instead of new Wallboard(this) inside addWindowsListener() . 应该有其他实现WindowListener东西,而不是addWindowsListener()内部的new Wallboard(this) addWindowsListener() Try this out instead: 尝试以下方法:

addWindowListener(new WindowListener() {});

You are creating a new instance of Wallboard in the constructor of Wallboard. 您正在Wallboard的构造函数中创建Wallboard的新实例。 Even if it would compile, it will cause an infinite loop. 即使可以编译,也会导致无限循环。 Instead, call addWindowListener with the current object. 而是使用当前对象调用addWindowListener。 I think what you want to do is this: 我认为您要执行的操作是:

public class Wallboard
extends JFrame
implements Runnable, WindowListener
{

     public Wallboard() 
     {
          ...
          addWindowListener(this);
          ...
     }

     public void windowOpened(WindowEvent e) {
     }
     ...
}

You have to implement the windowOpened and all the other methods in the WindowListener interface as well. 您还必须在WindowListener接口中实现windowOpened和所有其他方法。

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

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