
正文
网页超链接代码java html超链接网站
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java超链接怎么实现?
1.随便在一个文件夹包上右击新建TestLink类,勾选main方法程序,并初始化导入包、继承JFrame窗体等。
2.在main方法中,首先实例化TestLink类,以便测试随时使用。
3.在此类的构造方法(没有如何参数)中,初始化Java窗体,设置窗体的各项属性,用户可适当增加其他属性。
4.运行此类,查看基本的Java窗体是否实现。
5.在构造方法中实例化JLabel类,双引号内的文字为窗体上显示的文字,必须被final修饰,然后添加此类的鼠标监听事件,最后加上“this.add(mBlogSina);”将标签加入到窗体中。
6.运行程序后,如下图所示,JLabel被加入到窗体中。
7.在鼠标监听事件中添加匿名类MouseadAdapter的三个方法。
8.创建如图的URI对象(双引号内为点击链接到的网址),并创建Desktop类,此时发现程序出错,点击“X”添加异常处理并将声明的Desktop类移到声明的uri下方。
9.输入下图if语句执行判断,并输入“dptdtp.browse(uri);”(执行链接操作的方法)时发现重新报错,则执行类似第8步的异常处理。
10.直至此步,运行程序,点击JLabel标签就会自动打开系统默认浏览器转到用户设置的指定网址。
11.下一步在其他两个方法内输入以下语句,实现鼠标移出、移入链接的效果。具体的实现代码,你必须亲自去做。
相关问答
Q1: java代码怎么将"http://…"这样一个路径,写成类似超链接那样?
java超链接:
button.setLabel("htmla href=\"http:\\\angelsinklow"angelsinklow/a/html");
如果用start的话,这样写
Runtime.getRuntime().exec("cmd /c start ‘http:\\\angelsinklow");
Runtime.getRuntime().exec("iexplore ");
对于JEditorPane,JTextPane,JTextArea,JLabel可以使用
setText("htmlA href=''test/A/html")
对于JEditorPane使用
setEditorKitForContentType("text/html", new PatchedHTMLEditorKit());
addHyperlinkListener(HyperlinkListener ... );
需要引入java.net.url包。
try{getAppletContext().showDocument(new URL("http:\\\angelsinklow"),"打开位置");}
catch(Exception ex) {System.out.println("error"); }
就超链接了。
Q2: java 的超链接代码 怎么写啊
a href="your target url"Link/a
如果在servlet中:
PrintWriter out = res.getWriter();
out.println("a href='your target url'Link/a");
如果是连接自己项目中的,请注意相对路径的写法。建议加上context path.
Q3: java如何实现超链接下载
java实现超链接下载方法如下:
response.setHeader("Content-disposition","attachment;filename="下载的文件名字);
备注:让response调用setheader方法添加下载的头给客户的浏览器,浏览器收到该头后就会打开相应的下载对话框。
Q4: JAVA消息框中如何加入超链接地址
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class 超链接 extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 107369444274434673L;
JFrame jf = new JFrame("一个超链接实现的例子");
JPanel jp = new JPanel();
JButton butt = new JButton("确定弹出");
MyDialog1 mydialog;
超链接() {
butt.addActionListener(this);
jp.add(butt);
jf.setContentPane(jp);
jf.pack();
jf.setBounds(40, 40, 200, 200);
jf.setVisible(true);
}
public static void main(String[] args) {
new 超链接();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == butt) {
mydialog=new MyDialog1(jf,"提示",true);
mydialog.setVisible(true);
}
}
}
//1.须为有模式对话框
//2。要能在母窗体的中央弹出==未实现
//部分代码可能有更好的实现....总之是要能加入那个自定义的JLabel
class MyDialog1 extends Dialog implements ActionListener{
static final int YES=1,NO=0;
int message=-1;
Button yes,no;
MyDialog1(Frame f,String s,boolean b){
super(f,s,b);
LinkLabel2 label = new LinkLabel2("百度一下,你就知道", "");
yes=new Button("Yes");
yes.addActionListener(this);
no=new Button("No");
no.addActionListener(this);
setLayout(new FlowLayout());
add(label);
add(yes);
add(no);
//setBounds(60,60,100,100);
setSize(350,100);
int winWidth=getBounds().width;//获取窗口的款等于320
int winHeigth=getBounds().height;//获取窗口的款等于350
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int)dim.getWidth();//获取屏幕的宽
int screenHeight = (int)dim.getHeight();//获取屏幕的高
setLocation((screenWidth-winWidth)/2, (screenHeight-winHeigth)/2);//定制窗口的位置
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
message=-1;setVisible(false);
}
}
);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==yes){
message=YES;setVisible(false);
}
else if(e.getSource()==no){
message=NO;setVisible(false);
}
}
public int getMessage(){
return message;
}
}
class LinkLabel2 extends JLabel {
private String text, url;
private boolean isSupported;
public LinkLabel2(String text, String url) {
this.text = text;
this.url = url;
try {
this.isSupported = Desktop.isDesktopSupported()
Desktop.getDesktop().isSupported(Desktop.Action.BROWSE);
} catch (Exception e) {
this.isSupported = false;
}
setText(false);
addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
setText(isSupported);
if (isSupported)
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
setText(false);
}
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().browse(
new java.net.URI(LinkLabel2.this.url));
} catch (Exception ex) {
}
}
});
}
private void setText(boolean b) {
if (!b)
setText("htmlfont color=blueu" + text);
else
setText("htmlfont color=redu" + text);
}
}
Q5: java 项目中返回这个超链接怎样用a标签写 href怎么写
1.最简单:a href="链接地址"链接/a
2.如果还要实现一些页面效果,比如弹出一个框之类的,可以写一个js函数
a href="javascript:void(0)" onclick="link()"链接/a
function link(){
//你的功能
alert("即将到你的地址");
//跳转
window.location.href="你的地址";
//如果是弹出新窗口,则是
window.open("你的地址",“_blank”);
}
网页超链接代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于html超链接网站、网页超链接代码java的信息别忘了在本站进行查找喔。






