此为 Mathematica 7 文档,内容基于更早版本的 Wolfram 语言
查看最新文档(版本11.2)
Mathematica HowTo
How to | Connect a Java Program to Mathematica
Mathematica can be fully integrated with Java programs by using the J/Link interface.
There is some initial setup that must be done before you can start accessing Java programs.
First, load the J/Link package:
In[1]:=
Click for copyable input
InstallJava is the function that launches the Java Runtime Environment and installs it into Mathematica. Because some parts of Mathematica use Java internally, Java is almost always already running, but it is wise to make sure:
In[2]:=
Click for copyable input
Out[2]=
Here is how to connect Mathematica to a Java class using J/Link:
For example, create an instance of a Java Frame object:
In[3]:=
Click for copyable input
Out[3]=
You can call methods using the @ operator. Set the size of the window to 200×200 pixels:
In[4]:=
Click for copyable input
J/Link has some built-in commands that automatically call Java methods. Show the window:
In[5]:=
Click for copyable input
JavaShow[obj] is more or less equivalent to this Java call:
In[6]:=
Click for copyable input
You can reference static methods and fields using the ` operator. Minimize the window:
In[7]:=
Click for copyable input
You can find out what fields or methods are available for a given class by using Fields or Methods.
Print all the fields that pertain to window alignment:
In[8]:=
Click for copyable input
Out[8]//TableForm=
Because underscores have a special meaning in Mathematica, you must use an uppercase U in their place when referencing a name that contains an underscore:
Try to maximize the window with an underscore in the code. It does not work:
In[9]:=
Click for copyable input
Out[9]=
Using U in place of the underscore is the appropriate syntax:
In[10]:=
Click for copyable input
Now close the window:
In[11]:=
Click for copyable input