简体   繁体   English

如何从外部类追加JTextArea?

[英]How to append JTextArea from an outer class?

I have 3 classes, a Frame class, a Panel class and an Algorithm class. 我有3个类,分别是Frame类,Panel类和Algorithm类。 The Frame is created first and it contains the panel class, the panel then starts the algorithm with the press of a button. 首先创建框架,其中包含面板类,然后面板通过按按钮开始算法。 The panel has a JTextArea log, which the algorithm has to append. 面板上有一个JTextArea日志,算法必须附加该日志。 I tried passing the Panel, so that it can access it, but no luck. 我尝试通过面板,以便它可以访问它,但是没有运气。 Tried passing the JTextArea itself, no luck either. 试图通过JTextArea本身,也没有运气。 Even wrote a function, which the Algorithm could call to append, still nothing.Where is the catch here? 即使编写了一个函数,算法也可以调用该函数来追加,但仍然一无所获。

The code follows, hope it's not too much: PS The code contains a FileLoader class, but that just load a file. 代码如下,希望它不要太多:PS代码包含FileLoader类,但是仅加载文件。

Frame: 帧:

 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();

}
}

Panel: 面板:

    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();
}
}

The algorithm, which works fine, except doesn't append. 该算法工作正常,但没有追加。

  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;
}

}

Instead of : logScrool.add(log); 代替: logScrool.add(log); use the following line to add log to JScrollPane : 使用以下行将日志添加到JScrollPane
logScrool.setViewportView(log);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM