gpt4 book ai didi

java - 在 windowClosing 事件上终止 Java 中的 JFrame 线程

转载 作者:行者123 更新时间:2023-12-02 07:01:52 25 4
gpt4 key购买 nike

我正在尝试编写应用程序,该应用程序将在按下按钮时创建新窗口,显示当前窗口计数并关闭窗口和关闭窗口时的线程。

基本上,功能是这样的(其中一些正在工作):

  1. 启动应用程序时显示窗口(确定)
  2. 按按钮创建新窗口(确定)
  3. 按下按钮时显示当前窗口数(创建时确定,关闭窗口时否)
  4. 按下“X”(确定)时销毁窗口
  5. 当原始窗口关闭时销毁主线程(NOK)

这是我的代码:

package projectpackage;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

class MyMouseEvent extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
MyWindowThread.incrementMyWindowThreadCount();

MyWindowThread obj1 = new MyWindowThread();

Thread thr1 = new Thread(obj1);

thr1.start();

}
}

class MyWindowThread extends JFrame implements Runnable
{

private int height;
private int width;

private static int MyWindowThreadCount = 1;

public static int getMyWindowThreadCount()
{
return MyWindowThreadCount;
}

public static void incrementMyWindowThreadCount()
{
MyWindowThreadCount++;
}

public static void decrementMyWindowThreadCount()
{
MyWindowThreadCount--;
}

public MyWindowThread()
{
super("Frame "+getMyWindowThreadCount());

this.setHeight(300);

this.setWidth(400);

this.setBounds(100,100,getWidth(),getHeight());

this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
MyWindowThread.decrementMyWindowThreadCount();
//Thread.currentThread().interrupt();
return;
}
}
);

JPanel panel1 = new JPanel();

JButton button1 = new JButton("New window");

JButton button2 = new JButton("Count running windows");

this.getContentPane().add(panel1);

panel1.add(button1);

panel1.add(button2);

button1.addMouseListener(new MyMouseEvent());

button2.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
javax.swing.JOptionPane.showMessageDialog(null,"Window count = " + MyWindowThread.getMyWindowThreadCount());
}
}
);

this.setVisible(true); // show frame
}

public int getHeight()
{
return this.height;
}

public void setHeight(int h)
{
this.height = h;
}

public int getWidth()
{
return this.width;
}

public void setWidth(int w)
{
this.width = w;
}

public void run()
{

}

}

public class MyApp
{

public static void main(String[] args)
{
// Creating objects START

MyWindowThread obj1 = new MyWindowThread();

Thread thr1 = new Thread(obj1);

thr1.start();
}

}

list .mf:

Manifest-Version: 1.0Main-Class: projectpackage.MyApp

我希望您知道如何在控制台中编译和运行Java应用程序。如果您不这样做,这里是:

(在 CLI 中导航到其中包含“projectpackage”目录的目录。*.java 文件必须位于“projectpackage”目录内)

    javac projectpackage\*.java    jar cfm MyApp.jar manifest.mf projectpackage\*    java -jar MyApp.jar

任何人都可以告诉我我做错了什么或者我需要做什么才能使代码正常工作吗?或者我只是有根本性的误解?

最佳答案

JFrame frame = new JFrame();

这是您的问题,您扩展了 JFrame 类但尝试使用新对象。如果您愿意,您应该将此行替换为 super();super("Frame "+ getMyThreadCount());

编辑:另一个要提到的问题:您将类命名为 MyThread,这对于窗口来说是一个非常令人恼火的类名;)

编辑2:其他一些事情:

  • public void run() 没有内容,那么为什么 MyThread 实现 Runnable
  • 您创建了一些线程,但如果我没猜错的话,您就不会启动任何线程。

关于java - 在 windowClosing 事件上终止 Java 中的 JFrame 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16550239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com