不断在 java.lang.IllegalMonitorStateException 的线程中出现错误异常"AWT-EventQueue-0"



我正在研究此类似错误的先前问题,但是我找不到未正确声明或使用的变量。目前,我的程序上只有四个选项,问题是第一个图像是唯一显示的图像,然后出现错误。我尝试过几次重写此代码,但似乎无法避免此错误。其他问题或代码似乎没有帮助。

私有void btnstartactionperformed(java.awt.event.actionevent evt){
waittime = integer.valueof(lblwaitput.getText());

    if (waitTime <= 0){
        waitTime = 60;
    }
    // METHOD TO CLEAR UP DEBRIS AND TO SPLIT INTO ARRAY
    temp = txtList.getText();
    temp = temp.toLowerCase();
    selects = temp.split(" ");
    for (i = 0; i < selects.length; i++){ //CLEANS UP VARIABLES FOR A PROPER PATH TO GRAB IMAGES FROM
        selects[i] = "/SlideShow/" + selects[i] + ".jpg";
        selectcount++;
    }     // END OF CLEANING
    //START OF SLIDE SHOW
    for (x = 0; x <= selectcount; x++){//THE LOOP THAT CYCLES THROUGH THE IMAGES
        lblDisplay.setIcon(null);
        System.out.println(selects[x]); //THE VARIABLE WHICH THE IMAGE IS IN
        lblDisplay.setIcon(new javax.swing.ImageIcon(getClass().getResource(selects[x]))); //DISPLAYING IMAGE
        try {
            Thread.currentThread().wait(waitTime*10);
        }
        catch(InterruptedException ie){
        }
    }
    // END OF SLIDES NOTHING FURTHER
}          

以下是我的完整代码。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SlideShow;
    import javax.swing.*;
/**
 *
 * @author palaceag196
 */
public class MainSlides extends javax.swing.JFrame {
    String[] options = {"Soccer","Basketball","Football","Hockey","Ravioli","Banana","Pizza","Hotdog","Cat","Dog","Bird","Hamster"};
    String temp;
    String[] selects;
    int selectcount;
    int i, x;
    int waitTime;
    //              new ImageIcon(getClass().getResource(temp))
    /**
     * Creates new form MainSlides
     */
    public MainSlides() {
        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() {
        jScrollPane1 = new javax.swing.JScrollPane();
        listSelect = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtList = new javax.swing.JTextArea();
        lblWaitput = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnStart = new javax.swing.JButton();
        lblDisplay = new javax.swing.JLabel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        listSelect.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Soccer", "Basketball", "Football", "Hockey",       "Ravioli", "Banana", "Pizza", "Hotdog", "Cat", "Dog", "Bird", "Hamster" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listSelect.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listSelectMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listSelect);
        txtList.setColumns(12);
        txtList.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        txtList.setLineWrap(true);
        txtList.setRows(12);
        txtList.setAutoscrolls(false);
        txtList.setFocusable(false);
        jScrollPane2.setViewportView(txtList);
        lblWaitput.setText("60");
        lblWaitput.setToolTipText("Input wait time here.");
        jLabel1.setText("Wait Time:");
        btnStart.setText("Start The Show");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(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()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblWaitput, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                        .addComponent(btnStart))
                    .addComponent(lblDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblWaitput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(btnStart))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>
    private void listSelectMouseClicked(java.awt.event.MouseEvent evt) {                                        
        txtList.setText(txtList.getText()  + options[listSelect.getSelectedIndex()] + " ");
        // TODO add your handling code here:
    }                                       
    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {                                         
        waitTime = Integer.valueOf(lblWaitput.getText());
        if (waitTime <= 0){
            waitTime = 60;
        }
        // METHOD TO CLEAR UP DEBRIS AND TO SPLIT INTO ARRAY
        temp = txtList.getText();
        temp = temp.toLowerCase();
        selects = temp.split(" ");
        for (i = 0; i < selects.length; i++){ //CLEANS UP VARIABLES FOR A PROPER PATH TO GRAB IMAGES FROM
            selects[i] = "/SlideShow/" + selects[i] + ".jpg";
            selectcount++;
        }     // END OF CLEANING
        //START OF SLIDE SHOW
        for (x = 0; x <= selectcount; x++){//THE LOOP THAT CYCLES THROUGH THE IMAGES
            lblDisplay.setIcon(null);
            System.out.println(selects[x]); //THE VARIABLE WHICH THE IMAGE IS IN
            lblDisplay.setIcon(new javax.swing.ImageIcon(getClass().getResource(selects[x]))); //DISPLAYING IMAGE
            try {
                Thread.currentThread().wait(waitTime*10);
            }
            catch(InterruptedException ie){
            }
        }
        // END OF SLIDES NOTHING FURTHER
        // TODO add your handling code here:
    }                                        
    /**
     * @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(MainSlides.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainSlides.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainSlides.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainSlides.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 MainSlides().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnStart;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblDisplay;
    private javax.swing.JTextField lblWaitput;
    private javax.swing.JList listSelect;
    private javax.swing.JTextArea txtList;
    // End of variables declaration
}

不要将循环和线程(...)用于动画。

用于动画您应该使用秋千定时器。

保留所有摇摆基础知识的挥杆教程方便的链接。

相关内容

最新更新