java - 尽管编译了运行器文件,但 GUI 不会出现
问题描述
我的作业要求我们编写一个由原始项目文件打开的“运行程序”文件,并且只有在运行项目文件时,我的运行程序文件才不会创建 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
提前感谢您的帮助
解决方案
Proj05Runner 有一个静态的 main 方法,没有构造函数,但是当你调用时,你调用new Proj05Runner();
的不是那个 main 方法,而是一个默认的构造函数。
您必须找到可能的解决方案,Proj05Runner.main(args);
通过空构造函数更改该行或更改 Proj05Runner 主方法:
public Proj05Runner(){
GUI gui = new GUI();
}
顺便说一句,如果您使用的是 JDK10 或更高版本,则可以使用 var 关键字:var gui = new GUI();
推荐阅读
- database-normalization - 规范化...地址字段属于哪个表?
- rest - 没有角色和权限的 Web 服务的 Spring Security
- jquery - 如何在jQuery中将函数直接附加到ID?
- javascript - 加载 shapefile 时的传单错误(未定义 shp)
- c# - SignOutAsync 删除 cookie 但返回客户端应用程序时重新生成?
- python - 如何在用户输入负数时添加异常
- csv - 为什么我在 Snowflake PUT 加载中遇到意外错误?
- java - 使杰克逊在类的序列化尝试上失败
- php - Laravel 集合
- postgresql - 从联接表中选择 max(id) 低效查询计划