안녕하세요 루프를 사용하여 대기열 시뮬레이션을 만들었고 사용자가 실행 버튼을 클릭했을 때 GUI에 문제가있는 경우 GUI를 만들었습니다. 몇 초 만에 10-15 초 정도의 출력이 나오면 전체 출력이 나오지 않습니다. JTextArea에 보여줍니다. 콘솔에서와 같이 jtextarea에 추가되는 모든 것을 어떻게 출력합니까? 나는 SwingWorker라는 것을 찾아 보았다. 그러나 그것을 사용하는 방법을 모릅니다. 제발 도와주세요.JTextArea 실시간 출력
package Windows;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Station implements ActionListener
{
private JTextArea userinput, useroutput;
private JScrollPane sbr_userinput, sbr_useroutput;
private JButton runButton, clearButton, homeButton;
//LinkedList Customer Queue created here.
public static Queue<String> line = new LinkedList<String>();
private static String time; //time variable.
private static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //DateFormat variable.
private int intervals;
private int cashiers;
private int processing_time;
public static void main(String[] args)
{
new Station();
}
public Station()
{
JFrame frame = new JFrame("[=] Train Station Simulation [=]");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 400);
frame.setContentPane(GUI());
frame.setVisible(true);
}
public Container GUI()
{
JPanel totalGUI = new JPanel();
totalGUI.setLayout(new GridLayout(1, 2, 3, 3));
JPanel lPanel = new JPanel();
lPanel.setLayout(new GridLayout(2, 1, 3 , 3));
totalGUI.add(lPanel);
JPanel rPanel = new JPanel(new GridLayout(3, 1, 3 , 3));
totalGUI.add(rPanel);
userinput = new JTextArea("Welcome to Train Station Queue Simulation!!!" + "\n" +
"Enter the number of cashiers available HERE!!!!:" + "\n");
userinput.setEditable(true);
userinput.setLineWrap(true);
userinput.setWrapStyleWord(true);
lPanel.add(userinput);
useroutput = new JTextArea();
useroutput.setEditable(false);
useroutput.setLineWrap(true);
useroutput.setWrapStyleWord(true);
lPanel.add(useroutput);
sbr_userinput = new JScrollPane(userinput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sbr_userinput.setPreferredSize(new Dimension(300, 300));
lPanel.add(sbr_userinput);
sbr_useroutput = new JScrollPane(useroutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sbr_useroutput.setPreferredSize(new Dimension(300, 300));
lPanel.add(sbr_useroutput);
runButton = new JButton("RUN");
runButton.addActionListener(this);
rPanel.add(runButton);
clearButton = new JButton("CLEAR");
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
userinput.setText("");
useroutput.setText("");
System.out.println("cleared");
}
});
rPanel.add(clearButton);
homeButton = new JButton("HOME");
rPanel.add(homeButton);
totalGUI.setOpaque(true);
return totalGUI;
}
public void actionPerformed(ActionEvent e)
{
cashiers = Integer.parseInt(userinput.getText());
if (e.getSource() == this.runButton)
{
useroutput.append("CUSTOMERS ARE COMING !!!! !!!!" + "\n" + "\n");;
//Array of all the customer that will enter the queue.
String list[] = {"Naqi", "Monty", "Mohin", "Yasmin", "Maighjoo", "Ashish", "Paal", "Kevin", "Ruhail", "Tony"};
//2nd ArrayList which customer are added to and removed later on so no duplicates arise.
ArrayList<String> customer = new ArrayList<String>(Arrays.asList(list));
int array_customer_list = list.length; //Recording the number of customers in the array.
//While statement containing for loop add customers to the empty LinkedList object.
while (line.isEmpty())
{
for (int x = 0; x < array_customer_list; x++)
{
try
{
Thread.sleep(ran_interval() * 1000); //Sleep method to hold the arrival time by 1-2 seconds.
int cus = (int) (Math.random() * customer.size()); //Random customer is picked here.
String new_cus = customer.get(cus); //New customer object is created ere.
line.add(new_cus); //Customer objects are added to the empty LinkedList queue.
customer.remove(cus);
//For loop statement to outputting the queue.
for (String s : line)
{
useroutput.append("[" + s.toString() + " " + "]" + "\n");; //Outputting each customer and using the ".name" method so customers are readable.
}
//Outputting the whole queue and stating who has joined the queue.
useroutput.append("\n" + "The queue has " + line.size() + " customers so far" + "\n" +
new_cus.toString() + " Has Joined the Queue " + " <=== WAITING" + "\n" + "\n");
}
catch(Exception a) //ERROR handler for sleep method.
{
System.out.println("Intervals error: " + e); //Outputting the ERROR message.
System.exit(0); //If ERROR found exit system.
}
}
}
userinput.append("\n");
useroutput.append("CUSTOMERS ARE WAITING !!!! !!!!" + "\n" + "\n");
useroutput.append("Processing START !!!!" + "\n" + "\n");
while (!line.isEmpty()) //While statement with for loop to remove each customer from LinkedList queue.
{
try
{
String cus = line.remove(); //Method to remove customer from LinkedList queue.
String time = getTime();
Thread.sleep((processing_time() * 1000)/cashiers); //Sleep method to hold the processing by 1-3 seconds.
for (String s : line)
{
useroutput.append("[" + s.toString() + " " + "]" + "\n"); //Outputting each customer and using the ".name" method so customers are readable.
}
//Outputting the whole queue and stating who has joined the queue.
useroutput.append("\n" + "The queue has " + line.size() + " customers left" + "\n" +
cus.toString()+ " waited for " + time + " <=== SERVED" + "\n" + "\n");
}
catch(Exception a) //ERROR handler for sleep method.
{
System.out.println("Cashiers_wait error: " + e); //Outputting the ERROR message.
System.exit(0); //If ERROR found exit system.
}
}
}
useroutput.append("Processing FINISHED !!!!" + "\n");
System.out.println("working");
}
static String getTime() //Time Constructor
{
Calendar cal = Calendar.getInstance();
time = dateFormat.format(cal.getTime()); //Getting the current system time.
return time; //Return time.
}
public int ran_interval()
{
Random rand = new Random(); //Random object created here.
int interval = this.intervals = rand.nextInt(2) + 1; //Random number between 1-2 is generated for customer arrival here.
return interval;
}
public int processing_time()
{
Random ran = new Random(); //Random object created here.
int time = this.processing_time = ran.nextInt(4) + 1; //Random number between 1-3 is generated for customer arrival here.
return time;
}
}
에 대한 Concurrency in Swing에서 봐 존재하는 경우 EDT의 컨텍스트 내에서 호출, 즉, 모든 GUI를 제어하는 EDT (이벤트 발송 쓰레드)입니다 그래서 UR 프로그램은 얼어 버릴 것입니다, http://www.javacreed.com/swing-worker-example/ 여기에 간단한 예나 사용법이 있습니다 만, 기본적으로 U가 원하는 작업을 수행하는 또 다른 스레드입니다. 때 f inish는 관찰자 패턴처럼 통지한다. – nachokk
'나는 SwingWorker라고 불리는 것을 찾아 보았습니다. Swing 튜토리얼에 예제가 있습니다. 포럼에 예제가 있습니다. – camickr
분명히 여기서 도움이 충분하지 않습니다. 이제 질문은 crossposted입니다 : http://www.java-forums.org/awt-swing/76259-real-time-console-output-jtextarea.html – camickr