gui还是不会出现

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

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

  1. public static void main(String[] args){
  2. new Proj05Runner();
  3. }//end main
  4. }//end class Proj05

这是我的跑步者代码:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class Proj05Runner extends JFrame{
  5. public static void main(String[] args){
  6. GUI gui = new GUI();
  7. }
  8. }
  9. class GUI extends JFrame{
  10. public GUI(){//constructor
  11. //Create a new Frame object
  12. JFrame displayWindow = new JFrame();
  13. displayWindow.setSize(300,200);
  14. displayWindow.setTitle("Brendan");
  15. //Button button1 = new Button("Press");
  16. //Instantiate two Listener objects that will process
  17. // Window events
  18. WProc1 winProcCmd1 = new WProc1(displayWindow);
  19. WProc2 winProcCmd2 = new WProc2();
  20. //Register the Listener objects for notification of
  21. // Window events. This object is the Event Source.
  22. displayWindow.addWindowListener(winProcCmd1);
  23. displayWindow.addWindowListener(winProcCmd2);
  24. //windowActivated and windowOpened test messages
  25. // are produced here
  26. displayWindow.setVisible(true);
  27. }
  28. }
  29. class WProc1 implements WindowListener{
  30. //used to save a reference to the Frame object
  31. JFrame displayWindowRef;
  32. WProc1(JFrame windowIn){//constructor
  33. // save ref to JFrame object
  34. this.displayWindowRef = windowIn;
  35. }//end constructor
  36. public void windowClosed(WindowEvent e){
  37. System.out.println("WProc1 windowClosed test msg");
  38. }//end windowClosed()
  39. public void windowIconified(WindowEvent e){
  40. System.out.println("WProc1 windowIconified test msg");
  41. }//end windowIconified()
  42. public void windowOpened(WindowEvent e){
  43. System.out.println("WProc1 windowOpened test msg");
  44. }//end windowOpened()
  45. public void windowClosing(WindowEvent e){
  46. System.out.println("WProc1 windowClosing test msg");
  47. displayWindowRef.dispose();//generate WindowClosed
  48. }//end windowClosing()
  49. public void windowDeiconified(WindowEvent e){
  50. System.out.println(
  51. "WProc1 windowDeiconified test msg");
  52. }//end windowDeiconified()
  53. public void windowActivated(WindowEvent e){
  54. System.out.println("WProc1 windowActivated test msg");
  55. }//end windowActivated()
  56. public void windowDeactivated(WindowEvent e){
  57. System.out.println(
  58. "WProc1 windowDeactivated test msg");
  59. }//end windowDeactivated()
  60. }//end class WProc1
  61. //=======================================================//
  62. //This and the previous class can be used to instantiate
  63. // Listener objects. Note that this class extends an
  64. // Adapter class that can be used to avoid the
  65. // requirement to define all of the methods of the
  66. // actual Listener class named WindowListener. This class
  67. // overrides only two of the methods declared in the
  68. // interface. It displays a message whenever one of the
  69. // methods is called.
  70. class WProc2 extends WindowAdapter{
  71. public void windowIconified(WindowEvent e){
  72. System.out.println(
  73. "********WProc2 windowIconified test msg");
  74. }//end windowIconified()
  75. public void windowDeiconified(WindowEvent e){
  76. System.out.println(
  77. "********WProc2 windowDeiconified test msg");
  78. }//end windowDeiconified()
  79. }//end class WProc2

提前谢谢你的帮助

lrpiutwd

lrpiutwd1#

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

  1. public Proj05Runner(){
  2. GUI gui = new GUI();
  3. }

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

相关问题