写了个小程序,一个按钮点一下可以自动调出默认浏览器?java 和c++ 这个语句怎么写呢?

2024-12-22 15:50:12
推荐回答(2个)
回答1:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;
public class TestFrame {
public static String url="about:blank";//默认为空白页
public static String cmd = "rundll32 url.dll,FileProtocolHandler ";
public static void main(String[] args) {
JFrame mainFrame=new JFrame("JavaFrame");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(300, 70);

JLabel jLabel1=new JLabel("网址:");
final JTextField jTextField=new JTextField();
JButton jButton1=new JButton("确定");
mainFrame.getContentPane().add(jLabel1,BorderLayout.WEST);
mainFrame.getContentPane().add(jTextField,BorderLayout.CENTER);
mainFrame.getContentPane().add(jButton1,BorderLayout.EAST);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);

jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jTextField.getText()!=null&&!jTextField.getText().trim().equals("")) {
url=jTextField.getText().trim();
}
try {
Runtime.getRuntime().exec(cmd+url);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}

回答2:

system("你的网址")