以下代码:package Pingdemo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Ping { public static void main(String[] args) { BufferedReader reader = null; String ipAddress; try { reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please enter IP address: "); ipAddress = reader.readLine(); // run the ping command Process p = Runtime.getRuntime().exec("ping " + ipAddress); // read the output from the command BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = input.readLine()) != null) { System.out.println(line); } input.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }, 帮我改进一下,做出一个gui界面
时间: 2024-02-10 22:06:05 浏览: 123
好的,以下是针对这段代码的GUI改进示例:
```java
package Pingdemo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PingGUI extends JFrame implements ActionListener {
private final JLabel ipLabel;
private final JTextField ipTextField;
private final JTextArea resultTextArea;
private final JButton pingButton;
public PingGUI() {
super("Ping");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setResizable(false);
setLocationRelativeTo(null);
// set layout
setLayout(new BorderLayout());
// create components
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout());
ipLabel = new JLabel("IP address: ");
ipTextField = new JTextField("", 15);
inputPanel.add(ipLabel);
inputPanel.add(ipTextField);
add(inputPanel, BorderLayout.NORTH);
JPanel resultPanel = new JPanel();
resultPanel.setLayout(new BorderLayout());
resultTextArea = new JTextArea();
resultTextArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultTextArea);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
resultPanel.add(scrollPane, BorderLayout.CENTER);
add(resultPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
pingButton = new JButton("Ping");
pingButton.addActionListener(this);
buttonPanel.add(pingButton);
add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == pingButton) {
String ipAddress = ipTextField.getText().trim();
if (!ipAddress.isEmpty()) {
try {
// run the ping command
Process p = Runtime.getRuntime().exec("ping " + ipAddress);
// read the output from the command
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
resultTextArea.append(line + "\n");
}
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
PingGUI pingGUI = new PingGUI();
pingGUI.setVisible(true);
}
}
```
这个GUI界面使用了JFrame、JPanel、JLabel、JTextField、JTextArea、JScrollPane、JButton等Swing组件。用户可以在文本框中输入IP地址,然后点击按钮执行ping命令并显示结果。结果显示使用了滚动条,用户可以滚动查看所有结果。
阅读全文