【问题标题】:How do I get the JTextArea to update with each Ping?如何让 JTextArea 随每个 Ping 更新?
【发布时间】: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();
        }
    }
}

【问题讨论】:

标签: java swing jtextarea


【解决方案1】:

您的runPing() 实现阻止了event dispatch thread,因此在完成之前不会发生GUI 更新。相反,使用SwingWorker 在后台运行命令并发布临时更新。从这个完整的example 开始,使用ProcessBuilder,下面的更改会产生所示的结果。

@Override
protected Integer doInBackground() {
    try {
        ProcessBuilder pb = new ProcessBuilder("ping", "-c 3", "example.com");
    …
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2016-05-09
    • 2016-08-26
    • 2019-08-18
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多