求在JAVA界面上放超链接的代码

2025-12-06 11:08:20
推荐回答(2个)
回答1:

java界面?纯java程序吗,还是jsp。纯java程序的话,需要引入java.net.url包。
try{getAppletContext().showDocument(new URL("http://www.163.com"),"打开位置");}
catch(Exception ex) {System.out.println("error"); }
就超链接了。

回答2:

如果认可代码,请加分至 100

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
final public class LinkStyleButton extends JFrame {
private LinkStyleButton(){
super("link style button");
doLay();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEADING,5,5));

container.add(createLinkButton("first link"));
container.add(createLinkButton("second link"));
container.add(createLinkButton("third link"));
container.add(createLinkButton("fourth link"));

pack();
setVisible(true);
}

/**
* create button with the given text label. The action is defined with _LinkAction
* Override methods those represent the UI style.
* @param txt button's label
* @return a jbutton instance
*/
private JButton createLinkButton(String txt){
return new JButton(new _LinkAction(txt)){
public Cursor getCursor() {return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);}
public Border getBorder() {return null;}
public int getHorizontalTextPosition() {return SwingConstants.LEFT;}
public Color getForeground(){return Color.BLUE;}
public boolean isContentAreaFilled(){return false;}
};
}

private class _LinkAction extends AbstractAction{
public _LinkAction(String name) {
super(name);
putValue(Action.ACTION_COMMAND_KEY,name);
}
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}

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