在我的程序中,我在背景中顯示帶有歌曲的圖像。 歌曲完成後,我需要關閉幀和JInternalFrame
。 下面是代碼:JFrame行爲怪異
package projectfinal;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
/* MORE IMPORTS */
public class ImagePanel extends javax.swing.JFrame {
public static class audio extends JApplet {
private static final int EXTERNAL_BUFFER_SIZE = 128000;
public void aaudio() {
String strFilename = "E:/zehreelay.wav";
File soundFile = new File(strFilename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
AudioFormat audioFormat = audioInputStream.getFormat();
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
line.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
line.drain();
line.close();
jInternalFrame1.setVisible(false);
/**
* closing internal frame
*/
}
}
/**
* Creates new form NewJFrame
*/
public ImagePanel() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jInternalFrame1 = new javax.swing.JInternalFrame();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
//////////more code////////////////////////////////////
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentHidden(java.awt.event.ComponentEvent evt) {
hiden(evt);
}
});
/////adding a listner for component///
}
public static void main(String args[]) {
start();
}
private void hiden(java.awt.event.ComponentEvent evt) {
setVisible(false);
}
public static void start() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ImagePanel().setVisible(true);
}
});
audio obj = new audio();
obj.aaudio();
}
private static javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
/////more variables...//////
}
此代碼是好的,但是當我嘗試了沒有添加組件偵聽器, 就是getRootPane().SetVisible(false)
或jInternalFrame1.getContentPane().setVisible(false)
或setVisible(false)
他們都不是工作,但它的工作只有在添加一個隱藏的偵聽器組件之後。爲什麼?有什麼理由?那麼JInternalFrame
怎麼樣了(JInternalFrame.setVisible(false)
)?這與rootPane()
有關嗎?
Swing GUI對象應該在[事件調度線程](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)上的_only_上構建和操作。 – trashgod
@trashgod那麼爲什麼它能正常工作,如果我們只是通常說setVisible(false)?爲什麼不在這種情況下? –
Trashgod是對的。爲什麼你不能將調用包裝在SwingUtilities.invokeLater中? – StanislavL