-1
私が抱えている問題は、IPにpingを実行すると、pingが完了するまでJTextAreaが更新されないということです。ここでの目標は、IPがpingされるたびにJTextAreaが更新されることです。これは初めてのスイングライブラリを使用していて、オンラインで回答が見つかりませんでした。どんな助けでも大変感謝します。ありがとう!各PingでJTextAreaを更新するにはどうすればよいですか?
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();
}
}
}
あなたがそれをデバッグしようとしたがありますか? –
https://stackoverflow.com/questions/13393167/update-content-of-jpanel-on-a-frame-on-button-click-in-another-frame –
https:// docsをよく読んでください。 oracle.com/javase/tutorial/uiswing/concurrency/と、以前のスイング・チュートリアルがあります。 – pvg