我使用这个java控制台示例https://commons.apache.org/proper/commons-net/examples/telnet/WeatherTelnet.java来处理telnet服务器。我需要带有命令输入文本字段的框架(jTextField)和带命令和服务器响应的区域(JTextArea)。我有Swing UI,但无法将WeatherTelnet.java与它集成。
IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(), System.in, System.out);
我想我应该将System.out重定向到JTextArea,将jTextField重定向到System.in。请帮帮我吧。
Swing UI
public class ClientLauncher {
String appName = "Simple Telnet Client";
JFrame frame = new JFrame(appName);
JTextField textField;
JTextArea textArea;
JFrame preFrame;
public void display() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridBagLayout());
textField = new JTextField(30);
textField.requestFocusInWindow();
textField.addActionListener(new TextFieldListener());
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("Serif", Font.PLAIN, 15));
textArea.setLineWrap(true);
//MessageConsole mc = new MessageConsole(textComponent);
mainPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.LINE_START;
left.fill = GridBagConstraints.HORIZONTAL;
left.weightx = 512.0D;
left.weighty = 1.0D;
GridBagConstraints right = new GridBagConstraints();
right.insets = new Insets(0, 10, 0, 0);
right.anchor = GridBagConstraints.LINE_END;
right.fill = GridBagConstraints.NONE;
right.weightx = 1.0D;
right.weighty = 1.0D;
southPanel.add(textField, left);
mainPanel.add(BorderLayout.SOUTH, southPanel);
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 300);
frame.setVisible(true);
}
class TextFieldListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (textField.getText().length() > 1) {
textArea.append(textField.getText() + "\n");
textField.setText("");
textField.requestFocusInWindow();
}
}
}
public static void main(String[] args) {
ClientLauncher clientView = new ClientLauncher();
clientView.display();
}
}