完成一個ping的GUI界面的小程序:
???? ?新建窗口,設(shè)置窗口的大小。添加Label控件,設(shè)置字體和調(diào)整控件所處的位置。添加文本域,設(shè)置文本域的大小和位置,用來顯示輸出信息。添加文本控件,設(shè)置文本控件的大小和位置,用來輸入IP地址。添加Button控件,設(shè)置Button控件的大小和位置,編寫B(tài)utton控件的監(jiān)聽事件,用來獲取文本框輸入的IP地址,然后調(diào)用ping函數(shù),用來pingIP地址。
frame = new JFrame("PING");
frame.setBounds(300, 400, 600, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("IP地址");
lblNewLabel.setBounds(14, 573, 100, 50);
lblNewLabel.setFont(new Font("宋體",Font.PLAIN,20));
frame.getContentPane().add(lblNewLabel);//添加Label控件
textField = new JTextField();
textField.setBounds(104, 580, 300, 40);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("運行");
btnNewButton.setBounds(439, 578, 113, 40);
btnNewButton.setFont(new Font("宋體",Font.PLAIN,20));
frame.getContentPane().add(btnNewButton);
scrollPane.setBounds(0, 0, 582, 515);
frame.getContentPane().add(scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
btnNewButton.addActionListener(new ActionListener(){//Button的監(jiān)聽事件
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String content=textField.getText();
new Thread(new Runnable() {
@Override
public void run() {
Ping.ping02(textArea, content);
}
}).start();;
}
