package startsWith;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FrameTest extends JFrame {
private JTextField text;
private JButton action_button;
private JButton exit_button;
public FrameTest(){
JPanel center=new JPanel();
text=new JTextField("",20);
center.add(text);
add(center,BorderLayout.CENTER);
JPanel south=new JPanel();
action_button=new JButton("验证");
exit_button=new JButton("退出");
south.add(action_button);
south.add(exit_button);
add(south,BorderLayout.SOUTH);
exit_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
action_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String input=text.getText();
//startsWith()方法用于判断字符串的前缀
if(input.startsWith("http")){
JOptionPane.showMessageDialog(null, "你想进入的是一个网站");
}else if(input.startsWith("ftp")){
JOptionPane.showMessageDialog(null, "你输入的是FTP地址");
}else {
JOptionPane.showMessageDialog(null, "请从新输入");
}
}
});
pack();
}
}