我有3个类,一个Frame类,一个Panel类和一个Algorithm类。首先创建框架,它包含面板类,然后面板按下按钮启动算法。该面板有一个JTextArea日志,该算法必须附加该日志。我尝试通过Panel,以便它可以访问它,但没有运气。尝试通过JTextArea本身,也没有运气。甚至写了一个函数,算法可以调用它来追加,但仍然没有。这里有什么问题?
代码如下,希望不是太多: 附:该代码包含一个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 :(得分:3)
而不是:logScrool.add(log);
使用以下行将日志添加到JScrollPane
:
logScrool.setViewportView(log);