最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Nashorn: How to select constructor to invoke - Stack Overflow

programmeradmin0浏览0评论

I know from this page that I can select a method like this:

API["test(Integer[])"](1);

How do I do this for constructors? In particular, I'm trying to instantiate a java.awt.Color from Nashorn:

var highlightColor = new java.awt.Color(1, 1, 128/255, 1);

I get the following error: Can't unambiguously select between fixed arity signatures [(float, float, float, float), (int, int, int, int)] of the method java.awt.Color. for argument types [java.lang.Integer, java.lang.Integer, java.lang.Double, java.lang.Integer]

I've tried this:

var highlightColor = new java.awt.Color["(float,float,float,float)"](1, 1, 128/255, 1);

But that gives me this error: Caused by: :52 TypeError: null is not a function

I know from this page that I can select a method like this:

API["test(Integer[])"](1);

How do I do this for constructors? In particular, I'm trying to instantiate a java.awt.Color from Nashorn:

var highlightColor = new java.awt.Color(1, 1, 128/255, 1);

I get the following error: Can't unambiguously select between fixed arity signatures [(float, float, float, float), (int, int, int, int)] of the method java.awt.Color. for argument types [java.lang.Integer, java.lang.Integer, java.lang.Double, java.lang.Integer]

I've tried this:

var highlightColor = new java.awt.Color["(float,float,float,float)"](1, 1, 128/255, 1);

But that gives me this error: Caused by: :52 TypeError: null is not a function

Share Improve this question edited May 23, 2017 at 11:59 CommunityBot 11 silver badge asked Dec 9, 2014 at 21:23 David StruckDavid Struck 1572 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

We added this functionality, but it will only be available with Java 8u40. The exact syntax is java.awt["Color(int, int, int)"] (the signature is part of the last name ponent, consistent with how it works on method names). You can try it out with early access releases of 8u40 at this time (it's scheduled for release in March 2015).

You need to make sure you're passing in 4 integers (or floats in this case), try this.

var highlightColor = new java.awt.Color(1.0, 1.0, 128/255, 1.0);

Pass either 4 ints or 4 floats

var highlightColor = new java.awt.Color(255, 255, 128, 255);

OR

var highlightColor = new java.awt.Color(1.0, 1.0, 128/255, 1.0);
发布评论

评论列表(0)

  1. 暂无评论