- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,我明白我不应该有这个循环
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的代码将在单独的线程上执行,而您在构造函数中启动的代码将在构造函数中执行“主”线程的上下文。
当调用 closeGameButton
的 ActionListener
时,无论您如何启动代码,都会阻塞事件调度线程,直到 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 Swing和Worker Threads and SwingWorker
关于java - JFrame GUI 卡住 + Phantom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43357272/
我正在尝试将 JFrame (bar) 添加到 JFrame (foo),这会强制 bar 在之前关闭foo 可以再次集成。 例如,在我可以再次编写代码之前,必须关闭“About Eclipse”框架
我有一个名为 User 的 JFrame,在其中声明一个名为 id 的变量,并根据某些条件将其设置为特定值。 我需要在称为输出的第二个 JFrame 中使用此变量。 这是我的代码 class Inpu
我有这个问题:我有一个 ArrayList,其中包含一些我想在特定框架中使用的项目,问题是数组列表通过在主类中初始化而变满我的项目。在本类(class)中,我还启动了与其他框架链接的起始框架(登录框架
如何在隐藏一个 Swing 程序的同时将一个 javax.Swing 程序移动到另一个 Swing 程序,以及如何制作可滚动的 JFrame。请帮助我。 最佳答案 在这里你可以找到一个非常good e
所以基本上我有一个 2D 游戏(基本上是过去的仿制品的链接),当你按 e 键时,库存会打开,关闭时会隐藏。问题是,每次我按 e 键时,它都会打开一个包含所有初始值的新库存,我希望它在初始值之后打开一个
我有一个 JFrame(1,主框架),它延续了 JPanel 和 JTable 以及数据。用户可以编辑数据。因此,一个新的 JFrame (2) 打开,用户可以输入新数据。如果他们在第二帧中单击“确定
我有一个新的netbeans“Java应用程序”项目,我试图从主JFrame添加第二个JFrame,用户可以从中加载文件。 所以我有主要的 JFrame main 方法 public static v
我的程序应该在单击按钮时启动第二个 JFrame 并打印一条语句,但它总是启动三个 JFrame 并打印三个语句。我只需要它来打印出一份声明并启动一个 Jframe。这是代码: import java
我目前已经构建了一个使用多个框架的应用程序。但如果我能在 1 帧中使用所有我用过的帧,那就太好了。如下图所示。 因此,如果您按左侧按钮“Speler Overzicht”,它将在右侧面板中向用户显示,
我目前有一个按钮,单击该按钮时,会执行一种方法,该方法创建一个带有加载多个图像的面板的 jframe。如果多次单击此按钮,图像将继续添加到加载到 jframe 上的现有图像上。我应该使用什么代码,以便
为什么无法将JFrame添加到JFrame中?它只是将一个组件添加到容器中。 Java 如何禁止我这样做?是的,我知道这样做没有意义,但我的问题的重点是了解 Swing 机制 - 它在幕后是如何工作的
我创建了一个生成 StartUpWindow 对象的类。其中一个 JButton 组件关闭 JFrame,然后调用一种新类型的框架进行实例化,AdminMainWindow。但是,当 AdminMai
我试图在不显示 JFrame 本身的情况下将 JFrame 渲染为图像(类似于 this 问题所问的内容)。我试过使用这段代码: private static BufferedImage getScr
我正在使用 XFCE 4 桌面的 Debian 8 上编译并运行以下代码。 import javax.swing.JFrame; import javax.swing.JComponent; impo
我有 14 个 JFrame 类和一个 JFrame 类,其中包含一个充当日历的 JTable。我希望当用户从 JTable 日历中选择日期时,更改 14 个 JFrame 类中任何一个文本字段或任何
我有 3 个扩展 JFrame 的对象让我们调用他们 FrameA FrameB FrameC . FrameA是我的主应用程序窗口。来自 FrameA的构造函数如果应用程序未注册我创建 FrameB
我试图简单地创建四个 jtextfields 和一个 jbutton。按下按钮后,我希望输入到 jtextfields 的文本作为参数(p、var、s、f)传递到另一个窗口,该窗口使用给定的参数显示数
这个问题已经有答案了: Close one JFrame without closing another? (2 个回答) 已关闭 6 年前。 我制作了一个程序,其中存在三个jframe。但问题是,当
我正在用 java 构建一个桌面学生管理应用程序。所以我需要关闭当前的 JFRAME,同时另一个 JFRAME 可见(关闭新的 jframe 后,旧的 jframe 应该可见)。你能帮我一下吗? 最佳
我正在开发的应用程序包含一个主 JFrame,用户最终可能会从中打开另一个补充框架。我正在尝试实现应用程序的这种行为,其中一旦主框架最小化,补充框架就会最小化(图标化)。 我正在考虑重写主框架的 se
我是一名优秀的程序员,十分优秀!