gpt4 book ai didi

java - JFrame GUI 卡住 + Phantom

转载 作者:行者123 更新时间:2023-12-02 13:26:51 24 4
gpt4 key购买 nike

好吧,我明白我不应该有这个循环

while (pidInfo.contains(<processname>)){
pidInfo ="";
check<processname> =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
input = new BufferedReader(new InputStreamReader(check<processname>.getInputStream()));

while ((line = input.readLine()) != null) {
pidInfo+=line;
}

input.close();

if (pidInfo.contains(<processname>)){
System.out.println("<processname> RUNNING");
}
else if (closeReason == 2){
System.out.println("<processname> STOPPED VIA PROG & USER");
status = "Not Running";
int ll = JOptionPane.showOptionDialog(null, "<processname>", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
if (ll == JOptionPane.OK_OPTION){
//frame.setAlwaysOnTop(true);
frame.dispose();
}
}
// TODO: Other exit
}

基于其他问题但是

我不明白为什么当我从第一个框架启动这个框架(这是第二个框架)时它会卡住,但如果我单独启动第二个框架它会显示。

我希望从这个问题中得到两点

1:如何编写在后台运行循环的事物(threadworker?)。

2:为什么当我从第一帧启动它时它不会卡住(见下文)

提前谢谢您,这是第一个框架代码

if(pidInfo.contains(<processname>)) {
frame.setAlwaysOnTop(false);
status = "Running - In Game";

System.out.println("<processname>1st RUNNING");
int a = JOptionPane.showOptionDialog(null, "SUCCESS!", "Success", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (a == JOptionPane.OK_OPTION) {
frame.dispose();
new 2ndFrameRunning();
}
}

2ndFrameRunning 是第二个框架,它更改了其实际名称,这就是为什么它违反了 java 语法规则。

没有错误,只是“幻影”卡住。

示例代码

框架 1:命名为“menu.java”

/* Stack Overflow
* By Rabbitmcv
*/
package main;
// Imports
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.awt.*;
import javax.swing.*;

public class Menu implements ActionListener{
// Func
// Creates the JFrame
JFrame frame = new JFrame();
// Public Vars
boolean isGameRunning = false;
String status = "Not Running - In Main Menu";
// All code goes here
public Menu(){
// Vars
// Frame Vars
frame.setResizable(false);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
frame.setAlwaysOnTop(true);
// JPanels
JPanel defaultPanel = new JPanel();
// DefaultPanel J...
JLabel title = new JLabel("FirstLabel");
JLabel risk = new JLabel("SecondLabel");
JLabel titleSub = new JLabel("StackOverflow");
JButton start2frame = new JButton("start2frame");
// DefaultPanel Button Code
start2frame.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
System.out.println("start2frame button pressed");
try{
String line;
String pidInfo ="";

Process checkcs =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");

BufferedReader input = new BufferedReader(new InputStreamReader(checkcs.getInputStream()));

while ((line = input.readLine()) != null) {
pidInfo+=line;
}

input.close();

if(pidInfo.contains("explorer.exe")) // The process name here has been changed from the real process for StackOverflow.
{
frame.setAlwaysOnTop(false);
status = "Running - In Game";
frame.setTitle("StackOverflow - "+status);
System.out.println("1st Game RUNNING");
// TODO: hackRunning
int a = JOptionPane.showOptionDialog(null, "SUCCESS", "Success", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (a == JOptionPane.OK_OPTION){
frame.dispose();
new second();
}
}
else{
isGameRunning = false;
status = "Not Running - ERROR: Game NOT RUNNING";
frame.setTitle("StackOverflow - "+status);
System.out.println("game NOT RUNNING");
frame.setAlwaysOnTop(false);
int a = JOptionPane.showOptionDialog(null, "Game is not running", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
if (a == JOptionPane.OK_OPTION){
frame.setAlwaysOnTop(true);
}
}
}
catch(Exception e){
System.out.println("Failed to check process");
e.printStackTrace();
}
}
});
// Set Layouts
defaultPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
defaultPanel.setLayout(new GridLayout(0, 1));
// Adds the J... to a Jpanel (IN ORDER FROM TOP TO BOTTOM)
defaultPanel.add(title);
defaultPanel.add(risk);
defaultPanel.add(titleSub);
defaultPanel.add(start2frame);
// Center Labels
title.setHorizontalAlignment(JLabel.CENTER);
risk.setHorizontalAlignment(JLabel.CENTER);
titleSub.setHorizontalAlignment(JLabel.CENTER);
// Add the JPanel to the JFrame
frame.add(defaultPanel);
// Vis
frame.revalidate();
frame.setTitle("StackOverflow - "+status);
frame.setVisible(true);
}

public static void main (String args[]){
new Menu();
}
@Override
public void actionPerformed(ActionEvent e) {
// AUTO: Because of actionlistener

}

}

框架 2 命名为“second.java”

/* StackOverflow - 2nd
* By Rabbitmcv
* If one runs this one sans the first program it shows non-frozen... but if you run it from menu.java it will freeze... probably something with threads.
*/
package main;

// Import
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.awt.*;
import javax.swing.*;
// This file has been renamed to 2.java
public class second implements ActionListener{
// Frame
JFrame frame= new JFrame();
// Panel
JPanel panel = new JPanel();
// Int
// closeReason: 0 = user, 1 = program, 2 = user init via prog (non-error)
int closeReason = 0;
// String
String status = "Running";
public second(){
// Set Settings
frame.setResizable(false);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
frame.setAlwaysOnTop(true);
// Crap

frame.setTitle("StackOverflow- Running");
// Settings
// Add J...
JLabel label = new JLabel("Game "+status);
JButton closeGameButton = new JButton("Click here to close game");
// Adds the J... to a Jpanel (IN ORDER FROM TOP TO BOTTOM)
panel.add(label);
panel.add(closeGameButton);
// Button Func.
closeGameButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
try {
System.out.println("CloseGameButton Pressed");
Process killGame = Runtime.getRuntime().exec("taskkill /F /IM explorer.exe"); // My process has been changed to explorer.exe !!!! THIS WILL STOP WINDOWS EXPLORER. Feel free to change it to another process
closeReason = 2;
int exitCode = killGame.waitFor();
if (exitCode != 0){
throw new IOException("Failed to kill game; game not running");
}
frame.setTitle("StackOverflow - Not Running - Closed by user");
frame.setAlwaysOnTop(false);
int a = JOptionPane.showOptionDialog(null, "game has been closed", "game Closed", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (a == JOptionPane.OK_OPTION){
frame.setAlwaysOnTop(true);
frame.setTitle("StackOverflow - Not Running - Closed by user");
}
} catch (Exception e) {
frame.setAlwaysOnTop(false);
System.out.println("Failed to kill game");
int a = JOptionPane.showOptionDialog(null, "game is not running", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
if (a == JOptionPane.OK_OPTION){
frame.setAlwaysOnTop(true);
}
e.printStackTrace();
}
}
});
// END BUTTON FUNC
// Set layouts
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 30, 10));
panel.setLayout(new GridLayout(0, 1));
// Center Labels
label.setHorizontalAlignment(JLabel.CENTER);
// Add the JPanel to the JFrame
frame.add(panel);
// end
frame.revalidate();
System.out.println("far");
frame.setVisible(true);

try{
String line;
String pidInfo ="";

Process checkcs =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");

BufferedReader input = new BufferedReader(new InputStreamReader(checkcs.getInputStream()));

while ((line = input.readLine()) != null) {
pidInfo+=line;
}

input.close();

if (pidInfo.contains("explorer.exe")){ // Changed to explorer.exe
System.out.println("game running - pid");
status = "Running";
while (pidInfo.contains("explorer.exe")){ // Changed to explorer.exe
pidInfo ="";
checkcs =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
input = new BufferedReader(new InputStreamReader(checkcs.getInputStream()));

while ((line = input.readLine()) != null) {
pidInfo+=line;
}

input.close();

if (pidInfo.contains("explorer.exe")){ // This checks if the process is still going on... changed to explorer.exe
System.out.println("game RUNNING");
}
else if (closeReason == 2){
System.out.println("game STOPPED VIA PROG & USER");
status = "Not Running";
int ll = JOptionPane.showOptionDialog(null, "game has stopped running", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
if (ll == JOptionPane.OK_OPTION){
//frame.setAlwaysOnTop(true);
frame.dispose();
}
}
// TODO: Other exit
}
}
}
catch(Exception e){
System.out.println("Failed to check process");
e.printStackTrace();
}
}
public static void main (String args[]){
new second(); //StackOverflow
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
}

如果您喜欢,您可以直接下载包含所有文件的 .zip

https://drive.google.com/open?id=0BzuCgrBgpuD1V3dKLUFfQU12UWc

最佳答案

正如我在评论中所说,当...

public static void main (String args[]){
new second(); //StackOverflow
}

被调用(即由JVM调用,因为您已经从命令行执行了该类),它在JVM的“主”线程中运行。

这意味着构造函数中的代码不会在事件调度线程的上下文中执行,因此不会阻塞 UI。

当您调用frame.setVisible时,底层API将创建事件调度线程,并且所有基于UI的代码将在单独的线程上执行,而您在构造函数中启动的代码将在构造函数中执行“主”线程的上下文。

当调用 closeGameButtonActionListener 时,无论您如何启动代码,都会阻塞事件调度线程,直到 killGame 完成.

您可以通过更改使第二个以相同的方式执行

public static void main (String args[]){
new second(); //StackOverflow
}

public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new second(); //StackOverflow
}
});
}

这就是您应该如何启动所有 Swing 应用程序

我建议您首先查看 Concurrency in SwingWorker Threads and SwingWorker

关于java - JFrame GUI 卡住 + Phantom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43357272/

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