繁体   English   中英

如何在Java中将字符串显示为URI

[英]how to display string as URI in java

我是Java的初学者,我正在尝试构建一个聊天机器人。 查询和响应被硬编码在2D字符串数组chatbot [] []中。

这是我使用过的完整代码,已经使用了两个用于JTextArea“输入和对话”的对象。 对象输入已附加到JPanel上,用于从用户那里获取文本,然后在2D字符串数组中进行搜索,如果找到,则从同一数组引发响应。

现在我的要求是,当响应为任何URL时,都应将其显示为链接,以便用户可以直接单击并转到该站点(对于当前方案为http://google.com )。请提出如何实现此目标或需要对代码进行任何修改。 addText是定义为将整个对话添加到不可编辑的对话文本区域的方法。

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import java.awt.event.KeyListener; 
import java.awt.event.KeyEvent;
import java.lang.Math;
import java.awt.Color;
import java.awt.Toolkit;

public class test implements KeyListener{

JFrame j=new JFrame("Temenos ChatBot");
JPanel p=new JPanel();
JTextArea dialog=new JTextArea(20,50); //it will contain the whole conversation(non editable)
JTextArea input=new JTextArea(1,50); //where we user input our query(editable)
JScrollPane scroll=new JScrollPane( 
    dialog,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);


String[][] chatbot={

{"hi","hello","hola","ola","howdy"}, //check for query
{"hi","hello","hey"}, //provides output of the query

{"google","provide link for google","www.google.com"}, //Query
{"http://google.com"}, //response


//default 
{"we can't find your query",
"(Sorry, we could not understand)"}
};


public static void main(String[]args){
    new test();
}

private void icon(){
    j.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("dy1MzZ-W.png")));//change the icon, paste the icon in default package
}

public test(){

    j.setSize(600,400);
    j.setResizable(false);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    dialog.setEditable(false);
    input.addKeyListener(this);     

    p.add(scroll);
    p.add(input);
    p.setBackground(new Color(0,150,255));
    j.add(p);

    j.setVisible(true);
    icon();
}
public void keyPressed(KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_ENTER){
        input.setEditable(false); 
        //-----grab quote-----------
        String quote=input.getText(); //takes whatever text user is inputting
        input.setText("");
        addText("->You:\t"+quote); 
        quote.trim(); 
        while(
            quote.charAt(quote.length()-1)=='!' ||
            quote.charAt(quote.length()-1)=='.' ||
            quote.charAt(quote.length()-1)=='?'
        ){
            quote=quote.substring(0,quote.length()-1); 
        }
        quote.trim();
        byte response=0; //byte is data type that will check for response
        /*
        0:we're searching through chatBot[][] for matches
        1:we didn't find anything
        2:we did find something
        */
        //-----check for matches----
        int j=0;//which group we're checking
        while(response==0){
            if(inArray(quote.toLowerCase(),chatbot[j*2])){
                response=2;
                int r=(int)Math.floor(Math.random()*chatbot[(j*2)+1].length);
                addText("\n->A.I.sha:\t"+chatbot[(j*2)+1][r]); 
            }
            j++;
            if(j*2==chatbot.length-1 && response==0){
                response=1;
            } //if it has come to end then set response to 1
        }

        //-----default--------------
        if(response==1){
            int r=(int)Math.floor(Math.random()*chatbot[chatbot.length-1].length);
            addText("\n->A.I.sha:\t"+chatbot[chatbot.length-1][r]); 
        }
        addText("\n");
    }
}

public void keyReleased(KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_ENTER){
        input.setEditable(true);
    }
}

public void keyTyped(KeyEvent e){}

public void addText(String str){
    dialog.setText(dialog.getText()+str);
}//it will add whatever text dialogue box is having

public boolean inArray(String in,String[] str){
    boolean match=false;
    for(int i=0;i<str.length;i++){
        if(str[i].equals(in)){
            match=true;
        } //Searches for string 'in' in string array,if it finds return true
    }
    return match;
}
 }

您可以使用Uri中的静态解析方法

Uri myUri = Uri.parse("http://google.com")

如果您要制作一个摆动的GUI,则可以遵循另一个堆栈溢出答案,例如此方法如何在JLabel中添加超链接

或者,您可以只用Java打开该网页的浏览器:

 // Create Desktop object
 Desktop d=Desktop.getDesktop();
 // Browse a URL, say google.com
 d.browse(new URI("http://google.com"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM