간단한 Ping으로 생성 된 각각의 새 문자열로 JLabel을 동적으로 변경하려고하지만 JLabel의 기존 문자열을 Ping이 들어갈 때마다 새로운 문자열로 대체하는 방법을 알아낼 수 없습니다 실행 중. 아래는 제가 지금까지 가지고있는 것입니다. 지금까지는 JLabel의 텍스트가 Ping의 실행이 끝난 후에 만 바뀝니다.Ping 결과로 동적으로 JLabel을 변경하십시오.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
public class Ping extends JFrame implements ActionListener{
private JButton runButton = new JButton("Run");
private JLabel pingResult = new JLabel("Result");
private String results;
public Ping(){
runButton.addActionListener(this);
add(pingResult, BorderLayout.CENTER);
add(runButton, BorderLayout.NORTH);
}
//Action Listener
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();
if (buttonString.equals("Run"))
{
//Execute Ping
try {
String line;
Process p = Runtime.getRuntime().exec("/sbin/ping -c 4 www.google.com");
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
results += line + "\n";
pingResult.setText(results);
//System.out.println(line);
}
input.close();
}catch (Exception err){
err.printStackTrace();
}
}else{
System.exit(0);
}
}
public static void main(String[] args) {
Ping sp = new Ping();
sp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
sp.setSize(400, 400);
sp.setVisible(true);
sp.setLayout(new BorderLayout());
}
}
또한 보조 노트로의 진행률 표시 줄이 더 오래 작업에 대 한 좋은 것 동안 최소 2 초 이상 지속될 수 작업에 마우스 포인터를 "바쁜"로 변경 추천 . –