2009-07-11 4 views
-1

Le JScrollpane n'est pas visible au ... Quelqu'un peut-il trouver ce qui ne va pas dans mon code?
Et comment y remédier?Problème JScrollPane

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 
import java.text.DateFormat; 



class PrimaryInterface extends JFrame implements ActionListener 
{ 
     ImageIcon iimages; 
     JLabel jlimageContainer,jltime,jltitle,jlname1,jlname2; 
     JLabel jlnoValue; 
     JLabel jlnumberOfThreads, jlnameOfthreads,jlstatusOfThreads, jllane1; 
     JButton jbtstartSlideShow,jbtstopSlideShow; 
     Font ftimeFont,ftitleFont,fnameFont; 
     JProgressBar progbarMain,progbarLock; 
     JProgressBar progbarThread[] = new JProgressBar[10]; 
     JButton buttonGo; 
     JButton buttonStart; 
     JButton buttonStop; 
     JPanel panelMain,panelLocker,progPanel,sample;   
     JTextArea textArea; 
     JScrollPane scrPane; 
     JRadioButton radioButton[] = new JRadioButton[10]; 
     ButtonGroup buttonGroup; 
     int threadCount=1,targetThread=0; 
     firstTester ft[] = new firstTester[10]; 
     secondTester st[] = new secondTester[10]; 
     firstLocker first[] = new firstLocker[10]; 




     PrimaryInterface() 
     { 

       super("SLIDE SHOW"); 
       setLayout(null); 
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

     } 

     public void paintImageContainer() 
     { 

       jlname1= new JLabel("THREAD 1"); 
       jlname2=new JLabel("THREAD 2"); 
       jltitle = new JLabel("DEADLOCK APPLICATION",JLabel.CENTER); 
       jltitle.setBounds(350,5,300,50); 

       jlimageContainer = new JLabel();   
       jltime = new JLabel(); 
       jbtstartSlideShow = new JButton("StartShow");     
       jbtstopSlideShow = new JButton("StopShow"); 
       jlnoValue = new JLabel();   

       ftimeFont = new Font("Arial",1,40); 
       fnameFont = new Font("Arial",1,16); 
       ftitleFont = new Font("Constantia",1,25);   

       jbtstartSlideShow.setBounds(40,300,100,25); 
       jbtstopSlideShow.setBounds(220,300,100,25); 



       jlimageContainer.setBounds(80,80,200,200); 
       jlimageContainer.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE)); 
       jlimageContainer.setText("SLIDESHOW AREA"); 
       jltime.setBounds(500,160,300,100); 
       jltime.setForeground(Color.blue); 
       jlname1.setFont(fnameFont); 
       jlname2.setFont(fnameFont); 
       jltime.setFont(ftimeFont); 
       jltitle.setFont(ftitleFont); 
       jlname1.setBounds(10,30,100,30); 
       jlname2.setBounds(500,130,100,30); 
       jltitle.setForeground(Color.red); 
       jlimageContainer.setText("SLIDESHOW AREA"); 

       textArea = new JTextArea(180, 30); 
    scrPane=new JScrollPane(textArea); 


       new DigitalWatch(jltime,jlnoValue); 
       new SlideShow(iimages,jlimageContainer,jbtstartSlideShow,jbtstopSlideShow,jlnoValue); 

    jlnumberOfThreads = new JLabel("Number of Threads:"); 
       jlnameOfthreads = new JLabel(); 
       jlstatusOfThreads = new JLabel("Thread Status:"); 
       jllane1 = new JLabel(); 


       jllane1.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE)); 
       jllane1.setBounds(20,340,830,4); 

       jlnameOfthreads.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE)); 
       jlnameOfthreads.setBounds(15,560,830,140); 


       jlnumberOfThreads.setBounds(20,560,200,20); 
       jlnoValue.setBounds(250,560,200,20); 

       jlstatusOfThreads.setBounds(20,580,200,20); 



       panelMain = new JPanel(new GridLayout(1,1)); 
       panelLocker = new JPanel(new GridLayout(1,1)); 
       progPanel = new JPanel(new GridLayout(10,2)); 





       //panelMain.setLayout(null); 
       //panelLocker.setLayout(null);     



       panelMain.setBounds(500,260,180,30); 
       panelLocker.setBounds(20,400,180,30); 
       progPanel.setBounds(250,350,300,200); 



       buttonStart = new JButton("START"); 
       buttonStop = new JButton("DEADLOCK"); 

       buttonGroup = new ButtonGroup(); 
       progbarMain = new JProgressBar();     


       //panelMain.setLayout(null); 
       //panelLocker.setLayout(null); 

       buttonStart.setBounds(600,400,100,30); 
       buttonStop.setBounds(600,450,100,30); 
       textArea.setBounds(50,600,750,80); 
       textArea.setLineWrap(true); 

       panelMain.add(progbarMain); 

       progbarMain.setIndeterminate(true); 
       progbarMain.setStringPainted(true); 
       progbarMain.setString("Main Thread"); 


       progbarLock = new JProgressBar();     
       progbarLock.setIndeterminate(true); 
       progbarLock.setStringPainted(true); 
       progbarLock.setString("DeadLock Thread"); 
       panelLocker.add(progbarLock); 



       scrPane.setAutoscrolls(true); 





       buttonStop.setEnabled(false); 



       //setContentPane(panelMain); 

       //new PrimaryInterface(); 



       add(buttonStart); 
       add(buttonStop); 

       add(textArea); 

       buttonStart.addActionListener(this); 
       buttonStop.addActionListener(this); 





       add(jlname1); 
       add(jlname2); 
       add(jltitle); 
       add(jlimageContainer); 
       add(jbtstartSlideShow); 
       add(jbtstopSlideShow); 
       add(jltime);     
       add(jlnumberOfThreads); 
       add(jlnameOfthreads); 
       add(jllane1); 
       add(jlstatusOfThreads); 
       add(jlnoValue); 


       add(progPanel); 
       add(panelMain); 
       add(panelLocker); 


       setSize(900,730); 
       setVisible(true); 
       this.getContentPane().setBackground(Color.yellow.darker()); 

     } 
public void createProgress(int tNum) 
     { 

       radioButton[tNum] = new JRadioButton("Thread "+String.valueOf(tNum)); 
       buttonGroup.add(radioButton[tNum]); 
       progbarThread[tNum] = new JProgressBar(); 
       if(tNum!=0) 
       { 
       progPanel.add(radioButton[tNum]);   
       progPanel.add(progbarThread[tNum]); 
       } 
       radioButton[tNum].addActionListener(this); 

       buttonStop.setEnabled(false); 
       progbarThread[tNum].setIndeterminate(true); 
       progbarThread[tNum].setStringPainted(true); 
       progbarThread[tNum].setString("Thread "+tNum); 
     } 

     public void actionPerformed(ActionEvent ae) 
     { 
       String command = ae.getActionCommand(); 

       if(command.equals("START")) 
       {   

         textArea.append("Creating a new thread : Thread "+threadCount+"\n"); 
         ft[threadCount] = new firstTester(); 
         st[threadCount] = new secondTester(); 

         first[threadCount] = new firstLocker(String.valueOf(threadCount),ft[threadCount],st[threadCount],false,this); 
         threadCount++; 

       } 

       if(command.equals("DEADLOCK")) 
       { 
         textArea.append("Creating Deadlock thread : Thread 0\n"); 
         progbarThread[targetThread].setIndeterminate(false); 
         progbarThread[targetThread].setValue(0); 
         progbarLock.setIndeterminate(false); 
         progbarLock.setValue(0); 

         first[targetThread] = new firstLocker("0",ft[targetThread],st[targetThread],true,this); 
         secondLocker second = new secondLocker("0",ft[targetThread],st[targetThread],this); 
         radioButton[targetThread].setEnabled(false); 
         progbarThread[targetThread].setString("Thread "+targetThread+" in deadlock"); 
       }   

       for(int actionCounter = 0;actionCounter<=10;actionCounter++) 
       { 
         if(command.equals("Thread "+actionCounter)) 
         { 
           targetThread = actionCounter; 
           textArea.append("Thread "+actionCounter+" selected.\n"); 
           buttonStop.setEnabled(true); 
           progbarLock.setIndeterminate(true); 
           progbarLock.setString("DeadLock Thread"); 
         } 
       }   

     } 

} 


class SlideShow implements Runnable,ActionListener 
{   

     ImageIcon iimages; 
     JLabel jlimageContainer,jlnoValue; 
     JButton jbtstartSlideShow,jbtstopSlideShow; 

     Thread t1; 
     String path="h:\\images"; 

     int i = 0; 
     int j = 0; 



     SlideShow(ImageIcon images,JLabel imageContainer,JButton startSlideShow,JButton stopSlideShow, JLabel noValue) 
     { 

       iimages = images; 
       jlimageContainer = imageContainer; 
       jlnoValue = noValue; 
       jbtstartSlideShow = startSlideShow; 
       jbtstopSlideShow = stopSlideShow; 

       jbtstartSlideShow.addActionListener(this); 
       jbtstopSlideShow.addActionListener(this); 

     } 





     public void actionPerformed(ActionEvent ae) 
     { 
       if(ae.getSource() == jbtstartSlideShow) 
       { 
         t1 = new Thread(this); 
         t1.start(); 

         jbtstartSlideShow.setEnabled(false); 
       } 

       if(ae.getSource() == jbtstopSlideShow) 
       { 
         t1.stop(); 
         jbtstartSlideShow.setEnabled(true); 
       } 
     } 

     public void startSlideShow() 
     { 

       try 
       { 
         for(i=1;i<=5;i++) 
         { 
           iimages = new ImageIcon(path+"\\"+i+".jpg"); 
           jlimageContainer.setIcon(iimages); 
           t1.sleep(2000); 

         } 
       } 
       catch(Exception e) 
       { 
       }   
     } 

     public void run() 
     { 
         jlnoValue.setText((1+(Thread.currentThread().activeCount()-4)/2.0)+""); 
         jlimageContainer.setText("LOADING..."); 


         for(j=1;j<=2;j++) 
         { 

           startSlideShow();   

           if(j==2) 
             j=0; 
         }               


     } 

} 

class DigitalWatch implements Runnable 
{ 

     static Thread t2; 
     JLabel labelTime,jlnoValue; 
     String sformattedTime=""; 


     DigitalWatch(JLabel objL,JLabel noValue) 
     { 
       labelTime = objL;   
       jlnoValue = noValue; 
       t2 = new Thread(this);   

     } 

     public static void showWatch() 
     { 
       t2.start(); 


     } 

     public void run() 
     { 
       jlnoValue.setText("1");     
       for(;;) 
       { 
         Date rawTime=new Date(); 
         sformattedTime = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(rawTime); 

         labelTime.setText(sformattedTime); 

         System.out.println(sformattedTime);         

         try{ 
           t2.sleep(1000); 
         } 
         catch(Exception e) 
         { 
         } 
       } 

     } 

} 



//Synchronised Test method 1 
class firstTester{ 

     static int fCount; 
     boolean flagLock; 
     firstTester() 
     { 

       System.out.println("Test Class 1 started"); 
     } 
     synchronized void test1(int tNum,secondTester second,boolean flagLocker,PrimaryInterface pInterface) 
     { 
       //pInterface.textArea.append("Thread :"+tNum+" count :"+fCount+"\n"); 
       flagLock = flagLocker; 
       try{ 

       Thread.sleep(150); 
       }catch(Exception e){} 
       fCount++; 

       if(flagLock) 
       { 
         pInterface.textArea.append("Creating Dead lock...."); 
         second.test2(tNum,this,flagLock,pInterface); 
       } 

     } 
} 


//Synchronised Test method 2 

class secondTester{ 

     boolean flagLock; 
     secondTester() 
     { 

       System.out.println("Test Class 2 started"); 
     } 

     synchronized void test2(int tNum,firstTester first,boolean flagLocker,PrimaryInterface pInterface) 
     { 
       flagLock = flagLocker; 
       try{ 
       Thread.sleep(100); 
       }catch(Exception e){} 

       if(flagLock) 
       { 
         first.test1(tNum,this,flagLock,pInterface); 
       } 

     } 
} 



class firstLocker implements Runnable{ 
     Thread t; 
     String tName; 
     int tNum; 
     firstTester first; 
     secondTester second; 
     boolean flagReady = false; 
     PrimaryInterface pInterface; 
     firstLocker(String name,firstTester first,secondTester second,boolean flagLock,PrimaryInterface pInterface) 
     { 
       flagReady = flagLock; 
       System.out.println("Starting Thread"+name); 
       this.first = first; 
       this.second = second; 
       this.pInterface = pInterface; 
       tName = name; 
       tNum = Integer.parseInt(tName); 

         pInterface.createProgress(tNum); 


       t = new Thread(this); 
       t.start(); 
     } 

     public void run() 
     { 
       while(true) 
       { 

         first.test1(tNum,second,flagReady,pInterface); 
         second.test2(tNum,first,flagReady,pInterface); 
       } 
     } 
} 


class secondLocker implements Runnable{ 
     Thread t; 
     String tName; 
     int tNum; 
     firstTester first; 
     secondTester second; 
     PrimaryInterface pInterface; 
     secondLocker(String name,firstTester first,secondTester second,PrimaryInterface pInterface) 
     { 
       System.out.println("Starting Thread"+name); 
       this.first = first; 
       this.second = second; 
       this.pInterface = pInterface; 
       tName = name; 
       tNum = Integer.parseInt(tName); 

       t = new Thread(this); 
       t.start(); 
     } 

     public void run() 
     { 
       second.test2(tNum,first,true,pInterface); 
     } 
} 


class Caution 
{   

     public static void main(String args[]) 
     { 

       PrimaryInterface pInterface = new PrimaryInterface(); 


       pInterface.paintImageContainer(); 
       DigitalWatch.showWatch(); 

     } 

} 

Répondre

2

Trop long; n'a pas lu! :-)

Vous semblez ajouter la zone de texte au conteneur parent. Vous devriez ajouterez la scrollpane place:

Vous avez:

add(textArea); 

Essayez

add(new JScrollPane(textArea)); 

Conseils pour résoudre ce genre de problèmes:

Créer la classe de test le plus petit possible, vous pouvez cela démontre le problème. Quelque chose comme:

public class TestFrame { 
    public static void main(String args[]) { 
     JFrame frame = new JFrame("Test"); 

     // add some stuff to the frame 
     frame.add(...); 

     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

Souvent en créant une telle classe de test minimale, je trébuche sur la solution. Mais en tout cas, c'est beaucoup pour nous de repérer le problème en petit code.

+0

Des conseils judicieux. – banjollity

3

Vous instanciez correctement JTextArea et le JScrollPane et les liez correctement, mais vous ajoutez ensuite JTextArea directement à l'PrimaryInterface. Essayez d'ajouter le JScrollPane à la place.

En bref, il devrait ressembler à ceci:

JTextArea textArea = new JTextArea(); 
JScrollPane scrollPane = new JScrollPane(); 
scrollPane.setViewportView(textArea); 
add(scrollPane); 

Je remarque que vous n'utilisez pas LayoutManager. Peut-être que vous devriez utiliser un LayoutManager au lieu de définir vous-même toutes les limites. Cela réduira les instances de comportement imprévisible et rendra votre interface utilisateur correcte dans des conditions telles que le cadre en cours de redimensionnement.

+0

j'ai essayé .. textarea trop disparaît dans le cas de tat .. –

1

Comme cela a été souligné, une fois que vous avez ajouté le textArea au scrPane, vous devez ajouter le scrPane au conteneur. Vous ne devriez pas oublier de mettre en page le scrPane.

Il y a deux choses que vous devez changer:

textArea.setBounds(50,600,750,80); 

devrait être:

scrPane.setBounds(50,600,750,80); 

et

add(textArea); 

devrait être:

add(scrPane); 
+0

hey grt aide !! Merci beaucoup.. –