为什么 java 后台进程很慢?
Why is java background process slow?
请记住,我是 java 的新手,并且只将 C# 用于桌面应用程序。
我刚刚创建了一个全新的项目并希望在 window 打开时加载内容,我有一个带有 JPanel(swing) 的 JFrame,我测试了很多东西直到我发现 windowOpened
等同于 C# 中的 onload
方法,当尝试使用此事件加载内容时出现问题,经过多次尝试我没有得到任何东西所以我去了基础并调用了 System.out.print("Test");
.
JFrame 加载成功,但消息没有显示在控制台中直到我关闭 window,然后添加了一个按钮来玩并添加了相同的打印,点击按钮时我什么也没有在控制台中,直到我向它发送了 5 到 6 次垃圾邮件,有点奇怪所以我在打印后添加了一个 showMessageDialog
令我惊讶的是消息对话框按预期工作但打印仍然无法正常工作直到我多次发送垃圾邮件.
这是我尝试加载内容的第三个全新项目,但它们似乎都以相同的方式工作,这是我的电脑有问题还是这是 java 的正常行为?我可以改变这种行为吗?正确的做法是什么?
这是控制台日志:
cd PATH\NetBeansProjects\mavenproject1; "JAVA_HOME=C:\Program Files\Java\jdk-16.0.2" cmd /c "\"PATH" -Dexec.vmArgs= -Dexec.args=\"${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}\" -Dexec.appArgs= -Dexec.mainClass=.mavenproject1.NewJFrame -Dexec.executable=\"PATH" -Dmaven.ext.class.path=\"PATH"
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...
-----------------------< mavenproject1 >------------------------
Building mavenproject1 1.0
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:3.0.0:exec (default-cli) @ mavenproject1 ---
not working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expected
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 13.098 s
Finished at: 2021-08-16T17:14:40-05:00
------------------------------------------------------------------------
这是代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package .mavenproject1;
import javax.swing.JOptionPane;
/**
*
* @author
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(196, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(131, 131, 131))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(196, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(79, 79, 79))
);
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
System.out.print("not working as expected");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.print("not working as expected");
javax.swing.JOptionPane.showMessageDialog(this,"","",JOptionPane.OK_OPTION);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
正如@apangin 所说,这是因为缓冲输出,只需将 print
更改为 pringln
,您就可以看到我想要的即时结果。
请记住,我是 java 的新手,并且只将 C# 用于桌面应用程序。
我刚刚创建了一个全新的项目并希望在 window 打开时加载内容,我有一个带有 JPanel(swing) 的 JFrame,我测试了很多东西直到我发现 windowOpened
等同于 C# 中的 onload
方法,当尝试使用此事件加载内容时出现问题,经过多次尝试我没有得到任何东西所以我去了基础并调用了 System.out.print("Test");
.
JFrame 加载成功,但消息没有显示在控制台中直到我关闭 window,然后添加了一个按钮来玩并添加了相同的打印,点击按钮时我什么也没有在控制台中,直到我向它发送了 5 到 6 次垃圾邮件,有点奇怪所以我在打印后添加了一个 showMessageDialog
令我惊讶的是消息对话框按预期工作但打印仍然无法正常工作直到我多次发送垃圾邮件.
这是我尝试加载内容的第三个全新项目,但它们似乎都以相同的方式工作,这是我的电脑有问题还是这是 java 的正常行为?我可以改变这种行为吗?正确的做法是什么?
这是控制台日志:
cd PATH\NetBeansProjects\mavenproject1; "JAVA_HOME=C:\Program Files\Java\jdk-16.0.2" cmd /c "\"PATH" -Dexec.vmArgs= -Dexec.args=\"${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}\" -Dexec.appArgs= -Dexec.mainClass=.mavenproject1.NewJFrame -Dexec.executable=\"PATH" -Dmaven.ext.class.path=\"PATH"
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...
-----------------------< mavenproject1 >------------------------
Building mavenproject1 1.0
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:3.0.0:exec (default-cli) @ mavenproject1 ---
not working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expected
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 13.098 s
Finished at: 2021-08-16T17:14:40-05:00
------------------------------------------------------------------------
这是代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package .mavenproject1;
import javax.swing.JOptionPane;
/**
*
* @author
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(196, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(131, 131, 131))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(196, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(79, 79, 79))
);
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
System.out.print("not working as expected");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.print("not working as expected");
javax.swing.JOptionPane.showMessageDialog(this,"","",JOptionPane.OK_OPTION);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
正如@apangin 所说,这是因为缓冲输出,只需将 print
更改为 pringln
,您就可以看到我想要的即时结果。