错误:不兼容的类型:字符串无法转换为URI

时间:2016-01-31 09:55:53

标签: java string uri jbutton desktop

我正在构建一个计算器,其中JButton上有一个超链接到一个网站但我得到错误:不兼容的类型:字符串无法转换为URI。

错误在行中:

if(e.getSource()==LOGO
java.awt.Desktop.getDesktop().browse("Example.com");

错误是什么意思?我该如何解决呢?

/**
 Case : Computer Science Corporation
 */
import javax.swing.*;

import java.awt.*;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CSC extends JFrame {
    // frame settings//
    public static void main(String args[] ) {
        JFrame frame = new CSC();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Rekenmachine");
        JPanel Paneel = new Paneel();
        frame.setContentPane( new Paneel() );
        frame.setVisible(true);
    }
}
//paneel opbouw//
class Paneel extends JPanel implements ActionListener{
    public JButton Een,Twee,Drie,Vier,Vijf,Zes,Zeven,Acht,Negen,Nul,C,Min,Plus,Deel,Vermenigvuldig,Totaal,LOGO;
    public JTextField Output;
    static double a=0,b=0,result=0;
    static int operator=0;

    public Paneel(){
        setLayout (null);

        Een = new JButton("1");
        Een.setBounds(20, 225, 50, 50);
        Een.addActionListener(this);

        Twee = new JButton("2");
        Twee.setBounds(80, 225, 50, 50);
        Twee.addActionListener(this);

        Drie = new JButton("3");
        Drie.setBounds(140, 225, 50, 50);
        Drie.addActionListener(this);

        Vier = new JButton("4");
        Vier.setBounds(20, 165, 50, 50);
        Vier.addActionListener(this);

        Vijf = new JButton("5");
        Vijf.setBounds(80, 165, 50, 50);
        Vijf.addActionListener(this);

        Zes = new JButton("6");
        Zes.setBounds(140, 165, 50, 50);
        Zes.addActionListener(this);

        Zeven = new JButton("7");
        Zeven.setBounds(20, 105, 50, 50);
        Zeven.addActionListener(this);

        Acht = new JButton("8");
        Acht.setBounds(80, 105, 50, 50);
        Acht.addActionListener(this);

        Negen = new JButton("9");
        Negen.setBounds(140, 105, 50, 50);
        Negen.addActionListener(this);

        Nul = new JButton("0");
        Nul.setBounds(20, 285, 50, 50);
        Nul.addActionListener(this);

        C = new JButton("C");
        C.setBounds(80, 285, 50, 50);
        C.addActionListener(this);

        Min = new JButton("-");
        Min.setBounds(200, 285, 50, 50);
        Min.addActionListener(this);

        Plus = new JButton("+");
        Plus.setBounds(200, 225, 50, 50);
        Plus.addActionListener(this);

        Deel = new JButton("/");
        Deel.setBounds(200, 105, 50, 50);
        Deel.addActionListener(this);

        Vermenigvuldig = new JButton("*");
        Vermenigvuldig.setBounds(200, 165, 50, 50);
        Vermenigvuldig.addActionListener(this);

        Totaal = new JButton("=");
        Totaal.setBounds(140, 285, 50, 50);
        Totaal.addActionListener (this);

        Output = new JTextField("");
        Output.setBounds(20, 20, 230, 75);
        Output.setEditable(false);
        Output.setHorizontalAlignment(SwingConstants.RIGHT);

        LOGO = new JButton ("LOGO");
        LOGO.setBounds (20,345,230,75);
        LOGO.addActionListener (this);



        add (Een);
        add (Twee);
        add (Drie);
        add (Vier);
        add (Vijf);
        add (Zes);
        add (Zeven);
        add (Acht);
        add (Negen);
        add (Nul);
        add (C);
        add (Min);
        add (Plus);
        add (Vermenigvuldig);
        add (Totaal);
        add (Deel);
        add (Output);
        add (LOGO);
    }

     public void actionPerformed(ActionEvent e)
    {

        if(e.getSource()==LOGO)
            java.awt.Desktop.getDesktop().browse("example.com");

        if(e.getSource()==Een)
            Output.setText(Output.getText().concat("1"));

        if(e.getSource()==Twee)
            Output.setText(Output.getText().concat("2"));

        if(e.getSource()==Drie)
            Output.setText(Output.getText().concat("3"));

        if(e.getSource()==Vier)
            Output.setText(Output.getText().concat("4"));

        if(e.getSource()==Vijf)
            Output.setText(Output.getText().concat("5"));

        if(e.getSource()==Zes)
            Output.setText(Output.getText().concat("6"));

        if(e.getSource()==Zeven)
            Output.setText(Output.getText().concat("7"));

        if(e.getSource()==Acht)
            Output.setText(Output.getText().concat("8"));

        if(e.getSource()==Negen)
            Output.setText(Output.getText().concat("9"));

        if(e.getSource()==Nul)
            Output.setText(Output.getText().concat("0"));

        if(e.getSource()==Plus)
        {
            a=Double.parseDouble(Output.getText());
            operator=1;
            Output.setText("");
        } 

        if(e.getSource()==Min)
        {
            a=Double.parseDouble(Output.getText());
            operator=2;
            Output.setText("");
        }

        if(e.getSource()==Vermenigvuldig)
        {
            a=Double.parseDouble(Output.getText());
            operator=3;
            Output.setText("");
        }

        if(e.getSource()==Deel)
        {
            a=Double.parseDouble(Output.getText());
            operator=4;
            Output.setText("");
        }

        if(e.getSource()==Totaal)
        {
            b=Double.parseDouble(Output.getText());

            switch(operator)
            {
                case 1: result=a+b;
                    break;

                case 2: result=a-b;
                    break;

                case 3: result=a*b;
                    break;

                case 4: result=a/b;
                    break;

                default: result=0;
            }

            Output.setText(""+result);
        }

        if(e.getSource()==C)
            Output.setText("");

    }
}

更新

出现了2个新错误: 错误:未报告的异常URISyntaxException;必须被抓住或宣布被抛出 错误:未报告的异常IOException;必须被抓住或宣布被抛出

/**
 Case : Computer Science Corporation
 */
import javax.swing.*;

import java.awt.*;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.net.URI;
import java.net.URISyntaxException;

import java.io.IOException;


public class CSC extends JFrame {
    // frame settings//
    public static void main(String args[] ) {
        JFrame frame = new CSC();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Rekenmachine");
        JPanel Paneel = new Paneel();
        frame.setContentPane( new Paneel() );
        frame.setVisible(true);
    }
}
//paneel opbouw//
class Paneel extends JPanel implements ActionListener{
    public JButton Een,Twee,Drie,Vier,Vijf,Zes,Zeven,Acht,Negen,Nul,C,Min,Plus,Deel,Vermenigvuldig,Totaal,LOGO;
    public JTextField Output;
    static double a=0,b=0,result=0;
    static int operator=0;

    public Paneel(){
        setLayout (null);

        Een = new JButton("1");
        Een.setBounds(20, 225, 50, 50);
        Een.addActionListener(this);

        Twee = new JButton("2");
        Twee.setBounds(80, 225, 50, 50);
        Twee.addActionListener(this);

        Drie = new JButton("3");
        Drie.setBounds(140, 225, 50, 50);
        Drie.addActionListener(this);

        Vier = new JButton("4");
        Vier.setBounds(20, 165, 50, 50);
        Vier.addActionListener(this);

        Vijf = new JButton("5");
        Vijf.setBounds(80, 165, 50, 50);
        Vijf.addActionListener(this);

        Zes = new JButton("6");
        Zes.setBounds(140, 165, 50, 50);
        Zes.addActionListener(this);

        Zeven = new JButton("7");
        Zeven.setBounds(20, 105, 50, 50);
        Zeven.addActionListener(this);

        Acht = new JButton("8");
        Acht.setBounds(80, 105, 50, 50);
        Acht.addActionListener(this);

        Negen = new JButton("9");
        Negen.setBounds(140, 105, 50, 50);
        Negen.addActionListener(this);

        Nul = new JButton("0");
        Nul.setBounds(20, 285, 50, 50);
        Nul.addActionListener(this);

        C = new JButton("C");
        C.setBounds(80, 285, 50, 50);
        C.addActionListener(this);

        Min = new JButton("-");
        Min.setBounds(200, 285, 50, 50);
        Min.addActionListener(this);

        Plus = new JButton("+");
        Plus.setBounds(200, 225, 50, 50);
        Plus.addActionListener(this);

        Deel = new JButton("/");
        Deel.setBounds(200, 105, 50, 50);
        Deel.addActionListener(this);

        Vermenigvuldig = new JButton("*");
        Vermenigvuldig.setBounds(200, 165, 50, 50);
        Vermenigvuldig.addActionListener(this);

        Totaal = new JButton("=");
        Totaal.setBounds(140, 285, 50, 50);
        Totaal.addActionListener (this);

        Output = new JTextField("");
        Output.setBounds(20, 20, 230, 75);
        Output.setEditable(false);
        Output.setHorizontalAlignment(SwingConstants.RIGHT);

        LOGO = new JButton ("LOGO");
        LOGO.setBounds (20,345,230,75);
        LOGO.addActionListener (this);



        add (Een);
        add (Twee);
        add (Drie);
        add (Vier);
        add (Vijf);
        add (Zes);
        add (Zeven);
        add (Acht);
        add (Negen);
        add (Nul);
        add (C);
        add (Min);
        add (Plus);
        add (Vermenigvuldig);
        add (Totaal);
        add (Deel);
        add (Output);
        add (LOGO);
    }

     public void actionPerformed(ActionEvent e)
    {

        if(e.getSource()==LOGO)
        java.awt.Desktop.getDesktop().browse(new java.net.URI("http://www.google.com"));

        if(e.getSource()==Een)
            Output.setText(Output.getText().concat("1"));

        if(e.getSource()==Twee)
            Output.setText(Output.getText().concat("2"));

        if(e.getSource()==Drie)
            Output.setText(Output.getText().concat("3"));

        if(e.getSource()==Vier)
            Output.setText(Output.getText().concat("4"));

        if(e.getSource()==Vijf)
            Output.setText(Output.getText().concat("5"));

        if(e.getSource()==Zes)
            Output.setText(Output.getText().concat("6"));

        if(e.getSource()==Zeven)
            Output.setText(Output.getText().concat("7"));

        if(e.getSource()==Acht)
            Output.setText(Output.getText().concat("8"));

        if(e.getSource()==Negen)
            Output.setText(Output.getText().concat("9"));

        if(e.getSource()==Nul)
            Output.setText(Output.getText().concat("0"));

        if(e.getSource()==Plus)
        {
            a=Double.parseDouble(Output.getText());
            operator=1;
            Output.setText("");
        } 

        if(e.getSource()==Min)
        {
            a=Double.parseDouble(Output.getText());
            operator=2;
            Output.setText("");
        }

        if(e.getSource()==Vermenigvuldig)
        {
            a=Double.parseDouble(Output.getText());
            operator=3;
            Output.setText("");
        }

        if(e.getSource()==Deel)
        {
            a=Double.parseDouble(Output.getText());
            operator=4;
            Output.setText("");
        }

        if(e.getSource()==Totaal)
        {
            b=Double.parseDouble(Output.getText());

            switch(operator)
            {
                case 1: result=a+b;
                    break;

                case 2: result=a-b;
                    break;

                case 3: result=a*b;
                    break;

                case 4: result=a/b;
                    break;

                default: result=0;
            }

            Output.setText(""+result);
        }

        if(e.getSource()==C)
            Output.setText("");

    }
}   

3 个答案:

答案 0 :(得分:1)

  

不兼容的类型:字符串无法转换为URI。   在行:java.awt.Desktop.getDesktop()。browse(" Example.com");

应该是

java.awt.Desktop.getDesktop().browse(java.net.new URI("http://www.google.com"));

来自文档

  

public void browse(URI uri)               抛出IOException

<强>参数:

  

uri - 要在用户默认浏览器中显示的URI

Desktop#browse() reference

答案 1 :(得分:0)

您需要先创建URI。在传递参数中,您应该使用绝对 URI

  

绝对URI指定方案;一个非绝对的URI被认为是相对的。

Desktop#browse(URI uri)

  

启动默认浏览器以显示URI。如果是默认浏览器   无法处理已注册的应用程序的指定URI   用于处理指定类型的URI s。申请是   根据{{​​1}}的协议和路径确定,由...定义   URI上课。

URI

答案 2 :(得分:0)

错误意味着java.awt.Desktop.getDesktop().browse()函数期望URI作为参数(读取doc),并且您正在传递字符串。将您的代码更改为此

java.awt.Desktop.getDesktop().browse(new java.net.URI("Example.com"));