Java 스레드에 문제가 있습니다. Main이라는 메인 스레드와 Frame이라는 두 번째 스레드가 있다고 가정 해 보겠습니다. 게다가 프레임에는 JButton이 있습니다. Main에는 JFrame에서 Button을 푸시하는 동안 실행되는 루프가 있습니다. 내가 버튼을 눌러 경우, 아무 일도 발생하지 않습니다, 메인 클래스를 이제메인 스레드를 두 번째 스레드로 중지하십시오.
public class Frame extends javax.swing.JFrame{
private boolean isRunning;
public Frame() {
initComponents();
isRunning = true;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
isRunning = false;
}
public boolean isRunning(){
return isRunning;
}
// ***** Some netbeans stadard stuff *****
/**
* 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);
jButton1.setText("Stop");
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(layout.createSequentialGroup()
.addGap(167, 167, 167)
.addComponent(jButton1)
.addContainerGap(178, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(153, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(124, 124, 124))
);
pack();
}// </editor-fold>
public void main() {
/* 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(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame.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 Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
// ***** End of this netbeans stuff *****
}
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Frame f = new Frame();
f.main();
for (int i = 0; f.isRunning(); i++) {
System.out.println((i + 1));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
을하지만 :
내가 먼저 프레임 클래스, 이것에 대한 간단한 예제를 썼다. 또한 "Runnable를 구현합니다"로 클래스 프레임을 확장하기 위해 노력하고 실행() 덮어 - 방법과 함께 홈페이지에이를 시작
Thread t = new Thread(new Frame());
t.start();
하지만 같은 문제를 얻을.
제발, 아무도 도와 줄 수 있습니까?
안부 마티아스
* "아무 일도 일어나지 않습니다."*는 유용한 설명이 아닙니다. * 디버거를 사용하여 문제의 원인을 찾아야합니다. 그러나 당신의 디자인/접근 방식은 뒤쳐진 것처럼 보입니다 - UI는 주 스레드이어야하고 오래가는 작업은 별도의 스레드에 넣어야합니다. – UnholySheep
당신은 뒤로 물러나 진정한 [mcve]를 만들어야합니다. 우리는 코드를 실행하지 않습니다. 그냥보십시오. 내가 볼 수있는 것부터 ... 버튼을 클릭했을 때 Main main()의 루프가 멈추지 않아야하는 이유를 설명 할 수있는 것은 아무것도 없다. 적어도 특정 기간 후에; 언 홀리 (Unholy)가 말했듯이, 당신의 설명은 너무 모호합니다. – GhostCat