Java 在每个JTextArea中显示迭代

Java 在每个JTextArea中显示迭代,java,swing,sorting,actionlistener,jtextarea,Java,Swing,Sorting,Actionlistener,Jtextarea,我得到了5个代表排序输出的JTextareas,我需要关于在每个JTextarea中显示或追加迭代的帮助,以显示排序算法 我得到了{5,3,9,7,1,8}的值 JTextArea1|3,5,7,1,8,9 | JTextArea 2|3,5,1,7,8,9 | JTextArea3 | 3、5、1、7、8、9 | JTextArea4|1,3,5,7,8,9 | JTextArea5 | 1,3,5,7,8,9 | 我的问题是如何在每个文本区域中附加这些值 我的代码太长了,很抱歉 我的代码还没

我得到了5个代表排序输出的
JTextarea
s,我需要关于在每个
JTextarea
中显示或追加迭代的帮助,以显示排序算法

我得到了{5,3,9,7,1,8}的值

JTextArea1|3,5,7,1,8,9 |
JTextArea 2|3,5,1,7,8,9 |
JTextArea3 | 3、5、1、7、8、9 |
JTextArea4|1,3,5,7,8,9 |
JTextArea5 | 1,3,5,7,8,9 |

我的问题是如何在每个文本区域中附加这些值

我的代码太长了,很抱歉

我的代码还没有完成。我刚刚按了
ascbuble
的按钮,但它可以运行

//importing needed packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class Sorting extends JPanel
{

    // String needed to contain the values then convert into int
     int int1,int2,int3,int4,int5,temp;

     String str1,str2,str3,str4,str5;

    //Buttons needed 
    JButton ascbubble,descbubble,
            ascballoon,descballoon,
            clear;

    //Output Area of the result sorting
    JTextArea   output1,output2,
                output3,output4,
                output5;

    //Text Field for user to input numbers
    JTextField  input1,input2,
                input3,input4,
                input5;



    Sorting()
    {
        //set color of the background
        setBackground(Color.black);

        //initialize JButton,JTextArea and JTextField       
        //JButton
        ascbubble = new JButton("Ascending Bubble");
        descbubble = new JButton("Descending Bubble");
        ascballoon = new JButton("Ascending Balloon");
        descballoon = new JButton("Descending Balloon");
        clear = new JButton("Clear");

        //JTextArea
        output1 = new JTextArea(" ");
        output2 = new JTextArea(" ");
        output3 = new JTextArea(" ");
        output4 = new JTextArea(" ");
        output5 = new JTextArea(" ");

        //JTextField
        input1 = new JTextField("00");
        input2 = new JTextField("00");
        input3 = new JTextField("00");
        input4 = new JTextField("00");
        input5 = new JTextField("00");

        //SetBounds
        setLayout(null);
        input1.setBounds(120, 50, 30, 20);
        input2.setBounds(170, 50, 30, 20);
        input3.setBounds(220, 50, 30, 20);
        input4.setBounds(270, 50, 30, 20);
        input5.setBounds(320, 50, 30, 20);

        ascbubble.setBounds(50, 120, 150, 40);
        descbubble.setBounds(50, 180, 150, 40);
        clear.setBounds(210, 140, 100, 50);
        ascballoon.setBounds(320, 120, 150, 40);
        descballoon.setBounds(320, 180, 150, 40);

        output1.setBounds(20, 300, 80, 100);
        output2.setBounds(120, 300, 80, 100);
        output3.setBounds(220, 300, 80, 100);
        output4.setBounds(320, 300, 80, 100);
        output5.setBounds(420, 300, 80, 100);   


        //add function to the buttons
        thehandler handler = new thehandler();
        ascbubble.addActionListener(handler);
        descbubble.addActionListener(handler);
        ascballoon.addActionListener(handler);
        descballoon.addActionListener(handler);

        //add to the frame
        add(input1);
        add(input2);
        add(input3);
        add(input4);
        add(input5);

        add(output1);
        add(output2);
        add(output3);
        add(output4);
        add(output5);

        add(ascbubble);
        add(descbubble);
        add(ascballoon);
        add(descballoon);
        add(clear);

    }


        private class thehandler implements ActionListener
        {

            public void actionPerformed(ActionEvent e)
            {


                if(e.getSource()==ascbubble)
                {

                    //input1
                    str1=input1.getText();
                    int1=Integer.parseInt(str1);

                    //input2
                    str2=input2.getText();
                    int2=Integer.parseInt(str2);

                    //input3
                    str3=input3.getText();
                    int3=Integer.parseInt(str3);

                    //input4
                    str4=input4.getText();
                    int4=Integer.parseInt(str4);

                    //input5
                    str5=input5.getText();
                    int5=Integer.parseInt(str5);

                    int contain[]={int1,int2,int3,int4,int5};

                     //formula for Buble Sort Ascending Order                                            
                         for ( int pass = 1; pass < contain.length; pass++ )
                         {
                            for ( int i = 0; i < contain.length - pass; i++ )
                            {
                                    if ( contain[ i ] > contain[ i + 1 ] ) 
                                    {
                                            temp = contain[ i ];
                                            contain[ i ] = contain[ i + 1 ];
                                            contain[ i + 1 ] = temp;
                                    }


                            }


                         }





                }

            }

        }






    public static void main(String[]args)
    {
        JFrame frame = new JFrame("Sorting");
        frame.add(new Sorting());
        frame.setSize(550, 500);
        frame.setVisible(true);


    }

}
//导入所需的包
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类排序扩展了JPanel
{
//需要包含值的字符串,然后将其转换为int
int int1、int2、int3、int4、int5、温度;
字符串str1、str2、str3、str4、str5;
//需要按钮
JButton ascbuble,descbbuble,
气球,气球,
清楚的
//结果排序的输出区域
JTextArea输出1,输出2,
输出3,输出4,
输出5;
//用于用户输入数字的文本字段
JTextField输入1,输入2,
输入3,输入4,
输入5;
排序()
{
//设置背景的颜色
挫折背景(颜色:黑色);
//初始化JButton、JTextArea和JTextField
//钮扣
ascbuble=新的JButton(“上升气泡”);
descbubble=新按钮(“下降气泡”);
ascballoon=新按钮(“升空气球”);
descballoon=新按钮(“下降气球”);
清除=新的按钮(“清除”);
//JTextArea
output1=新的JTextArea(“”);
output2=新的JTextArea(“”);
output3=新的JTextArea(“”);
output4=新的JTextArea(“”);
output5=新的JTextArea(“”);
//JTextField
input1=新的JTextField(“00”);
input2=新的JTextField(“00”);
input3=新的JTextField(“00”);
input4=新的JTextField(“00”);
input5=新的JTextField(“00”);
//挫折
setLayout(空);
输入1.立根(120,50,30,20);
输入2.立根(170,50,30,20);
输入3.立根(220,50,30,20);
输入4.立根(270,50,30,20);
输入5.立根(320,50,30,20);
Ascbuble.backbounds(50,120,150,40);
下凹点(50、180、150、40);
清除。后退(210、140、100、50);
Ascbloon.立根(320、120、150、40);
后退(320、180、150、40);
输出1.挫折(20300,80100);
输出2.挫折(12030080100);
输出3.挫折(220、300、80、100);
输出4.后退(32030080100);
输出5.挫折(420、300、80、100);
//为按钮添加功能
thehandler=新的thehandler();
ascbuble.addActionListener(处理程序);
addActionListener(处理程序);
ascballow.addActionListener(处理程序);
addActionListener(处理程序);
//添加到框架中
添加(输入1);
添加(输入2);
添加(输入3);
添加(输入4);
添加(输入5);
添加(输出1);
添加(输出2);
添加(输出3);
添加(输出4);
添加(输出5);
添加(ascbuble);
添加(气泡);
添加(ascbloon);
添加(描述气球);
添加(清除);
}
私有类处理程序实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
如果(如getSource()==ascbubble)
{
//输入1
str1=input1.getText();
int1=整数.parseInt(str1);
//输入2
str2=input2.getText();
int2=整数.parseInt(str2);
//输入3
str3=input3.getText();
int3=整数.parseInt(str3);
//输入4
str4=input4.getText();
int4=Integer.parseInt(str4);
//输入5
str5=input5.getText();
int5=Integer.parseInt(str5);
int contain[]={int1,int2,int3,int4,int5};
//Buble排序升序公式
for(int pass=1;pass包含[i+1])
{
温度=包含[i];
包含[i]=包含[i+1];
包含[i+1]=温度;
}
}
}
}
}
}
公共静态void main(字符串[]args)
{
JFrame=新JFrame(“排序”);
frame.add(新排序());
框架设置尺寸(550500);
frame.setVisible(true);
}
}

首先,
JTextArea
有一个简单的
append
方法,所以您只需要一个。每次传递时,只需生成一个新的
字符串
,它表示数组的当前状态


如果你想实时完成,我建议使用SwingWorker,而且,JTextArea有一个很好的附加方法。我不知道如何使用SwingWorker,但我会研究它。是的,再过10秒,我会提到它。考虑到数据集的大小,它可以在不到几毫秒的时间内进行排序,说真的,不需要后台线程进行更新
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringJoiner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Sort {

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

    public Sort() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField fieldValues;
        private JTextArea fieldResults;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(2, 2, 2, 2);

            fieldValues = new JTextField(20);
            fieldResults = new JTextArea(10, 20);

            add(fieldValues, gbc);

            JButton btn = new JButton("Sort");
            add(btn, gbc);

            add(new JScrollPane(fieldResults), gbc);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String[] values = fieldValues.getText().split(",");
                    int[] contain = new int[values.length];
                    for (int index = 0; index < values.length; index++) {
                        contain[index] = Integer.parseInt(values[index].trim());
                    }
                    for (int pass = 1; pass < contain.length; pass++) {
                        for (int i = 0; i < contain.length - pass; i++) {
                            if (contain[i] > contain[i + 1]) {
                                int temp = contain[i];
                                contain[i] = contain[i + 1];
                                contain[i + 1] = temp;
                                StringJoiner sj = new StringJoiner(", ", "", "\n");
                                for (int value : contain) {
                                    sj.add(Integer.toString(value));
                                }
                                fieldResults.append(sj.toString());
                            }

                        }

                    }
                }
            });
        }

    }

}
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.StringJoiner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Sort {

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

    public Sort() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField fieldValues;
        private JTextArea fieldResults;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(2, 2, 2, 2);

            fieldValues = new JTextField(20);
            fieldResults = new JTextArea(10, 20);

            add(fieldValues, gbc);

            JButton btn = new JButton("Sort");
            add(btn, gbc);

            add(new JScrollPane(fieldResults), gbc);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    btn.setEnabled(false);
                    String text = fieldValues.getText();
                    SortWorker worker = new SortWorker(text);
                    worker.addPropertyChangeListener(new PropertyChangeListener() {
                        @Override
                        public void propertyChange(PropertyChangeEvent evt) {
                            String name = evt.getPropertyName();
                            if ("state".equals(name)) {
                                if (worker.getState() == SwingWorker.StateValue.DONE) {
                                    btn.setEnabled(true);
                                }
                            }
                        }
                    });
                    worker.execute();
                }
            });
        }

        public class SortWorker extends SwingWorker<int[], String> {

            private String text;

            public SortWorker(String text) {
                this.text = text;
            }

            @Override
            protected void process(List<String> chunks) {
                for (String value : chunks) {
                    fieldResults.append(value);
                }
            }

            @Override
            protected int[] doInBackground() throws Exception {
                String[] values = text.split(",");
                int[] contain = new int[values.length];
                for (int index = 0; index < values.length; index++) {
                    contain[index] = Integer.parseInt(values[index].trim());
                }
                for (int pass = 1; pass < contain.length; pass++) {
                    for (int i = 0; i < contain.length - pass; i++) {
                        if (contain[i] > contain[i + 1]) {
                            int temp = contain[i];
                            contain[i] = contain[i + 1];
                            contain[i + 1] = temp;
                            StringJoiner sj = new StringJoiner(", ", "", "\n");
                            for (int value : contain) {
                                sj.add(Integer.toString(value));
                            }
                            publish(sj.toString());
                        }

                    }

                }
                return contain;
            }

        }

    }

}