【发布时间】:2017-08-07 01:25:02
【问题描述】:
我遇到的问题是,当我 ping 一个 IP 时,JTextArea 直到 ping 完成才更新。这里的目标是让 JTextArea 在每次 ping IP 时更新。这是我第一次使用 swing 库,我在网上找不到任何答案。任何帮助将不胜感激,谢谢!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
public class pingGUI
{
private JFrame mainFrame; //initializing objects
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JTextField inputIP;
private JButton pingButton;
private JTextArea textArea;
private JScrollPane scroll;
public pingGUI(){
prepareGUI();
}
public static void main(String[] args){
pingGUI window = new pingGUI();
window.showEvent();
}
private void prepareGUI(){
mainFrame = new JFrame("Ping Test");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(4, 0));
headerLabel = new JLabel("",JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setVerticalAlignment(0);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showEvent(){
headerLabel.setText("Enter a IP address to ping.");
inputIP = new JTextField(15);
pingButton = new JButton("Ping");
textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(20);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pingButton.setActionCommand("Ping");
pingButton.addActionListener(new ButtonClickListener());
controlPanel.add(pingButton);
controlPanel.add(inputIP);
mainFrame.add(scroll);
mainFrame.setVisible(true);
}
public void printIP(String s) {
textArea.append(s + "\n");
}
private class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals( "Ping" )) {
if (!inputIP.getText().isEmpty()) {
statusLabel.setText("Pinging IP Address...");
runPing("ping " + inputIP.getText());
}
else
statusLabel.setText("No IP address entered.");
}
}
}
public void runPing(String command) { //Ping user specified IP address
try {
Process p = Runtime.getRuntime().exec(command); //accepts a command and returns according to command received.
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = "";
while ((str = input.readLine()) != null) {
printIP(str);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
【问题讨论】:
-
你试过调试了吗?
-
如果有不清楚的地方,您可能应该阅读docs.oracle.com/javase/tutorial/uiswing/concurrency 和早期的挥杆教程。