2013-03-11 39 views
0

我有3個類,一個Frame類,一個Panel類和一個Algorithm類。框架首先被創建並且包含面板類,面板然後按下按鈕來啓動算法。該面板有一個JTextArea日誌,該算法必須附加。我嘗試通過面板,以便它可以訪問它,但沒有運氣。試圖通過JTextArea本身,也沒有運氣。即使寫了一個函數,算法可以調用它來追加,仍然沒有。這裏的抓點在哪裏?如何從外部類追加JTextArea?

代碼如下,希望它不是太多: P.S.該代碼包含一個FileLoader類,但只是加載一個文件。

框架:

import java.awt.BorderLayout; 


    import java.util.LinkedList; 
    import javax.swing.BorderFactory; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 
    import javax.swing.JTabbedPane; 
    import javax.swing.border.EtchedBorder; 

@SuppressWarnings("serial") 
public class ClusteringSelection extends JFrame implements Runnable{ 

    LinkedList<Record> table; 
    KMeansUI kMeansUI; 

public void run() 
{ 
    StartUI(); 
} 

public void StartUI() 
{ 
    JTabbedPane tab1=new JTabbedPane(); 

    tab1.addTab("K-Means", kMeansUI=new KMeansUI(this)); 
    tab1.addTab("Aglomerative", new JPanel()); 
    add(tab1); 
    JLabel status=new JLabel(); 
    status.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); 
    add(status, BorderLayout.SOUTH); 
    setSize(320,320); 
    setResizable(false); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setVisible(true); 
    setLocationRelativeTo(null); 
} 

@SuppressWarnings("unused") 
ClusteringSelection() 
{ 
    FileLoader loader=new FileLoader(this); 
} 

public static void main(String[] args) 
{ 
    new ClusteringSelection(); 

} 
} 

面板:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.BorderFactory; 
import javax.swing.ButtonGroup; 
import javax.swing.GroupLayout; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.border.EtchedBorder; 

@SuppressWarnings("serial") 
public class KMeansUI extends JPanel implements Runnable{ 

    ClusteringSelection mainWindow; 
    JTextArea log; 
    KMeans kMeans; 
    JTextField centroids; 

    public void UpdateLog(String text) 
    { 
     log.append(text+"\n"); 
    } 

    public void run() 
    { 
     kMeans=new KMeans(mainWindow.table, Integer.parseInt(centroids.getText()), KMeansUI.this); 
    } 

    public void StartUI() 
    { 
     centroids=new JTextField(); 
     log=new JTextArea(" "); 

     log.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); 

     JScrollPane logScrool=new JScrollPane(); 
     logScrool.add(log); 
     logScrool.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

     JButton start=new JButton("Start"); 

     start.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       run(); 
       //kMeans=new KMeans(mainWindow.table, Integer.parseInt(centroids.getText()), KMeansUI.this); 
      } 
     }); 

    JLabel cenLabel=new JLabel("Count:"); 
    JRadioButton euclede=new JRadioButton("Eucledean"); 
    JRadioButton manhattan=new JRadioButton("Manhattan"); 
    JRadioButton pearsons=new JRadioButton("Pearson's"); 

    ButtonGroup messure=new ButtonGroup(); 
    messure.add(euclede); 
    messure.add(manhattan); 
    messure.add(pearsons); 

    GroupLayout layout=new GroupLayout(this); 
    setLayout(layout); 

    layout.setAutoCreateGaps(true); 
    layout.setAutoCreateContainerGaps(true); 

    layout.setHorizontalGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
         .addGroup(layout.createSequentialGroup() 
           .addComponent(cenLabel) 
           .addComponent(centroids,20,20,20)) 
           .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
             .addComponent(euclede)) 
             .addComponent(manhattan) 
             .addComponent(pearsons) 
             .addComponent(start)) 
       .addComponent(logScrool) 

      ); 

    layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
           .addComponent(cenLabel) 
           .addComponent(centroids,20,20,20) 
           ) 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER) 
           .addGroup(layout.createSequentialGroup() 
             .addComponent(euclede) 
             .addComponent(manhattan) 
             .addComponent(pearsons) 
             .addComponent(start)))) 
       .addComponent(logScrool)); 

    setVisible(true); 
} 
KMeansUI(ClusteringSelection mainWindow) 
{ 
    this.mainWindow=mainWindow; 
    StartUI(); 
} 
} 

的算法,它工作正常,但不追加。

import java.util.LinkedList; 

public class KMeans implements Runnable{ 

    LinkedList<Record> table; 
    LinkedList<Centroid> centroidList; 
boolean clusterStop=false; 
int precision=10000000; 
int centroidCount; 
double time, iterCount=0; 
KMeansUI kMeansUI; 

public void run() 
{ 
    while(clusterStop==false) 
    { 
     UpdateRecords(centroidList); 
     UpdateClusters(); 
     iterCount++; 

     for(int i=0;i<centroidCount;i++) 
     { 
      int count=0; 
      for(int j=0;j<table.size();j++) 
      { 
       if(table.get(j).type==i) 
       { 
        count++; 
       } 
      } 
      System.out.println("Cluster "+(i+1)+" has "+count+" records."); 
      //log.append("Cluster "+(i+1)+" has "+count+" records."); 
      kMeansUI.UpdateLog("Cluster "+(i+1)+" has "+count+" records."); 
     } 
    } 
    Output(); 
} 

KMeans(LinkedList<Record> table, int centroidCount, KMeansUI kMeansUI) 
{ 
    this.kMeansUI=kMeansUI; 
    time=System.currentTimeMillis(); 
    this.table=table; 
    this.centroidCount=centroidCount; 
    CreateCentroids(); 
    run(); 
} 

public void UpdateClusters() 
{ 
    clusterStop=true; 
    for(int i=0;i<centroidList.size();i++) //staiga pa centroidiem 
    { 
     for(int j=0;j<table.get(0).values.size();j++) //staiga pa kolonnam 
     { 
      double sum=0; 
      double count=0; 
      for(int k=0;k<table.size();k++) //staiga pa rindam 
      { 
       if(centroidList.get(i).type==table.get(k).type) 
       { 
        sum+=table.get(k).values.get(j); 
        count++; 
       } 

      } 
      if(centroidList.get(i).dimVal.get(j)!=(double) Math.round(((1/count)*sum)*precision)/precision) 
      { 
       clusterStop=false; 
      } 
      centroidList.get(i).dimVal.set(j, (double) Math.round(((1/count)*sum)*precision)/precision); 

     } 

    } 
} 

public void UpdateRecords(LinkedList<Centroid> centroidList) 
{ 
    for(int i=0;i<table.size();i++) 
    { 
     table.get(i).Update(centroidList); 
    } 
} 

public void CreateCentroids() 
{ 
    centroidList=new LinkedList<Centroid>(); 
    for(int i=0;i<centroidCount;i++) 
    { 
     centroidList.add(new Centroid(table.get(0).values.size(),i)); 
    } 
} 

public void Output() 
{ 
    LinkedList<String> types=new LinkedList<String>(); 
    for(int i=0;i<table.size();i++) 
    { 
     if(!types.contains(table.get(i).realType)) 
     { 
      types.add(table.get(i).realType); 
     } 
    }  
    for(int i=0;i<centroidCount;i++) //staiga pa centroidiem 
    {  
     for(int j=0;j<types.size();j++) //staiga pa klasem 
     { 
      int count=0; 
      for(int k=0;k<table.size();k++) // staiga pa rindam 
      { 
       if(table.get(k).type==i && table.get(k).realType.equals(types.get(j))) 
       { 
         count++;  
       } 
      } 
      System.out.println("Centroid "+(i+1)+" has "+count+" of type "+types.get(j)); 
      //log.append("Centroid "+(i+1)+" has "+count+" of type "+types.get(j)); 
      kMeansUI.UpdateLog("Centroid "+(i+1)+" has "+count+" of type "+types.get(j)); 
     } 
    } 

    for(int i=0;i<centroidCount;i++) 
    { 
     int count=0; 
     for(int j=0;j<table.size();j++) 
     { 
      if(table.get(j).type==i) 
      { 
       count++; 
      } 
     } 
     System.out.println("Cluster "+i+" has "+count+" records."); 
     //log.append("Cluster "+i+" has "+count+" records."); 
     kMeansUI.UpdateLog("Cluster "+i+" has "+count+" records."); 
    } 

    time=System.currentTimeMillis()-time; 
} 

} 
+0

請包括每個班級 – amphibient 2013-03-11 18:20:36

+1

頂部的import語句希望它能幫助:) – MustSeeMelons 2013-03-11 18:27:03

+0

什麼'Centroid'和'Record'? – amphibient 2013-03-11 18:32:16

回答

3

相反的:logScrool.add(log);使用下面的行添加日誌JScrollPane
logScrool.setViewportView(log);

+0

謝謝,這工作,但請你解釋我爲什麼? – MustSeeMelons 2013-03-11 19:43:37

+0

@MesseMelons:add方法繼承自java.awt.Container,addImpl方法繼承自javax.swing.JComponent。這些應僅用於內部使用。在JScrollPane構建完成之後,您應該使用它的'JViewport'來代替。 'JViewport'提供了一個窗口或「視口」到數據源 - 例如文本文件。該數據源是JViewport視圖顯示的「可滾動客戶端」(又名數據模型)。 'setViewportView(Component view)'如果需要創建一個視口,然後設置它的視圖。 – 2013-03-11 20:00:29

+0

@MessSeeMelons:要了解更多關於'setViewportView'的信息,請看這裏:http://docs.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html#setViewportView%28java.awt.Component%29 – 2013-03-11 20:02:27