2013-05-13 82 views
1

我正在創建一個Java swing應用程序 - 排序和搜索算法的動畫。我使用JDeveloper 11g release2編寫代碼,默認情況下使用jdk 1.6,應用程序運行良好,但部署後我嘗試在我的計算機上運行它,女巫擁有jdk 1.7,應用程序在隨機時刻凍結並且不會恢復。我嘗試了不同的個人電腦和操作系統,每個人的應用程序都與Java 6很好,但如果與Java 7啓動卡住了。我嘗試調試它,但調試器凍結喜歡的應用程序,不解凍,直到我關閉它與Windows任務管理器。應用程序在Java 6上工作,但不在Java 7

設法跟蹤應用程序在嘗試設置文本時會凍結方法descriptionTA.setText(文本)(下面類的最後一個方法),有時會設置正確,但大多數情況下,一切都會停止沒有錯誤信息或其他生命信號。

這是發生問題的女巫班。

import animatedalgorithms.MasterFrame; 
import animatedalgorithms.sorting.SortAnimationComponent; 
import helperclasses.ApplicationConstants; 
import helperclasses.RenderedJLabel; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.TreeMap; 
import javax.swing.BorderFactory; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.JTextArea; 
import javax.swing.border.Border; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.LineBorder; 
import javax.swing.border.TitledBorder; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 


public class SortAnimationPanel extends JPanel { 

    private static final int DESCENDING_EELMENT_ORDER = 0; 
    private static final int ASCENDING_ELEMEMENT_ORDER = 1; 
    private static final int RANDOM_ELEMENT_ORDER = 2; 

    private MasterFrame frame; 
    private SortAnimationComponent comp; 
    private RenderedJLabel h1; 
    private JSlider algSlider; 
    private JSlider animSlider; 
    BackButtonComponent backBtn; 
    private int elmOrder; 
    private JTextArea descriptionTA ; 


    public SortAnimationPanel(MasterFrame frame) { 
     elmOrder = RANDOM_ELEMENT_ORDER; 
     this.setBorder(new LineBorder(Color.black)); 
     this.frame = frame; 
     this.setLayout(new BorderLayout()); 

     h1 = new RenderedJLabel(""); 
     h1.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 20)); 
     h1.setForeground(Color.white); 

     createControls(); 
    } 

    public void createControls() { 
     // Header 
     JPanel header = new JPanel(); 
     header.setLayout(new BorderLayout()); 
     header.setBackground(new Color(30, 30, 30)); 
     header.setBorder(new EmptyBorder(0, 10, 5, 5)); 
     header.setForeground(Color.WHITE); 
     header.add(h1, BorderLayout.CENTER); 

     JPanel aboutBtn = new JPanel(); 
     aboutBtn.setName("aboutPanel"); 
     aboutBtn.setBackground(Color.BLACK); 

     final RenderedJLabel aboutLabel = new RenderedJLabel("Apie algoritmą"); 
     aboutLabel.setForeground(Color.WHITE); 
     aboutLabel.setFont(new Font("Arial", Font.BOLD, 12)); 
     aboutBtn.add(aboutLabel); 

     header.add(aboutBtn, BorderLayout.EAST); 
     add(header, BorderLayout.NORTH);   

     //Bottom controls 
     JButton btn = new JButton("Iš naujo"); 
     btn.setName("replay"); 
     JButton btnPlay = new JButton("Rikiuoti"); 
     btnPlay.setName("play"); 
     JButton btnPause = new JButton("Pauzė"); 
     btnPause.setName("pause"); 
     JButton btnStep = new JButton("Žingsnis"); 
     btnStep.setName("step"); 
     backBtn = new BackButtonComponent(); 

     JPanel controls = new JPanel(); 
     controls.setLayout(new BoxLayout(controls, BoxLayout.X_AXIS)); 
     controls.setBackground(Color.WHITE); 
     controls.add(btn); 
     controls.add(btnPlay); 
     controls.add(btnPause); 
     controls.add(btnStep); 
     controls.add(Box.createHorizontalStrut(10)); 

     Border empty = BorderFactory.createEmptyBorder(10, 10, 30, 10); 
     controls.setBorder(empty); 

     JPanel labels = new JPanel(); 
     labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS)); 
     labels.add(new JLabel("Algoritmo greitis: ")); 
     labels.add(new JLabel("Animacijos greitis: ")); 
     labels.setBackground(Color.WHITE); 
     controls.add(labels); 

     JPanel sliders = new JPanel(); 
     sliders.setBackground(Color.WHITE); 
     sliders.setLayout(new BoxLayout(sliders, BoxLayout.Y_AXIS)); 
     algSlider = new JSlider(JSlider.HORIZONTAL, 100, 10000, 10000 - ApplicationConstants.DEFAULT_ALGORITHM_DELAY); 
     algSlider.setBackground(Color.WHITE); 
     algSlider.setMajorTickSpacing(2000); 
     algSlider.setMinorTickSpacing(100); 

     animSlider = new JSlider(JSlider.HORIZONTAL, 0, 20, 10); 
     animSlider.setMajorTickSpacing(10); 
     animSlider.setBackground(Color.WHITE); 
     animSlider.setMinorTickSpacing(2); 
     sliders.add(algSlider); 
     sliders.add(animSlider); 
     controls.add(Box.createHorizontalStrut(10)); 
     controls.add(sliders); 

     class ButtonListener implements ActionListener { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       JButton eventObj = (JButton)e.getSource(); 
       if (eventObj.getName().equalsIgnoreCase("play")) { 
        comp.setToPlay(); 
       } else if (eventObj.getName().equalsIgnoreCase("pause")) { 
        comp.setToPause(); 
       } else if (eventObj.getName().equalsIgnoreCase("replay")) { 
        resetControls(); 
        switch (elmOrder) { 
        case RANDOM_ELEMENT_ORDER: 
         comp.generateRandomArray(); 
         break; 
        case ASCENDING_ELEMEMENT_ORDER: 
         comp.generateAscendingArray(); 
         break; 
        case DESCENDING_EELMENT_ORDER: 
         comp.generateDescendingArray(); 
         break; 
        } 
        comp.resetSorter(); 
        comp.startAnimation(); 
       } else if (eventObj.getName().equalsIgnoreCase("step")) { 
        comp.step(); 
       } 
      } 
     } 

     class CustomMouseListener implements MouseListener { 
      boolean pressed = false; 

      @Override 
      public void mouseClicked(MouseEvent e) { 
      } 

      @Override 
      public void mousePressed(MouseEvent e) { 
       pressed = true; 
      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       if (pressed) { 
        if (e.getSource() instanceof JPanel) { 
         JFrame aboutFrame = new DescriptionFrame(comp.getAlgorithmNum()); 
        } else { 
         comp.stopThread(); 
         frame.setVisibleCard(MasterFrame.MENU_WINDOW); 
        } 
       } 
      } 

      @Override 
      public void mouseEntered(MouseEvent e) { 
       if (e.getSource() instanceof JPanel) { 
        JPanel p = (JPanel) e.getSource(); 
        p.setBackground(Color.WHITE); 
        aboutLabel.setForeground(Color.black); 
       } else { 
        BackButtonComponent comp = (BackButtonComponent)e.getSource(); 
        comp.setCompColor(Color.lightGray); 
        comp.repaint(); 
       } 
      } 

      @Override 
      public void mouseExited(MouseEvent e) { 
       if (e.getSource() instanceof JPanel) { 
        JPanel p = (JPanel) e.getSource(); 
        p.setBackground(Color.black); 
        aboutLabel.setForeground(Color.white); 
       } else { 
        BackButtonComponent comp = (BackButtonComponent)e.getSource(); 
        comp.setCompColor(Color.GRAY); 
        comp.repaint(); 
        pressed = false; 
       } 
      } 
     } 


     class SliderListener implements ChangeListener { 

      @Override 
      public void stateChanged(ChangeEvent e) { 
       JSlider source = (JSlider)e.getSource(); 
       if (!source.getValueIsAdjusting()) { 
        if (source.getMaximum() == 20) { 
         comp.setAnimationDelay(source.getMaximum() - source.getValue()); 
        } else { 
         comp.setAlgorithmStepDelay(source.getMaximum() - source.getValue()); 
        } 
       } 
      } 
     } 

     ActionListener btnListener = new ButtonListener(); 
     btn.addActionListener(btnListener); 
     btnPlay.addActionListener(btnListener); 
     btnStep.addActionListener(btnListener); 
     btnPause.addActionListener(btnListener); 

     CustomMouseListener l = new CustomMouseListener(); 
     backBtn.addMouseListener(l); 
     aboutBtn.addMouseListener(l);   

     ChangeListener sliderListener = new SliderListener(); 
     algSlider.addChangeListener(sliderListener); 
     animSlider.addChangeListener(sliderListener); 

     add(controls, BorderLayout.SOUTH); 

    } 

    public void createLeftMenu() { 

    } 

    public void resetControls() { 
     algSlider.setValue(algSlider.getMaximum() - ApplicationConstants.DEFAULT_ALGORITHM_DELAY); 
     animSlider.setValue(animSlider.getMaximum() - ApplicationConstants.DEFAULT_ANIMATION_DELAY); 
    } 

    public void loadAlgorithm(int sorterNum) { 
     resetControls(); 

     JPanel sortPanel = new JPanel(); 
     sortPanel.setBackground(Color.WHITE); 

     descriptionTA = new JTextArea(); 
     descriptionTA.setSelectionColor(Color.BLACK); 
     descriptionTA.setFocusable(false); 
     descriptionTA.setEnabled(true); 
     descriptionTA.setRows(5); 
     descriptionTA.setColumns(10); 
     descriptionTA.setFont(new Font("Arial", Font.BOLD, 12)); 
     descriptionTA.setWrapStyleWord(true); 
     descriptionTA.setLineWrap(true); 
     TitledBorder title = BorderFactory.createTitledBorder("Aprašymas"); 
     descriptionTA.setBorder(title); 

     HistoryComponent hist = new HistoryComponent(new TreeMap<String, Color>(), 30); 
     TreeMap<String, Color> map = new TreeMap<String, Color>(); 
     switch (sorterNum) { 
      case ApplicationConstants.SELECTION_SORT_NUM: 
      h1.setText("Išrinkimo rikiavimo algoritmas"); 
      map.put("- Mažiausias elementas", ApplicationConstants.IMPORTANT_ELEMENT_COLOR); 
      map.put("- Pasirinktas elementas", ApplicationConstants.SELECTED_ELEMENT_COLOR); 
      map.put("- Neutralus elementas", ApplicationConstants.NEUTRAL_ELEMENT_COLOR); 
      map.put("- Surikiuotas masyvas", ApplicationConstants.SORTED_ELEMENT_COLOR); 
      hist.setMap(map); 
      hist.repaint(); 
      comp = new SortAnimationComponent(ApplicationConstants.SELECTION_SORT_NUM, this); 
      comp.setToPause(); 
      comp.startAnimation(); 
      break; 
      case ApplicationConstants.BUBBLE_SORT_NUM: 
      h1.setText("Burbulo rikiavimo algoritmas"); 
      map.put("- Pasirinktas elementas", ApplicationConstants.SELECTED_ELEMENT_COLOR); 
      map.put("- Neutralus elementas", ApplicationConstants.NEUTRAL_ELEMENT_COLOR); 
      hist.setMap(map); 
      hist.repaint(); 
      comp = new SortAnimationComponent(ApplicationConstants.BUBBLE_SORT_NUM, this); 
      comp.setToPause(); 
      comp.startAnimation(); 
      break; 
      case ApplicationConstants.INSERTION_SORT_NUM: 
      h1.setText("Įterpimo rikiavimo algoritmas"); 
      map.put("- Surikiuotas masyvas", ApplicationConstants.SORTED_ELEMENT_COLOR); 
      map.put("- Pasirinktas elementas", ApplicationConstants.SELECTED_ELEMENT_COLOR); 
      map.put("- Neutralus elementas", ApplicationConstants.NEUTRAL_ELEMENT_COLOR); 
      hist.setMap(map); 
      hist.repaint(); 
      comp = new SortAnimationComponent(ApplicationConstants.INSERTION_SORT_NUM, this); 
      comp.setToPause(); 
      comp.startAnimation(); 
      break; 
      case ApplicationConstants.QUICK_SORT_NUM: 
      h1.setText("Greitojo rikiavimo algoritmas"); 
      map.put("- Vidurio taškas ", ApplicationConstants.IMPORTANT_ELEMENT_COLOR); 
      map.put("- Neutralus elementas", ApplicationConstants.NEUTRAL_ELEMENT_COLOR); 
      map.put("- Neaktyvus elementas", ApplicationConstants.INACTIVE_ELEMENT_COLOR); 
      hist.setMap(map); 
      hist.repaint(); 
      comp = new SortAnimationComponent(ApplicationConstants.QUICK_SORT_NUM, this); 
      comp.setToPause(); 
      comp.startAnimation(); 
      break; 
      case ApplicationConstants.MERGE_SORT_NUM: 
      h1.setText("Suliejimo rikiavimo algoritmas"); 
      map.put("- Neutralus elementas", ApplicationConstants.NEUTRAL_ELEMENT_COLOR); 
      map.put("- Neaktyvus elementas", ApplicationConstants.INACTIVE_ELEMENT_COLOR); 
      hist.setMap(map); 
      hist.repaint(); 
      comp = new SortAnimationComponent(ApplicationConstants.MERGE_SORT_NUM, this); 
      comp.setToPause(); 
      comp.startAnimation(); 
      break; 
     default: 
      h1.setText("Išrinkimo rikiavimo algoritmas"); 
      map.put("- Mažiausias elementas", ApplicationConstants.IMPORTANT_ELEMENT_COLOR); 
      map.put("- Pasirinktas elementas", ApplicationConstants.SELECTED_ELEMENT_COLOR); 
      map.put("- Neutralus elementas", ApplicationConstants.NEUTRAL_ELEMENT_COLOR); 
      hist.setMap(map); 
      hist.repaint(); 
      comp = new SortAnimationComponent(ApplicationConstants.SELECTION_SORT_NUM, this); 
      comp.setToPause(); 
      comp.startAnimation(); 
      break; 
     } 

     JPanel leftPanel = new JPanel(); 
     leftPanel.setLayout(new BorderLayout()); 
     leftPanel.setBackground(Color.WHITE); 
     JPanel spacer = new JPanel(); 
     spacer.setBackground(Color.white); 
     spacer.setLayout(new BoxLayout(spacer, BoxLayout.X_AXIS)); 
     spacer.add(backBtn); 
     spacer.add(Box.createHorizontalStrut(120)); 
     leftPanel.add(spacer, BorderLayout.NORTH); 

     JPanel elmOrderPanel = new JPanel(); 
     elmOrderPanel.setLayout(new BoxLayout(elmOrderPanel, BoxLayout.Y_AXIS)); 
     elmOrderPanel.setBorder(BorderFactory.createTitledBorder("Elementų išdėstymas")); 
     JButton ascBtn = new JButton("Didėjimo tvarka"); 
     ascBtn.setName("ascBtn"); 
     JButton descBtn = new JButton("Mažėjimo tvarka"); 
     descBtn.setName("descBtn"); 
     JButton randBtn = new JButton("Atsitiktine tvarka"); 
     randBtn.setName("randBtn"); 
     elmOrderPanel.add(ascBtn); 
     elmOrderPanel.add(descBtn); 
     elmOrderPanel.add(randBtn); 
     elmOrderPanel.setBackground(Color.WHITE); 
     leftPanel.add(elmOrderPanel, BorderLayout.SOUTH); 

     TitledBorder colorBorder = BorderFactory.createTitledBorder("Spalvų reikšmės"); 
     hist.setBorder(colorBorder); 
     leftPanel.add(hist, BorderLayout.CENTER); 

     class ButtonListener implements ActionListener { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       JButton eventObj = (JButton)e.getSource(); 
       if (eventObj.getName().equalsIgnoreCase("randBtn")) { 
        resetControls(); 
        comp.generateRandomArray(); 
        comp.resetSorter(); 
        comp.startAnimation(); 
        elmOrder = RANDOM_ELEMENT_ORDER; 
       } else if (eventObj.getName().equalsIgnoreCase("descBtn")) { 
        resetControls(); 
        comp.generateDescendingArray(); 
        comp.resetSorter(); 
        comp.startAnimation(); 
        elmOrder = DESCENDING_EELMENT_ORDER; 
       } else if (eventObj.getName().equalsIgnoreCase("ascBtn")) { 
        resetControls(); 
        comp.generateAscendingArray(); 
        comp.resetSorter(); 
        comp.startAnimation(); 
        elmOrder = ASCENDING_ELEMEMENT_ORDER; 
       } 
      } 
     } 
     ButtonListener l = new ButtonListener(); 
     ascBtn.addActionListener(l); 
     descBtn.addActionListener(l); 
     randBtn.addActionListener(l); 

     sortPanel.setLayout(new BorderLayout()); 
     sortPanel.add(comp, BorderLayout.CENTER); 
     sortPanel.add(descriptionTA, BorderLayout.SOUTH); 
     sortPanel.add(leftPanel, BorderLayout.WEST); 
     add(sortPanel, BorderLayout.CENTER); 
     this.updateUI(); 
    } 

    public void setDescriptionText(String text){   
     descriptionTA.setText(text);  
    } 
} 

的setDescriptionText會從管理中的JTextArea面板上面的動畫,並設置在它的文本諾特爾線程調用。

怪異的事情對我來說,它可以完美兼容比Java 7

舊版本如果你需要更多的代碼來檢測問題,請讓我知道。

+2

那種你所看到的行爲和事實,即你的類名爲SortAnimationPanel導致我認爲這是一個線程問題。確保每個對swing組件的訪問都是從事件派發線程完成的,而不是從背景動畫線程中完成 – 2013-05-13 14:37:55

+0

是否有您的代碼拋出的任何異常?或者它只是停止? – 2013-05-13 14:39:26

+4

請將代碼縮小到所需的最小值。只要問題仍然存在,就儘量減少代碼。確保從EDT調用setDescriptionText。 – 2013-05-13 14:40:50

回答

2

您應該確保descriptionTA.setText()被調用的EventDispatchedThread:

public void setDescriptionText(String text){  
    Runnable toEDT = new Runnable() { 

     @Override 
     public void run() { 
      descriptionTA.setText(text);  
     } 
    }; 

    if(SwingUtilities.isEventDispatchThread()) { 
     toEDT.run(); 
    }else { 
     SwingUtilities.invokeLater(toEDT); 
    } 
} 

所有UI相關的操作(instanciation,修改等)應在美國東部時間,以避免死鎖或其他有趣的事情。

+2

它在Java <7中意外工作。就像在180公里/小時的小路上騎車一樣,不會總是導致事故。 – 2013-05-13 15:02:30

+0

我不知道,我想沒人能解釋。這是線程相關的東西,很難調試和理解。只需申請一下:從不做與美國東部時間以外的用戶界面相關的東西。 – 2013-05-13 15:05:43

2

Swing有一個專門用於更新其GUI的線程。所以,如果你想更新你的界面,不調用任何其他線程,嘗試用預留的方法來更新你的界面:

SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
     // Here, we can safely update the GUI 
     // because we'll be called from the 
     // event dispatch thread 
     statusLabel.setText("Query: " + queryNo); 
    } 
}); 
相關問題