gui还是不会出现

umuewwlo  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(339)

我的任务要求我们编写一个由原始项目文件打开的“runner”文件,只有在项目文件运行时,我的runner文件才不会创建gui,否则运行runner文件本身就会创建gui。
老师反馈说我没有包括我的事件处理代码,但我不确定我在这一点上遗漏了什么。
这是我不干涉的程序:

public static void main(String[] args){
    new Proj05Runner();
  }//end main
}//end class Proj05

这是我的跑步者代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Proj05Runner extends JFrame{
    public static void main(String[] args){
        GUI gui = new GUI();
    }
}

class GUI extends JFrame{
    public GUI(){//constructor
    //Create a new Frame object
    JFrame displayWindow = new JFrame();
    displayWindow.setSize(300,200);
    displayWindow.setTitle("Brendan");
    //Button button1 = new Button("Press");
    //Instantiate two Listener objects that will process
    // Window events
    WProc1 winProcCmd1 = new WProc1(displayWindow);
    WProc2 winProcCmd2 = new WProc2();

    //Register the Listener objects for notification of
    // Window events. This object is the Event Source.
    displayWindow.addWindowListener(winProcCmd1);
    displayWindow.addWindowListener(winProcCmd2);

    //windowActivated and windowOpened test messages
    // are produced here
    displayWindow.setVisible(true);
    }
}

class WProc1 implements WindowListener{
  //used to save a reference to the Frame object
  JFrame displayWindowRef;

  WProc1(JFrame windowIn){//constructor
    // save ref to JFrame object
    this.displayWindowRef = windowIn;
  }//end constructor

  public void windowClosed(WindowEvent e){
    System.out.println("WProc1 windowClosed test msg");
  }//end windowClosed()

  public void windowIconified(WindowEvent e){
    System.out.println("WProc1 windowIconified test msg");
  }//end windowIconified()

  public void windowOpened(WindowEvent e){
    System.out.println("WProc1 windowOpened test msg");
  }//end windowOpened()

  public void windowClosing(WindowEvent e){
    System.out.println("WProc1 windowClosing test msg");
    displayWindowRef.dispose();//generate WindowClosed
  }//end windowClosing()

  public void windowDeiconified(WindowEvent e){
    System.out.println(
                      "WProc1 windowDeiconified test msg");
  }//end windowDeiconified()

  public void windowActivated(WindowEvent e){
    System.out.println("WProc1 windowActivated test msg");
  }//end windowActivated()

  public void windowDeactivated(WindowEvent e){
    System.out.println(
                     "WProc1 windowDeactivated test msg");
  }//end windowDeactivated()
}//end class WProc1
//=======================================================//

//This and the previous class can be used to instantiate
// Listener objects. Note that this class extends an
// Adapter class that can be used to avoid the
// requirement to define all of the methods of the
// actual Listener class named WindowListener. This class
// overrides only two of the methods declared in the
// interface.  It displays a message whenever one of the
// methods is called.
class WProc2 extends WindowAdapter{

  public void windowIconified(WindowEvent e){
    System.out.println(
              "********WProc2 windowIconified test msg");
  }//end windowIconified()

  public void windowDeiconified(WindowEvent e){
    System.out.println(
            "********WProc2 windowDeiconified test msg");
  }//end windowDeiconified()

}//end class WProc2

提前谢谢你的帮助

lrpiutwd

lrpiutwd1#

proj05runner有一个静态main方法,没有构造函数,但是当您调用 new Proj05Runner(); 您调用的不是主方法,而是默认构造函数。
你必须找到可能的解决方案,把那条线改成 Proj05Runner.main(args); 或通过空构造函数更改proj05runner主方法:

public Proj05Runner(){
    GUI gui = new GUI();
}

顺便说一下,如果您使用的是jdk10或更高版本,则可以使用var关键字: var gui = new GUI();

相关问题