J/Link und Java Tutorial

Erzeugung von Java Paletten

Diese einfachen Beispiel zeigen, wie mit J/Link "graphical user interfaces" (GUIs) innerhalb von Mathematica aufgebaut werden. Wir starten mit einem einfachen Beispiel und gehen schrittwiese bis zu etwas komplexeren

Ein einfaches Fenster mit dem Text "Mein erstes Fenster!"

Needs["JLink`"]
Off[General::spell1];
InstallJava[];
frm  =  JavaNew["com.wolfram.jlink.MathJFrame"];
panel  =  JavaNew["javax.swing.JPanel"];
label  =  
          JavaNew["javax.swing.JLabel",  "Mein erstes Fenster !"];
panel@add[label];
frm@getContentPane[]@add[panel];
frm@setSize[200,  80];
JavaShow[frm];

Inkrementelles hochzählen auf Tastendruck

Needs["JLink`"];
Off[General::spell1];
InstallJava[];

\(SimpleModal[]\  := 
\ \ \ JavaBlock[
\ \ \ \ \ Module[{frame, \ button, \ textField, \ windowListener, \ 
                  buttonListener, 
\ \ \ \ \ \ \ \ \ buttonFunc}, \ 
                frame\  = \ 
                  JavaNew["com.wolfram.jlink.MathJFrame"]; 
\ \ \ \ \ \ \ panel\  = \ JavaNew["javax.swing.JPanel"]; 
\ \ \ \ \ \ \ button\  = \ 
                  JavaNew["javax.swing.JButton", \ "Inkrement um 1"]; 
\ \ \ \ \ \ \ textField\  = \ 
                  JavaNew["javax.swing.JTextField", \ "1", \ 
                    10]; 
\ \ \ \ \ \ \ panel@
                  add[button]; 
\ \ \ \ \ \ \ panel@
                  add[textField]; 
\ \ \ \ \ \ \ buttonListener\  = \ 
                  
                  JavaNew["com.wolfram.jlink.MathActionListener"]; 
\ \ \ \ \ \ \ buttonListener@
                  setHandler["actionPerformed", \ 
                    ToString[buttonFunc]]; 
\ \ \ \ \ \ \ button@
                  addActionListener[
                    buttonListener]; 
\ \ \ \ \ \ \ buttonFunc[_, \ _]\  := 
\ \ \ \ \ \ \ \ \ JavaBlock[
                    Module[{curText, \ newVal}, \ 
                      curText\  = 

                        textField@
                          getText[]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ newVal\  = \ 
                        ToExpression[curText]\  + \ 
                          1; 
\ \ \ \ \ \ \ \ \ \ \ \ \ textField@
                        setText[
                          ToString[
                            newVal]]]]; 
\ \ \ \ \ \ \ frame@\(getContentPane[]@add[panel]\); 
\ \ \ \ \ \ \ frame@
                  setSize[200, \ 100]; 
\ \ \ \ \ \ \ JavaShow[
                  frame]; 
\ \ \ \ \ \ \ frame@
                  setModal[]; 
\ \ \ \ \ \ \ DoModal[];]]\)

SimpleModal[];

Einfaches Fenster zum Zeichnen von Funktionen

Needs["JLink`"];
Off[General::spell1];
InstallJava[];
PlotApplet[] := Module[{},
frame  =  
                  JavaNew["com.wolfram.jlink.MathJFrame"];
panel  =  JavaNew["javax.swing.JPanel"];
panel@setLayout[Null];
titleLabel  =  
                  JavaNew["javax.swing.JLabel",  "Generate a
plot of your function, with respect to x."];
titleLabel@setBounds[10,  0,  350,  20];
panel@add[titleLabel];
functionField  =  
                  
                  JavaNew["javax.swing.JTextField",  "Sin[x]^2/Sqrt[x]\
"];
functionField@setBounds[100,  30,  150,  20];
panel@add[functionField];
mathCanvas  =  
                  JavaNew["com.wolfram.jlink.MathCanvas"];
mathCanvas@setBounds[50,  50,  250,  220];
panel@add[mathCanvas];
plotButton  =  
                  JavaNew["javax.swing.JButton",  "Plot"];
plotButton@setBounds[150,  270,  60,  20];
panel@add[plotButton];
buttonListener  =  
                  JavaNew["com.wolfram.jlink.MathActionListener"];
buttonListener@
                  setHandler["actionPerformed",  "buttonFunc"];
plotButton@addActionListener[buttonListener];
\(buttonFunc[_, \ _]\  := 

                  JavaBlock[

                    Module[{plotFunction}, \ 
                      plotFunction\  = \ functionField@getText[]; 

                      mathCanvas@
                        setMathCommand["Plot["\  <> \ 
                            plotFunction\  <> \ ", {x, 0, 10},
Background -> GrayLevel[.80]]"]]]\);
frame@getContentPane[]@add[panel];
frame@setSize[360,  360];
frame@layout[];
JavaShow[frame];
frame@setModal[];
jLabel1= JavaNew["javax.swing.JLabel",  "Zu \
zeichnende Funktion"];
jLabel1@setBounds[15, 182, 141, 28];
panel@add[jLabel1];

DoModal[];] 

PlotApplet[] ;

3D-Plot mit Funktionswahl, Popups und Variablenfelder

Needs["JLink`"];
Off[
        General::spell1];
InstallJava[];
PlotAppletwithAdjustment[]:=Module[{},
frame = JavaNew["com.wolfram.jlink.MathJFrame"];
panel = JavaNew["javax.swing.JPanel"];
panel@setLayout[Null];

mathCanvas = 
                JavaNew["com.wolfram.jlink.MathCanvas"];
mathCanvas@
                setBounds[10, 10, 220, 180];
panel@add[mathCanvas];

jLabel1= JavaNew["javax.swing.JLabel",  "Zu \
zeichnende Funktion"];
jLabel1@setBounds[15, 182, 160, 28];
panel@add[jLabel1];

jTextField1= JavaNew["javax.swing.JTextField",  "Cos[x \
y]"];
jTextField1@
                setBounds[17, 213, 200, 27];
panel@add[jTextField1];           

\(jLabel2 = 
                JavaNew["javax.swing.JLabel", \ "<html>X<sub>min</sub></html>"]\);
jLabel2@setBounds[16, 255, 34, 21];
panel@add[jLabel2];

jTextField2= JavaNew["javax.swing.JTextField",  "0"];
jTextField2@
                setBounds[53, 255, 34, 21]; 
panel@add[jTextField2];

\(jLabel3 = 
                JavaNew["javax.swing.JLabel", \ "<html>X<sub>max</sub></html>"]\);
jLabel3@setBounds[95, 255, 34, 21];
panel@add[jLabel3];

jTextField3= JavaNew["javax.swing.JTextField",  "2 \
Pi"];
jTextField3@
                setBounds[136, 255, 35, 21];
panel@add[jTextField3];

\(jLabel4 = 
                JavaNew["javax.swing.JLabel", \ "<html>Y<sub>min</sub></html>"]\);
jLabel4@setBounds[17, 285, 29, 19];
panel@add[jLabel4];

jTextField4= JavaNew["javax.swing.JTextField",  "0"];
jTextField4@
                setBounds[54, 284, 33, 21];
panel@add[jTextField4];

\(jLabel5 = 
                JavaNew["javax.swing.JLabel", \ "<html>Y<sub>max</sub></html>"]\);
jLabel5@setBounds[99, 284, 31, 19];
panel@add[jLabel5];

jTextField5= JavaNew["javax.swing.JTextField",  "2 \
Pi"];
jTextField5@
                setBounds[136, 283, 35, 21];
panel@add[jTextField5];

jLabel6 = 
                JavaNew["javax.swing.JLabel",  "PlotLabel"];
jLabel6@setBounds[16, 321, 70, 17];
panel@add[jLabel6];

jTextField6= JavaNew["javax.swing.JTextField",  "Cos[x \
y]"];
jTextField6@
                setBounds[80, 319, 130, 21];
panel@add[jTextField6];

jLabel7 = 
                JavaNew["javax.swing.JLabel",  "PlotType"];
jLabel7@setBounds[15, 352, 60, 17];
panel@add[jLabel7];

jComboBox1= \(JavaNew["javax.swing.JComboBox", \ 
                    MakeJavaObject[{"Plot3D", \ "ContourPlot", \ "DensityPlot"}]]\);
jComboBox1@
                setBounds[77, 350, 114, 21];
panel@add[jComboBox1];

jLabel8 = 
                JavaNew["javax.swing.JLabel",  "PlotStyle"];
jLabel8@setBounds[15, 380, 60, 17];
panel@add[jLabel8];

jComboBox2= \(JavaNew["javax.swing.JComboBox", \ 
                    MakeJavaObject[{"Default", \ "Technical", \ "Minimal"}]]\);
jComboBox2@
                setBounds[77, 380, 114, 21];
panel@add[jComboBox2];

jButton1 = JavaNew["javax.swing.JButton",  "Plot"];
jButton1@setBounds[80, 410, 83, 27];
panel@add[jButton1];

buttonListener = 
                JavaNew["com.wolfram.jlink.MathActionListener"];
    buttonListener@
                setHandler["actionPerformed", "buttonFunc"];
    jButton1@addActionListener[buttonListener];

    \(buttonFunc[_, _] := 
                JavaBlock[
                  Module[{plotFunction, \ xmin, xmax, ymin, ymax, \ 
                      plotlabel}, 
plotFunction = 
                      jTextField1@getText[]; 
xmin = 
                      jTextField2@getText[]; 
xmax = 
                      jTextField3@getText[]; 
ymin = 
                      jTextField4@getText[]; 
ymax = 
                      jTextField5@getText[]; 
plotlabel = 
                      jTextField6@getText[]; 
mathCanvas@
                      setUsesFE[True]; 
\ \ \ \ \ mathCanvas@
                      setMathCommand["Plot3D[" <> 
                          plotFunction <> ",{x, " <> xmin <> ", " <> 
                          xmax <> "},{y, " <> ymin <> ", " <> 
                          ymax <> "}, PlotPoints->80, Mesh->False, 
PlotLabel->StyleForm[TraditionalForm[" <> 
                          plotlabel <> "],"Section"]]"]]]\);

frame@getContentPane[]@
                  add[panel];
frame@setSize[280,  470];
frame@layout[];
JavaShow[frame];
frame@setModal[];
DoModal[];];
PlotAppletwithAdjustment[]

(*setBounds einzugLinks,einzugOben, länge, höhe

UnShareKernel[]