gpt4 book ai didi

java - 在每个 JTextArea 中显示迭代

转载 作者:行者123 更新时间:2023-11-30 08:06:09 25 4
gpt4 key购买 nike

我有 5 个代表排序输出的 JTextarea 我需要有关在每个 JTextArea 中显示或附加迭代以显示排序算法的帮助。

我有值 { 5,3,9,7,1,8 }

JTextArea1 |3, 5, 7, 1, 8, 9|
JTextArea2 |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|

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

我的代码太长了,我很抱歉。

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

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


}

}

最佳答案

首先,JTextArea有一个简单的append方法,所以你只需要一个。在每次传递中,您只需要生成一个新的 String 来表示数组的当前状态

Sort

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

}

}
}
});
}

}

}

现在,问题是,数据集越大,排序所花的时间就越多,这意味着 UI 将暂停,直到排序完成

一个解决方案是使用 SwingWorker,例如...

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

}

}

}

关于java - 在每个 JTextArea 中显示迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com