Macroの保管場所
Macroの基
Msgbox
Data
[ Date ]
[ Time ]
[ Number ]
[ String ]
[ 正規表現 ]
Function
Object
I/O( File )
[ File操作 ]
[ File情報 ]
<< 取得・確認 >>
<< 設定 >>
[ Drive ]
[ Stream ]
Thead
Utility
Swing
[ Frame ]
[ Listener ]
NetWork
Shell関数
Rhino定義関数
XMLHttpRequest()
Mouse
ScriptEngine
Tips
RhinoからOpenOffice.org起動(外部起動)
Macroの保管場所
[ Windows7 ] マイマクロ : C:\Users\[ UserName ]\AppData\Roaming\LibreOffice\3\user\Scripts\javascript\ LibreOfficeのマクロ : C:\Program Files\LibreOffice 3\Basis\share\Scripts\javascript\ [ WindowsXP ] マイマクロ : C:\Documents and Settings\[ UserName ]\Application Data\OpenOffice.org\3\user\Scripts\javascript\ LibreOfficeのマクロ : C:\Program Files\OpenOffice 3\Basis\share\Scripts\javascript\ [ Ubuntu11.04 ] <= 但し、Ubuntu11.04ではjavascript macroは実行出来ません。(2011/05/14時点) マイマクロ : /home/[ UserName ]/.libreoffice/3/user/Scripts/javascript/ LibreOfficeのマクロ : /usr/lib/libreoffice/basis-link/share/Scripts/javascript/ [ Ubuntu10.04LTS ] マイマクロ : /home/[ UserName ]/.libreoffice/3/user/Scripts/javascript/ LibreOfficeのマクロ : /usr/lib/libreoffice/basis-link/share/Scripts/javascript/
Macroの基
// Hello World in JavaScript
importClass(Packages.javax.swing.JOptionPane);
JOptionPane.showMessageDialog(null, "Hello World in JavaScript");
//if <= Ifは駄目
importClass(Packages.javax.swing.JOptionPane);
var oI=2
if(oI != 1){
JOptionPane.showMessageDialog(null, "oI = 1 ではありません");
}
else{
JOptionPane.showMessageDialog(null, "oI = " + oI + "です");
}
importClass(Packages.javax.swing.JOptionPane);
var oDisp ="";
var oI = 2;
oDisp = (oI != 1) ? oDisp = "oI = 1 ではありません" : oDisp = "oI = " + oI + "です";
//oDisp = "Success"
JOptionPane.showMessageDialog(null, oDisp);
//for(){ }
importClass(Packages.javax.swing.JOptionPane);
for(i=0; i<=3; i++){
JOptionPane.showMessageDialog(null, i + "回目です");
}
//while(){}
importClass(Packages.javax.swing.JOptionPane);
var i = 1
while(i <= 3){
JOptionPane.showMessageDialog(null, i + "回目です");
i = i + 1
}
//Switch(){case default}
importClass(Packages.javax.swing.JOptionPane);
importPackage(java.util.date);
JOptionPane.showMessageDialog(null, "今日の何曜日?");
oToday = new Date();
oWeek = oToday.getDay();
switch(oWeek)
{
case 0: JOptionPane.showMessageDialog(null, "今日は日曜日です!"); break;
case 1: JOptionPane.showMessageDialog(null, "今日は月曜日です!"); break;
case 2: JOptionPane.showMessageDialog(null, "今日は火曜日です!"); break;
case 3: JOptionPane.showMessageDialog(null, "今日は水曜日です!"); break;
case 4: JOptionPane.showMessageDialog(null, "今日は木曜日です!"); break;
case 5: JOptionPane.showMessageDialog(null, "今日は金曜日です!"); break;
default : JOptionPane.showMessageDialog(null, "今日は土曜日です!");break;
}
//Array[]
importClass(Packages.javax.swing.JOptionPane);
oTimes = new Array();
for(i=0; i<=3; i++){
oTimes[i] = i + "回目です"
JOptionPane.showMessageDialog(null, oTimes[i]);
}
//Input data of Array[]
importClass(java.lang.System);
importPackage(java.util);
var list = new ArrayList();
list.add("value1");
list.add("value2");
list.add("value3");
//Input data of Array[]
importClass(java.lang.System);
importPackage(java.util);
importClass(Packages.javax.swing.JOptionPane);
var list = new ArrayList();
list.add("value1");
list.add("value2");
list.add("value3");
var it = list.iterator();
while(it.hasNext()){
JOptionPane.showMessageDialog(null, it.next());
}
Msgbox
// message box default
importClass(Packages.javax.swing.JOptionPane);
frame = new Packages.javax.swing.JFrame("frame")
frame.setSize(200,200)
JOptionPane.showMessageDialog(frame, "Success");
// message box Warning
importClass(Packages.javax.swing.JOptionPane);
frame = new Packages.javax.swing.JFrame("frame")
frame.setSize(200,200)
JOptionPane.showMessageDialog(frame, "Success","Title",JOptionPane.WARNING_MESSAGE);
// message box Error
importClass(Packages.javax.swing.JOptionPane);
frame = new Packages.javax.swing.JFrame("frame")
frame.setSize(200,200)
JOptionPane.showMessageDialog(frame, "Error","Title",JOptionPane.ERROR_MESSAGE);
// message box Error
importClass(Packages.javax.swing.JOptionPane);
frame = new Packages.javax.swing.JFrame("frame")
frame.setSize(200,200)
JOptionPane.showMessageDialog(frame, "No Icon Message","A plain message",JOptionPane.PLAIN_MESSAGE);
// Confirm1
importClass(Packages.javax.swing.JOptionPane);
JOptionPane.showOptionDialog(null, "Hello World in JavaScript","test",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,
null,null,null);
//Custom button
importClass(Packages.javax.swing.JOptionPane);
frame = new Packages.javax.swing.JFrame("frame")
frame.setSize(200,200)
options = new Array("Yes, please","No, thanks","No eggs, no ham!");
JOptionPane.showOptionDialog(frame,"Would you like some green eggs to go with that ham?",
"Title : A Silly Question",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,options,options[2]);
//
// options[2] => Defaultにて選択されているButton
// Confirm2
importClass(Packages.javax.swing.JOptionPane);
JOptionPane.showConfirmDialog(null, "Hello World in JavaScript");
// shows message in the message box
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.awt.XWindowPeer);
importClass(Packages.com.sun.star.awt.XWindow);
importClass(Packages.com.sun.star.awt.XMessageBoxFactory);
importClass(Packages.com.sun.star.awt.Rectangle);
function showMessage(xDesktop, message)
{
xFrame = xDesktop.getCurrentFrame();
xWindow = xFrame.getContainerWindow();
xWindowPeer = UnoRuntime.queryInterface(XWindowPeer, xWindow);
xToolkit = xWindowPeer.getToolkit();
xMessageboxFactory = UnoRuntime.queryInterface(XMessageBoxFactory, xToolkit);
xMessageBox = xMessageboxFactory.createMessageBox(xWindowPeer, new Rectangle(), 'messbox', 1, "", message);
return xMessageBox.execute();
}
//
oDisp = "To Be defined a Function" + "\n" + "for MessageBox";
showMessage(XSCRIPTCONTEXT.getDesktop(), oDisp);
// Msgbox_Return
importClass(Packages.javax.swing.JOptionPane);
oAns = JOptionPane.showOptionDialog(null, "Hello World in JavaScript","test",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,
null,null,null);
frame = new Packages.javax.swing.JFrame("frame")
frame.setSize(200,200)
if(oAns == 0){
JOptionPane.showMessageDialog(frame, "「はい」が選択されました。");
}
if(oAns == 1){
JOptionPane.showMessageDialog(frame, "「いいえ」が選択されました。");
}
if(oAns == -1){
JOptionPane.showMessageDialog(frame, "「×」でキャンセルされました。");
}
Data
[ Date ]
// Date
importClass(Packages.javax.swing.JOptionPane);
importPackage(java.util.date);
oDateTime = new Date()
oYear = oDateTime.getYear() + 1900
oMonth = oDateTime.getMonth() + 1
oDate = oDateTime.getDate()
oDay = oDateTime.getDay()
switch(oDay){
case 0: oWeek="日"; break;
case 1: oWeek="月"; break;
case 2: oWeek="火"; break;
case 3: oWeek="水"; break;
case 4: oWeek="木"; break;
case 5: oWeek="金"; break;
default : oWeek="土"; break;
}
JOptionPane.showMessageDialog(null, oYear+"年");
JOptionPane.showMessageDialog(null, oMonth+"月");
JOptionPane.showMessageDialog(null, oDate+"日");
JOptionPane.showMessageDialog(null, oWeek+"曜日");
JOptionPane.showMessageDialog(null, "Success");
// setLastModified
importClass(Packages.java.text.SimpleDateFormat);
importClass(Packages.java.util.Calendar);
importPackage(java.util.date);
importClass(Packages.javax.swing.JOptionPane);
//Long type => Date Type
function convertDate(lo){
cal = Calendar.getInstance();
sf = new SimpleDateFormat("yyyy/MM/dd");
return sf.format(cal.getTime());
}
// Time
importClass(Packages.javax.swing.JOptionPane);
importPackage(java.util.date);
oDateTime = new Date()
oHour = oDateTime.getHours()
oMinute = oDateTime.getMinutes()
oSecond = oDateTime.getSeconds()
JOptionPane.showMessageDialog(null, oHour+"時");
JOptionPane.showMessageDialog(null, oMinute+"分");
JOptionPane.showMessageDialog(null, oSecond+"秒");
JOptionPane.showMessageDialog(null, "Success");
[ Number ]
// Number
importClass(Packages.javax.swing.JOptionPane);
var oNum = 12345.67890123456789
oInt = Integer(oNum)
JOptionPane.showMessageDialog(null, oInt+" <= [ Integer ]");
JOptionPane.showMessageDialog(null, "Success");
// Number
importClass(Packages.javax.swing.JOptionPane);
var oNum = 12345.67890123456789
oDbl = Double(oNum)
JOptionPane.showMessageDialog(null, oDbl+" <= [ Double ]");
JOptionPane.showMessageDialog(null, "Success");
// Array()
importClass(Packages.javax.swing.JOptionPane);
jArr = new Array(200,500,600)
// jArr[0] = 300 // <= OK
JOptionPane.showMessageDialog(null, jArr[0]);
JOptionPane.showMessageDialog(null, "Success");
//length
importClass(Packages.javax.swing.JOptionPane);
jArr = new Array(200,500,600)
jNum = jArr.length
JOptionPane.showMessageDialog(null, jArr+" の配列要素数は => "+jNum);
JOptionPane.showMessageDialog(null, "Success");
//push()
importClass(Packages.javax.swing.JOptionPane);
jArr = new Array(200,500,600)
old = jArr.toString()
jArr.push(100)
JOptionPane.showMessageDialog(null, old + " => "+ jArr);
JOptionPane.showMessageDialog(null, "Success");
//pop()
importClass(Packages.javax.swing.JOptionPane);
jArr = new Array("black","red","yellow","green","white")
old = jArr.toString()
jArr.pop()
JOptionPane.showMessageDialog(null, old + "\n" + " => "+ jArr);
JOptionPane.showMessageDialog(null, "Success");
//sort()
importClass(Packages.javax.swing.JOptionPane);
function sortFunc(n1,n2){
return n1-n2;
}
jArr = new Array(60,40,50,30,100,20,70);
old = jArr.toString();
jArr.sort(sortFunc);
JOptionPane.showMessageDialog(null, old + "\n" + " => " + "\n" + jArr);
JOptionPane.showMessageDialog(null, "Success");
//reverse()
importClass(Packages.javax.swing.JOptionPane);
jArr = new Array(60,40,50,30,100,20,70);
old = jArr.toString();
jArr.reverse();
JOptionPane.showMessageDialog(null, old + "\n" + " => " + "\n" + jArr);
JOptionPane.showMessageDialog(null, "Success");
//shift()
importClass(Packages.javax.swing.JOptionPane);
jArr = new Array("gold","silver","black","white");
old = jArr.toString();
jArr.shift();
JOptionPane.showMessageDialog(null, old + "\n" + " => " + "\n" + jArr);
//unshift()
importClass(Packages.javax.swing.JOptionPane);
jArr = new Array("gold","silver","black","white");
old = jArr.toString();
jArr.unshift("add");
JOptionPane.showMessageDialog(null, old + "\n" + " => " + "\n" + jArr);
// PAI
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.PI;
JOptionPane.showMessageDialog(null, oNum);
// Trigonometric function
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.cos(0);
JOptionPane.showMessageDialog(null, oNum);
// Trigonometric function
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum1 = java.lang.Math.acos(0);
var oNum = oNum1*180/oPI
JOptionPane.showMessageDialog(null, oNum+"°");
// Surplus
importClass(Packages.javax.swing.JOptionPane);
var oNum = 5%3
JOptionPane.showMessageDialog(null, oNum);
// Integer
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = parseInt(oPI)
JOptionPane.showMessageDialog(null, oNum);
// Float
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = parseFloat(oPI)
JOptionPane.showMessageDialog(null, oNum);
// eval
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = eval(oPI/2)
JOptionPane.showMessageDialog(null, oNum);
// Not to be Number
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = parseInt(oPI)
oStr = "test"
oN = isNaN(oNum)
oS = isNaN(oStr)
JOptionPane.showMessageDialog(null, oNum+ "は数値で無い? => "+oN+" : "+oStr+"は数値で無い?=> "+oS);
// Finite
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = parseInt(oPI)
var oNum2 = 1/0
oF1 = isFinite(oNum)
oF2 = isFinite(oNum2)
JOptionPane.showMessageDialog(null, oNum+ "は有限? => "+oF1+" : "+oNum2+"は有限?=> "+oF2);
// abs
importClass(Packages.javax.swing.JOptionPane);
var oNum1 = -100
var oNum2 = java.lang.Math.abs(oNum1)
oF1 = isFinite(oNum)
oF2 = isFinite(oNum2)
JOptionPane.showMessageDialog(null, oNum1+ "の絶対値は => "+oNum2);
// ceil
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = java.lang.Math.ceil(oPI)
JOptionPane.showMessageDialog(null, oPI+ "の切上げ値は => "+oNum);
// floor
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = java.lang.Math.floor(oPI)
JOptionPane.showMessageDialog(null, oPI+ "の切下げ値は => "+oNum);
// round
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum = java.lang.Math.round(oPI)
JOptionPane.showMessageDialog(null, oPI+ "の四捨五入値は => "+oNum);
// log
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.log(5*2)
JOptionPane.showMessageDialog(null, "e^(5*2) = "+oNum);
// exp
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.exp(5*2)
JOptionPane.showMessageDialog(null, "exp(5*2) = "+oNum);
// max
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.max(1,2)
JOptionPane.showMessageDialog(null, "max(1,2) = "+oNum);
// min
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.min(1,2)
JOptionPane.showMessageDialog(null, "max(1,2) = "+oNum);
// pow
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.pow(2,5)
JOptionPane.showMessageDialog(null, "2^5 = "+oNum);
// squrt
importClass(Packages.javax.swing.JOptionPane);
var oNum = java.lang.Math.sqrt(3*3*3)
JOptionPane.showMessageDialog(null, "sqrt(3*3*3) = "+oNum);
// random()
importClass(Packages.javax.swing.JOptionPane);
var oNum1 = java.lang.Math.random()
JOptionPane.showMessageDialog(null, "1回目の乱数 => "+oNum1);
var oNum2 = java.lang.Math.random()
JOptionPane.showMessageDialog(null, "2回目の乱数 => "+oNum2);
// toExponential
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum1 = oPI * 100
var oNum2 = oNum1.toExponential(3)
JOptionPane.showMessageDialog(null, oNum2);
// toFixed
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum1 = oPI * 100
var oNum2 = oNum1.toFixed(1)
JOptionPane.showMessageDialog(null, oNum2);
// toPrecision
importClass(Packages.javax.swing.JOptionPane);
var oPI = java.lang.Math.PI
var oNum1 = oPI * 100
var oNum2 = oNum1.toPrecision(2)
JOptionPane.showMessageDialog(null, oNum2);
[ String ]
// charAt()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABSDEFG";
oTC = jString.charAt(0);
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬"
oTC = jString.charAt(2);
JOptionPane.showMessageDialog(null, oTC);
// concat("")
importClass(Packages.javax.swing.JOptionPane);
jString = "ABSDEFG";
oTC = jString.concat("を繋ぎました。");
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬"
oTC = jString.concat("を繋ぎました。");
JOptionPane.showMessageDialog(null, oTC);
// equals("")
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfG";
oTC = jString.equals("BC");
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬"
oTC = jString.equals("坂本龍馬");
JOptionPane.showMessageDialog(null, oTC);
// replace("","")
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfG";
oTC = jString.replace("B","zz");
JOptionPane.showMessageDialog(null, oTC);
// jString = "坂本龍馬";
// oTC = jString.equals("龍","竜"); <= Not To Be Used
// indexOf("")
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfG";
oTC = jString.indexOf("dE");
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬";
oTC = jString.indexOf("本");
JOptionPane.showMessageDialog(null, oTC);
// lastIndexOf("")
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.lastIndexOf("A");
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬日本";
oTC = jString.lastIndexOf("本");
JOptionPane.showMessageDialog(null, oTC);
// substring()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.substring(4);
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬日本";
oTC = jString.substring(4);
JOptionPane.showMessageDialog(null, oTC);
// substring(開始位置,終了位置)
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.substring(2,5); // substring(開始位置,終了位置)
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬日本";
oTC = jString.substring(2,4);
JOptionPane.showMessageDialog(null, oTC);
// slice(開始位置,終了位置)
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.slice(2,5);
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬日本";
oTC = jString.slice(2,4);
JOptionPane.showMessageDialog(null, oTC);
// substr(開始位置,文字数)
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.substr(2,2); // substring(開始位置,終了位置+1)
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬日本";
oTC = jString.substr(2,2);
JOptionPane.showMessageDialog(null, oTC);
// toLowerCase()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.toLowerCase();
JOptionPane.showMessageDialog(null, oTC);
// toUpperCase()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.toUpperCase();
JOptionPane.showMessageDialog(null, oTC);
// toString()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.toString();
JOptionPane.showMessageDialog(null, oTC);
jString = "坂本龍馬";
oTC = jString.toString();
JOptionPane.showMessageDialog(null, oTC);
// charCodeAt()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.charCodeAt(3);
JOptionPane.showMessageDialog(null, jString.charAt(3)+" => "+oTC);
oTC = jString.charCodeAt(1);
JOptionPane.showMessageDialog(null, jString.charAt(1)+" => "+oTC);
jString = "坂本龍馬";
oTC = jString.charCodeAt(0);
JOptionPane.showMessageDialog(null, jString.charAt(0)+" => "+oTC);
oTC = jString.charCodeAt(2);
JOptionPane.showMessageDialog(null, jString.charAt(2)+" => "+oTC);
// fromCharCode
importClass(Packages.javax.swing.JOptionPane);
jCode = 65;
oTC = String.fromCharCode(jCode);
JOptionPane.showMessageDialog(null, jCode+" => "+oTC);
jCode = 22338;
oTC = String.fromCharCode(jCode);
JOptionPane.showMessageDialog(null, jCode+" => "+oTC);
// escape() / encodeURI()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = escape(jString);
JOptionPane.showMessageDialog(null, jString+" => "+oTC);
oTC = encodeURI(jString);
JOptionPane.showMessageDialog(null, jString+" => "+oTC);
jString = "坂本龍馬";
oTC = escape(jString);
JOptionPane.showMessageDialog(null, jString+" => "+oTC);
oTC = encodeURI(jString);
JOptionPane.showMessageDialog(null, jString+" => "+oTC);
// Encode using Java Class
importClass(Packages.java.io.UnsupportedEncodingException);
importClass(Packages.java.net.URLEncoder);
importClass(Packages.javax.swing.JOptionPane);
//
param = "あいうえおかきくけこ";
try{
oCode = URLEncoder.encode(param,"UTF-8");
oDisp = param + "\n" + " => " + "\n" + oCode;
JOptionPane.showMessageDialog(null, oDisp);
} catch(UnsupportedEncodingException) {
// 例外処理
JOptionPane.showMessageDialog(null, "文字CodeがSupportされていません");
}
// unescape()
importClass(Packages.javax.swing.JOptionPane);
jString = "%u5742%u672C%u9F8D%u99AC";
oTC = unescape(jString);
JOptionPane.showMessageDialog(null, jString+" => "+oTC);
// oTC = decodeURI(jString);
// JOptionPane.showMessageDialog(null, jString+" => "+oTC);
jString = "%E5%9D%E6%9C%AC%E9%AC%E9%BE%8D%E9%A6%AC";
oTC = unescape(jString);
JOptionPane.showMessageDialog(null, jString+" => "+oTC);
// oTC = decodeURI(jString);
// JOptionPane.showMessageDialog(null, jString+" => "+oTC);
// Decode using Java Class
importClass(Packages.java.io.UnsupportedEncodingException);
importClass(Packages.java.net.URLEncoder);
importClass(Packages.java.net.URLDecoder);
importClass(Packages.javax.swing.JOptionPane);
//
param = "あいうえおかきくけこ";
try{
oEnCode = URLEncoder.encode(param,"UTF-8");
oDeCode = URLDecoder.decode(oEnCode,"UTF-8");
oDisp = param + "\n" + " => " + "\n" + oEnCode;
oDisp = oDisp + "\n" + " => " + "\n" + oDeCode;
JOptionPane.showMessageDialog(null, oDisp);
} catch(UnsupportedEncodingException) {
// 例外処理
JOptionPane.showMessageDialog(null, "文字CodeがSupportされていません");
}
// indexOf()
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG坂本龍馬";
oTC = jString.indexOf("d",0)+1;
JOptionPane.showMessageDialog(null, jString+" におけるdの位置は頭から"+oTC+"番目");
jString = "坂本龍馬ABcdEfAG";
oTC = jString.indexOf("龍",0)+1;
JOptionPane.showMessageDialog(null, jString+" における「龍」の位置は頭から"+oTC+"番目");
// match()
importClass(Packages.javax.swing.JOptionPane);
jString = "龍ABcdEfAG坂本龍馬";
oR = new RegExp("龍", "g");
oTC1 = jString.match(oR);
oTC =oTC1.length
JOptionPane.showMessageDialog(null, jString+" 中に「龍」は"+oTC+"個matchしました");
// length
importClass(Packages.javax.swing.JOptionPane);
jString = "ABcdEfAG";
oTC = jString.length;
JOptionPane.showMessageDialog(null, jString+" は "+oTC+" 文字です");
jString = "坂本龍馬";
oTC = jString.length;
JOptionPane.showMessageDialog(null, jString+" は "+oTC+" 文字です");
// Retun
importClass(Packages.javax.swing.JOptionPane);
oDisp = "OpenOffice.org" + "\n" + "Macro using JavaScript";
JOptionPane.showMessageDialog(null, oDisp);
[ 正規表現 ]
//正規表現
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.util.regex.Matcher);
importClass(Packages.java.util.regex.Pattern);
function isCorrectLetterNumber(otarget){
isCorrect = false;
if(otarget != null){
oP = Pattern.compile(".{4,7}"); // <= 4文字以上7文字以内
oM =oP.matcher(otarget);
if (oM.matches()){
isCorrect = true;
}
}
return isCorrect;
}
try{
oStr1 = "12345";
oStr2 = "マクロ";
oCheck1 = isCorrectLetterNumber(oStr1);
oCheck2 = isCorrectLetterNumber(oStr2);
oDisp1 = oStr1 + "は4~7文字? => " + oCheck1
oDisp2 = oStr2 + "は4~7文字? => " + oCheck2
JOptionPane.showMessageDialog(null, oDisp1 + "\n" + oDisp2);
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//正規表現
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.util.regex.Matcher);
importClass(Packages.java.util.regex.Pattern);
function isCorrectLetter(otarget){
isCorrect = false;
if(otarget != null){
oP = Pattern.compile("[a-zA-Z0-9]+");
oM =oP.matcher(otarget);
if (oM.matches()){
isCorrect = true;
}
}
return isCorrect;
}
try{
oStr1 = "12345ABCabc";
oStr2 = "マクロ";
oStr3 = "aマbクcロd";
oCheck1 = isCorrectLetter(oStr1);
oCheck2 = isCorrectLetter(oStr2);
oCheck3 = isCorrectLetter(oStr3);
oDisp1 = oStr1 + "は半角文字? => " + oCheck1
oDisp2 = oStr2 + "は半角文字? => " + oCheck2
oDisp3 = oStr3 + "は半角文字? => " + oCheck3
JOptionPane.showMessageDialog(null, oDisp1 + "\n" + oDisp2 + "\n" + oDisp3);
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//正規表現
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.util.regex.Matcher);
importClass(Packages.java.util.regex.Pattern);
function isCorrectLetter(otarget){
isCorrect = false;
if(otarget != null){
oP = Pattern.compile("['\",]+");
oM =oP.matcher(otarget);
if (oM.matches()){
isCorrect = true;
}
}
return isCorrect;
}
try{
oStr1 = "1234";
oStr2 = "'temp";
oStr3 = "\temp";
oCheck1 = isCorrectLetter(oStr1);
oCheck2 = isCorrectLetter(oStr2);
oCheck3 = isCorrectLetter(oStr3);
oDisp1 = oStr1 + "は記号を含む? => " + oCheck1
oDisp2 = oStr2 + "は記号を含む? => " + oCheck2
oDisp3 = oStr3 + "は記号を含む? => " + oCheck3
JOptionPane.showMessageDialog(null, oDisp1 + "\n" + oDisp2 + "\n" + oDisp3);
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//正規表現
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.util.regex.Matcher);
importClass(Packages.java.util.regex.Pattern);
function isCorrectLetter(otarget){
isCorrect = false;
if(otarget != null){
oP = Pattern.compile("[0-9]{3}-[0-9]{4}");
oM =oP.matcher(otarget);
if (oM.matches()){
isCorrect = true;
}
}
return isCorrect;
}
try{
oStr1 = "100-0003";
oStr2 = "100-0003";
oCheck1 = isCorrectLetter(oStr1);
oCheck2 = isCorrectLetter(oStr2);
oDisp1 = oStr1 + "は郵便番号? => " + oCheck1
oDisp2 = oStr2 + "は郵便番号? => " + oCheck2
JOptionPane.showMessageDialog(null, oDisp1 + "\n" + oDisp2);
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//正規表現
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.util.regex.Matcher);
importClass(Packages.java.util.regex.Pattern);
function isCorrectLetter(otarget){
isCorrect = false;
if(otarget != null){
oP = Pattern.compile("\\d{2,4}-\\d{2,4}-\\d{4}"); // <= 定義済文字使用
// oP = Pattern.compile("[0-9]{2,4}-[0-9]{2,4}-[0-9]{4}"); // <= こちらでも良い
oM =oP.matcher(otarget);
if (oM.matches()){
isCorrect = true;
}
}
return isCorrect;
}
try{
oStr1 = "03-6267-4000";
oStr2 = "01234-5-6789";
oCheck1 = isCorrectLetter(oStr1);
oCheck2 = isCorrectLetter(oStr2);
oDisp1 = oStr1 + "は電話番号? => " + oCheck1
oDisp2 = oStr2 + "は電話番号? => " + oCheck2
JOptionPane.showMessageDialog(null, oDisp1 + "\n" + oDisp2);
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//正規表現
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.util.regex.Matcher);
importClass(Packages.java.util.regex.Pattern);
function isCorrectLetter(otarget){
isCorrect = false;
if(otarget != null){
oP = Pattern.compile("[\\w]+@[\\w-][.\\w]+");
oM =oP.matcher(otarget);
if (oM.matches()){
isCorrect = true;
}
}
return isCorrect;
}
try{
oStr1 = "test_@test.co.jp";
oStr2 = "test*@aaa.com";
oCheck1 = isCorrectLetter(oStr1);
oCheck2 = isCorrectLetter(oStr2);
oDisp1 = oStr1 + "は不正文字の使用は無い? => " + oCheck1
oDisp2 = oStr2 + "は不正文字の使用は無い? => " + oCheck2
JOptionPane.showMessageDialog(null, oDisp1 + "\n" + oDisp2);
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
Function
// Define Function
importClass(Packages.javax.swing.JOptionPane);
//
function func1(fp1,fp2){
return fp1 + " = " + fp2
};
oInt1 = "Macro_Test" ;
oInt2 = 1.0 ;
exmp1 = func1("oInt1",oInt1);
exmp2 = func1("oInt2",oInt2);
JOptionPane.showMessageDialog(null, exmp1);
JOptionPane.showMessageDialog(null, exmp2);
JOptionPane.showMessageDialog(null, "Success");
Object
// Rhino
importClass(Packages.org.mozilla.javascript.Context);
importClass(Packages.org.mozilla.javascript.Scriptable);
// JavaScriptの実行に必要なObject
ctx = Context.enter();
scope = ctx.initStandardObjects();
// JavaScriptを実行
result = ctx.evaluateString( scope, "function add(a, b) {return a+b} add(5.4,3.8)",
"RhinoExample",0,null);
// 実行結果を表示
rNum = Context.toString( result );
// JavaScriptの実行を終了
Context.exit();
// Rhino2
importClass(Packages.org.mozilla.javascript.Context);
importClass(Packages.org.mozilla.javascript.Scriptable);
// JavaScriptの実行に必要なObject
ctx = Context.enter();
scope = ctx.initStandardObjects();
//Initial Value
oInt1 = 10;
oInt2 = 20;
// Define Function
function func(a, b) {return a+b} ;
// Show the result of Funtion "func" on New Context
result = ctx.evaluateString( scope, func(oInt1, oInt2),"RhinoExample",0,null); // New Contextにfunc(oInt1,oInt2)の結果を表示
// JavaScriptの実行を終了
Context.exit();
// Rhino2
importClass(Packages.org.mozilla.javascript.Context);
importClass(Packages.org.mozilla.javascript.Scriptable);
importClass(Packages.javax.swing.JOptionPane);
// JavaScriptの実行に必要なObject
ctx = Context.enter();
scope = ctx.initStandardObjects();
//Initial Value
oInt1 = 10;
oInt2 = 20;
// Define Function
function func(a, b) {return a+b} ;
// Show the result of Funtion "func" on New Context
result = ctx.evaluateString( scope, "function add(a, b) {return a+b} add(5.4,3.8)",
"RhinoExample",0,null); // Contextにadd(oInt1,oInt2)の結果を表示
// JavaScriptの実行を終了
Context.exit();
// Rhino Class
importClass(Packages.org.mozilla.javascript.Context);
importClass(Packages.org.mozilla.javascript.Scriptable);
// JavaScriptの実行に必要なObject
ctx = Context.enter();
scope = ctx.initStandardObjects();
// JavaScriptを実行
result = ctx.evaluateString( scope, "function add(a, b) {return a+b} add(5.4,3.8)",
"RhinoExample",0,null);
// 実行結果のJava Class Nameを取得
rJavaClassName = result.getClass().getName();
JOptionPane.showMessageDialog(null, rJavaClassName)
// JavaScriptの実行を終了
Context.exit();
//[ Note ]
//引数がIntegerでも戻り値はDoubleになる。
File
[ File操作 ]
// File Exists
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
JOptionPane.showMessageDialog(null, oFile + " は存在します");
}
// File to be Renamed
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test1.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
if(oFile.renameTo(new File("C:/temp/test2.csv"))){
JOptionPane.showMessageDialog(null, oFile + " のFile名を変更しました。");
}
}
// File to be Create
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test1.csv");
oExists = oFile.exists();
if(oExists){
JOptionPane.showMessageDialog(null, oFile + " は既に存在します!!");
}else{
if(oFile.createNewFile()){
JOptionPane.showMessageDialog(null, oFile + " の作成に成功しました。");
}
}
// File Exists
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test_test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
delete oFile
oEx=oFile.exists();
if(!oEx){
JOptionPane.showMessageDialog(null, oFile + " を削除しました。");
}else{
JOptionPane.showMessageDialog(null, oFile + " は削除出来ません。");
}
}
}catch(e){
//Error
JOptionPane.showMessageDialog(null, "Error発生");
}
// Directory to be Create
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oDir = File("C:/temp/test_D");
oExists = oDir.exists();
if(oExists){
JOptionPane.showMessageDialog(null, oDir + " は既に存在します!!");
}else{
if(oDir.mkdir()){
JOptionPane.showMessageDialog(null, oDir + " の作成に成功しました。");
}
}
[ File情報 ]
<< 取得・確認 >>
// File to be Check case1
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test1.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
if(oFile.canRead()){
JOptionPane.showMessageDialog(null, oFile + " は「読み込み可能」です。");
}else{
JOptionPane.showMessageDialog(null, oFile + " は「読み込み可能」ではありません。");
}
}
// File to be Check case1b
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test2.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
if(oFile.canWrite()){
JOptionPane.showMessageDialog(null, oFile + " は「書き込み可能」です。");
}else{
JOptionPane.showMessageDialog(null, oFile + " は「書き込み可能」ではありません。");
}
}
// File to be Check case2
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test1.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
if(oFile.isFile()){
JOptionPane.showMessageDialog(null, oFile + " は「File」です。");
}else{
JOptionPane.showMessageDialog(null, oFile + " は「File」ではありません。");
}
}
// File to be Check case3
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test1.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
if(oFile.isDirectory()){
JOptionPane.showMessageDialog(null, oFile + " は「Directory」です。");
}else{
JOptionPane.showMessageDialog(null, oFile + " は「Directory」ではありません。");
}
}
// File
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oHidden = oFile.isHidden();
if(oHidden){
JOptionPane.showMessageDialog(null, oFile + " はHidden Fileです。");
}else{
JOptionPane.showMessageDialog(null, oFile + " はHidden Fileではありません。");
}
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "Error発生");
}
// File
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("test.csv");
oAbsolute = oFile.isAbsolute();
if(oAbsolute){
JOptionPane.showMessageDialog(null, oFile + " のPathは絶対Pathです。");
}else{
JOptionPane.showMessageDialog(null, oFile + " のPathは絶対Pathではありません。");
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "Error発生");
}
// Get File Name
importPackage(java.io);
importClass(Packages.javax.swing.JOptionPane);
oFile = File("C:/temp/test.csv");
oName = oFile.getName();
JOptionPane.showMessageDialog(null, oName);
// File
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oP = oFile.getAbsolutePath();
oDisp = "[ getAbsolutePath() ] => " + oP;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// File
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oP = oFile.getPath();
oDisp = "[ getPath() ] => " + oP;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// File
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oP = oFile.getParent();
oDisp = "[ getParent() ] => " + oP;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// File
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.java.text.SimpleDateFormat);
importClass(Packages.java.util.Calendar);
importClass(Packages.javax.swing.JOptionPane);
//Long type => Date Type
function convertDate(lo){
cal = Calendar.getInstance();
sf = new SimpleDateFormat("yyyy/MM/dd");
return sf.format(cal.getTime());
}
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oM = oFile.lastModified();
oMD = convertDate(oM);
oDisp = "[ lastModified() ] => " + oMD;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// List
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oDir = File("C:/temp/rhino");
oExists = oDir.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oDir + " は存在しません!!");
}else{
oL = oDir.list();
oC = oL.length
oDisp = "[ Directory内のFile&Directory数 ]" + "\n" +" => " + oC;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// List
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oDir = File("C:/temp/rhino");
oExists = oDir.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oDir + " は存在しません!!");
}else{
oL = oDir.list();
oC = oL.length;
for(i=0 ; i < oC ; i++){
oDisp = oL[i];
JOptionPane.showMessageDialog(null, oDisp);
}
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// List
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oDir = File("C:/temp/rhino");
oExists = oDir.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oDir + " は存在しません!!");
}else{
oL = oDir.listFiles();
oC = oL.length;
for(i=0 ; i < oC ; i++){
oDisp = oL[i];
JOptionPane.showMessageDialog(null, oDisp);
}
// JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// Separator
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oP = oFile.pathSeparator;
oDisp = "[ path separator ] => " + oP;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// Separator
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oP = oFile.separator;
oDisp = "[ path separator ] => " + oP;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
<< 設定 >>
// setRreadOnly
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oFile = File("C:/temp/test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oFile.setReadOnly();
//Confirm
oDisp = "[ Not ReadOnly? ] => " + oFile.canWrite();
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// setLastModified
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.java.text.SimpleDateFormat);
importClass(Packages.java.util.Calendar);
importPackage(java.util.date);
importClass(Packages.javax.swing.JOptionPane);
//Long type => Date Type
function convertDate(lo){
cal = Calendar.getInstance();
sf = new SimpleDateFormat("yyyy/MM/dd");
return sf.format(cal.getTime());
}
//
try{
oFile = File("C:/temp/test_test.csv");
oExists = oFile.exists();
if(!oExists){
JOptionPane.showMessageDialog(null, oFile + " は存在しません!!");
}else{
oDate = new Date();
oMT = oDate.getSeconds();
oFile.setLastModified(oMT);
// Confirm
oM = oFile.lastModified();
oMD = convertDate(oM);
oDisp = "[ lastModified() ] => " + oMD;
JOptionPane.showMessageDialog(null, oDisp);
}
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
[ Drive ]
// Drive
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oDrive = File("C:/");
oP = oDrive.getTotalSpace();
oDisp = oP + "[ bytes ]";
JOptionPane.showMessageDialog(null, oDisp);
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// Drive
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oDrive = File("C:/");
oP = oDrive.getFreeSpace();
oDisp = oP + "[ bytes ]";
JOptionPane.showMessageDialog(null, oDisp);
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
// Drive
importClass(Packages.java.io.File);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//
try{
oDrive = File("C:/");
oP = oDrive.getUsableSpace();
oDisp = oP + "[ bytes ]";
JOptionPane.showMessageDialog(null, oDisp);
}catch(IOException){
//例外処理
JOptionPane.showMessageDialog(null, "例外発生");
}
[ Stream ]
<< Byte Stream >>
// Stream
//in.txt / out.txtは事前に作成済み(out.txtはEmpty file)
importClass(Packages.java.io.BufferedInputStream);
importClass(Packages.java.io.BufferedOutputStream);
importClass(Packages.java.io.File);
importClass(Packages.java.io.FileInputStream);
importClass(Packages.java.io.FileOutputStream);
importClass(Packages.java.io.FileNotFoundException);
importClass(Packages.java.io.IOException);
importClass(Packages.javax.swing.JOptionPane);
//入力用のfile objectを生成
inputfile = new File("c:/temp/Rhino/tmp/in.txt");
//出力用のfile objectを生成
outputfile = new File("c:/temp/Rhino/tmp/out.txt")
//Finaly句にてStreamのclose処理をする為にtry句の外にstreamを定義する
inputputStream = null;
outputStream = null;
try{
//入力fileからbufferに蓄えるstream生成
inFS = new FileInputStream(inputfile);
inputStream = new BufferedInputStream(inFS);
//bufferに蓄えたbyteを出力fileへ書込むstream生成
outFS = new FileOutputStream(outputfile);
outputStream = new BufferedOutputStream(outFS);
//入力fileからbyte読込
//出力fileにdefaultのbuffer単位で書出
while((bytes=inputStream.read()) != -1){
outputStream.write(bytes);
}
JOptionPane.showMessageDialog(null, "success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}finally{
// Streamのclose処理をfillay句で行う
if(inputStream != null){
try{
inputStream.close();
}catch(IOException){
ename1 = e.name;
emsg1 = e.message;
eDisp1= ename1 + "\n" + emsg1;
JOptionPane.showMessageDialog(null, eDisp1);
}
}
if(outputStream != null){
try{
outputStream.close();
}catch(IOException){
ename2 = e.name;
emsg2 = e.message;
eDisp2= ename2 + "\n" + emsg2;
JOptionPane.showMessageDialog(null, eDisp2);
}
}
JOptionPane.showMessageDialog(null, "Closed Stream");
}
// Char Stream
//in.txt / out.txtは事前に作成済み(out.txtはEmpty file)
importClass(Packages.java.io.BufferedReader);
importClass(Packages.java.io.BufferedWriter);
importClass(Packages.java.io.File);
importClass(Packages.java.io.FileInputStream);
importClass(Packages.java.io.FileOutputStream);
importClass(Packages.java.io.FileNotFoundException);
importClass(Packages.java.io.IOException);
importClass(Packages.java.io.InputStreamReader);
importClass(Packages.java.io.OutputStreamWriter);
importClass(Packages.javax.swing.JOptionPane);
//入力用のfile objectを生成
inputfile = new File("c:/temp/rhino/temp/in.txt");
//出力用のfile objectを生成
outputfile = new File("c:/temp/rhino/temp/out.txt")
//Finaly句にてStreamのclose処理をする為にtry句の外にstreamを定義する
reader = null;
writer = null;
try{
//入力fileからBufferInputStreamにて蓄えるstream生成
inFS = new FileInputStream(inputfile);
intext= new InputStreamReader(inFS, "SJIS"); // "UTF-8"
reader = new BufferedReader(intext);
//bufferに蓄えたbyteを出力fileへ書込むstream生成
outFS = new FileOutputStream(outputfile);
outtext = new OutputStreamWriter(outFS, "SJIS"); // "UTF-8"
writer = new BufferedWriter(outtext);
//入力fileから1 Line 分byte読込
//出力fileに1 Line defaultのbuffer単位で書出
line = null
while((line=reader.readLine()) != null){
writer.write(line);
writer.newLine();
}
JOptionPane.showMessageDialog(null, "success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}finally{
// Streamのclose処理をfillay句で行う
if(reader != null){
try{
reader.close();
}catch(IOException){
ename1 = e.name;
emsg1 = e.message;
eDisp1= ename1 + "\n" + emsg1;
JOptionPane.showMessageDialog(null, eDisp1);
}
}
if(writer != null){
try{
writer.close();
}catch(IOException){
ename2 = e.name;
emsg2 = e.message;
eDisp2= ename2 + "\n" + emsg2;
JOptionPane.showMessageDialog(null, eDisp2);
}
}
JOptionPane.showMessageDialog(null, "Closed Stream");
}
Thead
//thread
importClass(Packages.java.lang.Thread);
importClass(Packages.javax.swing.JOptionPane);
//
try{
sleeptime = 5000; // 5sec
Thread.sleep(sleeptime);
JOptionPane.showMessageDialog(null, "Success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
Utility
//HashMap
var map = new java.util.HashMap();
map.put("7788", "scott");
map.put("7839", "king");
a=map.get("7788");
b=map.keySet();
importClass(Packages.javax.swing.JOptionPane);
JOptionPane.showMessageDialog(null, a+"\n"+b);
//Map Size
importPackage(java.util.map);
var map = new java.util.HashMap();
map.put("7788", "scott");
map.put("7839", "king");
a=map.get("7788");
b=map.keySet();
map_length = map.size();
importClass(Packages.javax.swing.JOptionPane);
JOptionPane.showMessageDialog(null, "Keyの値の数 : "+map_length);
//Remove Map
importPackage(java.util.map);
importClass(Packages.javax.swing.JOptionPane);
// HasMap 初期化
var map = new java.util.HashMap();
//key
keys = new Array("060", "050", "040", "030", "020", "010");
//value
vals = new Array("1番", "2番", "3番", "4番", "5番", "6番");
//set HashMap
for( i = 0; i < keys.length; i++ ){
map.put(keys[i],vals[i]);
}
//Display initial
map_length = map.size();
JOptionPane.showMessageDialog(null, "Init Keyの値の数 : "+ map_length
+ "\n" +
"Keyの値 : "+map);
//remove key="040"
map.remove("040");
//Display Modified
map_length = map.size();
JOptionPane.showMessageDialog(null, "Modified Keyの値の数 : "+ map_length
+ "\n" +
"Keyの値 : "+map);
//Clear Map
importPackage(java.util.map);
importClass(Packages.javax.swing.JOptionPane);
// HasMap 初期化
var map = new java.util.HashMap();
//key
keys = new Array("060", "050", "040", "030", "020", "010");
//value
vals = new Array("1番", "2番", "3番", "4番", "5番", "6番");
//set HashMap
for( i = 0; i < keys.length; i++ ){
map.put(keys[i],vals[i]);
}
//Display initial
map_length = map.size();
JOptionPane.showMessageDialog(null, "Init Keyの値の数 : "+ map_length
+ "\n" +
"Keyの値 : "+map);
//remove key="040"
map.clear();
//Display Modified
map_length = map.size();
JOptionPane.showMessageDialog(null, "Modified Keyの値の数 : "+ map_length
+ "\n" +
"Keyの値 : "+map);
//Check Map
importPackage(java.util.map);
importClass(Packages.javax.swing.JOptionPane);
// HasMap 初期化
var map = new java.util.HashMap();
//key
keys = new Array("060", "050", "040", "030", "020", "010");
//value
vals = new Array("1番", "2番", "3番", "4番", "5番", "6番");
//set HashMap
for( i = 0; i < keys.length; i++ ){
map.put(keys[i],vals[i]);
}
//Check key
checkkey = "030"
ckey = map.containsKey(checkkey);
if(ckey){
JOptionPane.showMessageDialog(null, checkkey + " は存在します。");
} else {
JOptionPane.showMessageDialog(null, checkkey + " は存在しません。");
}
//Check value
checkvalue = "7番"
cValue = map.containsValue(checkvalue);
if(cValue){
JOptionPane.showMessageDialog(null, checkvalue + " は存在します。");
} else {
JOptionPane.showMessageDialog(null, checkvalue + " は存在しません。");
}
// java.util.Hashtable / java.util.Enumeration
importClass(java.util.Hashtable);
importClass(java.util.Enumeration);
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.org.mozilla.javascript.Context);
importClass(Packages.org.mozilla.javascript.Scriptable);
// JavaScriptの実行に必要なObject
ctx = Context.enter();
scope = ctx.initStandardObjects();
jArr = new Array(200,500,600)
numbers = new Hashtable();
numbers.put("mikan", jArr[0]);
numbers.put("ringo", jArr[1]);
numbers.put("budou", jArr[2]);
e = numbers.elements();
while (e.hasMoreElements()){
// JavaScriptを実行
result = ctx.evaluateString( scope, e.nextElement(),
"RhinoExample",0,null)
};
// 実行結果を表示
rNum = Context.toString( result );
// JavaScriptの実行を終了
Context.exit();
JOptionPane.showMessageDialog(null, "Success");
// Zip
//in.txt は事前に作成済み
importClass(Packages.java.io.File);
importClass(Packages.java.io.FileInputStream);
importClass(Packages.java.io.FileOutputStream);
importClass(Packages.java.io.IOException);
importClass(Packages.java.util.zip.DeflaterInputStream);
importClass(Packages.java.util.zip.InflaterOutputStream);
importClass(Packages.javax.swing.JOptionPane);
//入力用のfile objectを生成
inputfile = new File("c:/temp/rhino/temp/in.txt");
//出力用のfile objectを生成
outputfile = new File("c:/temp/rhino/temp/outLIB.txt")
//
try{
// text file読み込み圧縮したStreamにする
inFS = new FileInputStream(inputfile)
inDeflater = new DeflaterInputStream(inFS);
// 圧縮されたStreamを解凍して text fileへ書込む
outFS = new FileOutputStream(outputfile);
outInflater = new InflaterOutputStream(outFS);
//実際にStreamを読み込み、同時に書き出す
while((bytes = inDeflater.read()) != -1){
outInflater.write(bytes);
}
JOptionPane.showMessageDialog(null, "success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//merory
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.lang.Runtime);
//
try{
oruntime = Runtime.getRuntime();
oM1 = oruntime.totalMemory() / 1000000; // <=MB
oDisp1= "Avaiable Memory1 =>" + oM1 + "MB";
JOptionPane.showMessageDialog(null, oDisp1);
test = 0;
for(i=0; i<10000; i++){
test = test + i;
}
oM2 = oruntime.freeMemory() / 1000000; // <=MB
oDisp2= "Avaiable Memory2 =>" + oM2 + "MB";
JOptionPane.showMessageDialog(null, oDisp2);
JOptionPane.showMessageDialog(null, "Success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//merory
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.java.lang.Runtime);
//
try{
oruntime = Runtime.getRuntime();
oM1 = oruntime.totalMemory() / 1000000; // <=MB
oDisp1= "Jva仮想マシンのメモリ総容量 =>" + oM1 + "MB";
JOptionPane.showMessageDialog(null, oDisp1);
test = 0;
for(i=0; i<10000; i++){
test = test + i;
}
oM2 = oruntime.freeMemory() / 1000000; // <=MB
oDisp2= "Jva仮想マシンのFreeメモリ総容量 =>" + oM2 + "MB";
JOptionPane.showMessageDialog(null, oDisp2);
JOptionPane.showMessageDialog(null, "Success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
Swing
[ Frame ]
// Rhino Aplet Frame
importClass(Packages.org.mozilla.javascript.Context);
importClass(Packages.org.mozilla.javascript.Scriptable);
importClass(Packages.org.mozilla.javascript.ScriptableObject);
importClass(Packages.javax.swing.JOptionPane);
// JavaScriptの実行に必要なObject
ctx = Context.enter();
scope = ctx.initStandardObjects();
// 表示させるFrame(Window)
ScriptableObject.putProperty( scope, "f", new Packages.javax.swing.JFrame("Frame") );
// Applet FrameをJavaScriptで表示
result = ctx.evaluateString( scope, "f.setSize(200,200);"
+ "f.defaultCloseOperation = Packages.javax.swing.JFrame.DISPOSE_ON_CLOSE;" // <= Appletの終了
+ "f.visible = true;"
,"RhinoExample",0,null);
// JavaScriptの実行を終了
Context.exit();
//
// DO_NOTHING_ON_CLOSE : Do not do anything when the user requests that the window close.
// HIDE_ON_CLOSE : Hide the window when the user closes it.
// DISPOSE_ON_CLOSE : Hide and dispose of the window when the user closes it.
// EXIT_ON_CLOSE : Exit the application, using System.exit(0). This is recommended for applications only.
// Color Smile Mark
var awt = java.awt;
var event = awt.event;
var graphics = awt.Graphics;
var system = java.lang.System;
var smileyCanvas = new JavaAdapter(awt.Canvas, {
paint: function (g) {
var size = this.getSize();
var d = Math.min(size.width, size.height);
var ed = d / 20;
var x = (size.width - d) / 2;
var y = (size.height - d) / 2;
// draw head (color already set to foreground)
g.fillOval(x, y, d, d);
g.setColor(awt.Color.black);
g.drawOval(x, y, d, d);
// draw eyes
g.fillOval(x+d/3-(ed/2), y+d/3-(ed/2), ed, ed);
g.fillOval(x+(2*(d/3))-(ed/2), y+d/3-(ed/2), ed, ed);
//draw mouth
g.drawArc(x+d/4, y+2*(d/5), d/2, d/3, 0, -180);
}
});
smileyCanvas.setForeground(awt.Color.yellow);
var winAdapter = new JavaAdapter(event.WindowAdapter, {
windowClosing: function (evt) {
system.exit(0);
}
});
var f = new awt.Frame('Have a nice day');
f.addWindowListener(winAdapter);
f.add(smileyCanvas, awt.BorderLayout.CENTER);
f.pack();
f.show();
f.setSize(new awt.Dimension(320, 240));
// Color Smile Mark
var awt = java.awt;
var event = awt.event;
var graphics = awt.Graphics;
var system = java.lang.System;
var smileyCanvas = new JavaAdapter(awt.Canvas, {
paint: function (g) {
var size = this.getSize();
var d = Math.min(size.width, size.height);
var ed = d / 20;
var x = (size.width - d) / 2;
var y = (size.height - d) / 2;
// draw head (color already set to foreground)
g.fillOval(x, y, d, d);
g.setColor(awt.Color.black);
g.drawOval(x, y, d, d);
// draw eyes
g.fillOval(x+d/3-(ed/2), y+d/3-(ed/2), ed, ed);
g.fillOval(x+(2*(d/3))-(ed/2), y+d/3-(ed/2), ed, ed);
//draw mouth
g.drawArc(x+d/4, y+2*(d/5), d/2, d/3, 0, -180);
}
});
//
smileyCanvas.setForeground(awt.Color.yellow);
var winAdapter = new JavaAdapter(event.WindowAdapter, {
windowClosing: function (evt) {
Context.exit();
}
});
var f = new awt.Frame('Have a nice day');
f.addWindowListener(winAdapter);
f.add(smileyCanvas, awt.BorderLayout.CENTER);
f.pack();
f.show();
f.setSize(new awt.Dimension(320, 240));
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,200,300); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,200,300); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Label
olavel = new JLabel("Label");
basicPanel.add(olavel)
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTextField);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Lavel
olavel = new JLabel("Label");
basicPanel.add(olavel);
//Text Box Size
otext = new JTextField(10);
basicPanel.add(otext);
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTextField);
importClass(Packages.javax.swing.JPasswordField);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Lavel
olavel = new JLabel("Label");
basicPanel.add(olavel);
//Text Box Size
otext = new JTextField(10);
basicPanel.add(otext);
//Password Box Size
opass = new JPasswordField(10);
basicPanel.add(opass)
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTextField);
importClass(Packages.javax.swing.JPasswordField);
importClass(Packages.javax.swing.JTextArea);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Lavel
olavel = new JLabel("Label");
basicPanel.add(olavel);
//Text Box Size
otext = new JTextField(10);
basicPanel.add(otext);
//Password Box Size
opass = new JPasswordField(10);
basicPanel.add(opass)
//Text Area Size (行数,幅)
otextarea = new JTextArea(5,10);
basicPanel.add(otextarea)
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTextField);
importClass(Packages.javax.swing.JPasswordField);
importClass(Packages.javax.swing.JTextArea);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Lavel
olavel = new JLabel("Label");
basicPanel.add(olavel);
//Text Box Size
otext = new JTextField(10);
basicPanel.add(otext);
//Password Box Size
opass = new JPasswordField(10);
basicPanel.add(opass);
//Text Area Size (行数,幅)
otextarea = new JTextArea(5,10);
basicPanel.add(otextarea);
//Bottun
obutton = new JButton("Bottun");
basicPanel.add(obutton);
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTextField);
importClass(Packages.javax.swing.JPasswordField);
importClass(Packages.javax.swing.JTextArea);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JCheckBox);
//importClass(Packages.javax.swing.ButtonGroup);
//importClass(Packages.javax.swing.JRadioButton);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Lavel
olavel = new JLabel("Label");
basicPanel.add(olavel);
//Text Box Size
otext = new JTextField(10);
basicPanel.add(otext);
//Password Box Size
opass = new JPasswordField(10);
basicPanel.add(opass);
//Text Area Size (行数,幅)
otextarea = new JTextArea(5,10);
basicPanel.add(otextarea);
//Bottun
obutton = new JButton("Bottun");
basicPanel.add(obutton);
//Check Box
ocheckbox = new JCheckBox("CheckBox");
basicPanel.add(ocheckbox);
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTextField);
importClass(Packages.javax.swing.JPasswordField);
importClass(Packages.javax.swing.JTextArea);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JCheckBox);
importClass(Packages.javax.swing.JRadioButton);
//importClass(Packages.javax.swing.ButtonGroup);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Lavel
olavel = new JLabel("Label");
basicPanel.add(olavel);
//Text Box Size
otext = new JTextField(10);
basicPanel.add(otext);
//Password Box Size
opass = new JPasswordField(10);
basicPanel.add(opass);
//Text Area Size (行数,幅)
otextarea = new JTextArea(5,10);
basicPanel.add(otextarea);
//Bottun
obutton = new JButton("Bottun");
basicPanel.add(obutton);
//Check Box
ocheckbox = new JCheckBox("CheckBox");
basicPanel.add(ocheckbox);
//Radio Bottun
oradiobottun1 = new JRadioButton("Radio1");
basicPanel.add(oradiobottun1);
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTextField);
importClass(Packages.javax.swing.JPasswordField);
importClass(Packages.javax.swing.JTextArea);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JCheckBox);
importClass(Packages.javax.swing.JRadioButton);
importClass(Packages.javax.swing.ButtonGroup);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
// 新規Panel
basicPanel = new JPanel();
// Lavel
olavel = new JLabel("Label");
basicPanel.add(olavel);
//Text Box Size
otext = new JTextField(10);
basicPanel.add(otext);
//Password Box Size
opass = new JPasswordField(10);
basicPanel.add(opass);
//Text Area Size (行数,幅)
otextarea = new JTextArea(5,10);
basicPanel.add(otextarea);
//Bottun
obutton = new JButton("Bottun");
basicPanel.add(obutton);
//Check Box
ocheckbox = new JCheckBox("CheckBox");
basicPanel.add(ocheckbox);
//Radio Bottun
oradiobutton1 = new JRadioButton("Radio1");
oradiobutton2 = new JRadioButton("Radio2");
//Group化
radiogroup = new ButtonGroup();
radiogroup.add(oradiobutton1);
radiogroup.add(oradiobutton1);
//Panelに追加
basicPanel.add(oradiobutton1);
basicPanel.add(oradiobutton2);
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JMenu);
importClass(Packages.javax.swing.JMenuBar);
importClass(Packages.javax.swing.JMenuItem);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,500,200); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
basicPanel = new JPanel();
// 新規Menu Bar
menubar = new JMenuBar();
// Panelに対して設定
basicPanel.add(menubar);
// menu作成
omenu = new JMenu("File");
menubar.add(omenu);
//menuに追加するcomponentの生成
oitem1 = new JMenuItem("New");
oitem2 = new JMenuItem("Open");
oitem3 = new JMenuItem("Close");
oitem4 = new JMenuItem("Exit");
omenu.add(oitem1);
omenu.add(oitem2);
omenu.add(oitem3);
omenu.add(oitem4);
// FrameにPanelを追加
basicframe.add(basicPanel)
JOptionPane.showMessageDialog(null, "Success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JTree);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,300,500);
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel
basicPanel = new JPanel();
// Tree作成
otree = new JTree();
// Panelに対して設定
basicPanel.add(otree);
// FrameにPanelを追加
basicframe.add(basicPanel);
JOptionPane.showMessageDialog(null, "Success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JTabbedPane);
importClass(Packages.javax.swing.SwingConstants);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,300,500); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Tab Panel作成
tabp = new JTabbedPane(SwingConstants.BOTTOM);
tabp.addTab("Tab1", new JLabel("tab1の内容"));
tabp.addTab("Tab2", new JLabel("=>tab2の内容"));
tabp.addTab("Tab3", new JLabel("=>=>tab3の内容"));
tabp.addTab("Tab4", new JLabel("=>=>=>tab4の内容"));
// FrameにPanelを追加
basicframe.add(tabp);
JOptionPane.showMessageDialog(null, "Success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JPanel);
importClass(Packages.javax.swing.JComboBox);
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,200,500); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame")
// Display Frame
basicframe.setVisible(true)
//Panel作成
basicPanel = new JPanel();
//Combo Box Data
cdata = new Array();
cdata[0] = "徳川家康";
cdata[1] = "徳川秀忠";
cdata[2] = "徳川家光";
cdata[3] = "徳川家綱";
cdata[4] = "徳川綱吉";
cdata[5] = "徳川家宣";
cdata[6] = "徳川家継";
cdata[7] = "徳川吉宗";
cdata[8] = "徳川家重";
cdata[9] = "徳川家治";
cdata[10] = "徳川家斉";
cdata[11] = "徳川家慶";
cdata[12] = "徳川家定";
cdata[13] = "徳川家茂";
cdata[14] = "徳川慶喜";
//
oComboBox = new JComboBox(cdata);
// PanelにCombo Boxを追加
basicPanel.add(oComboBox);
// FrameにPanelを追加
basicframe.add(basicPanel);
JOptionPane.showMessageDialog(null, "Success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
[ Listener ]
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.java.awt.event.ActionEvent);
importClass(Packages.java.awt.event.ActionListener);
importClass(Packages.java.awt.Color);
importClass(Packages.java.awt.event.MouseEvent);
importClass(Packages.java.awt.event.MouseListener);
importClass(Packages.java.awt.event.WindowAdapter);
importClass(Packages.java.awt.event.WindowEvent);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JPanel);
//importClass(Packages.javax.swing.border.LineBorder); // <= error
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,200,300); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame");
// Display Frame
basicframe.setVisible(true);
//Panel
basicPanel = new JPanel();
// oline = new LineBorder(Color.black, 1, true); // <= error
// Initialize Label
resultLabel = new JLabel("結果表示のLabelです");
// resultLabel.setBorder(oline);
basicPanel.add(resultLabel);
//Button
//Buttonに表示する文字
obutton = new JButton("Push Button");
//Buttonが押された場合の処理
obutton.addActionListener(ActionListener({
actionPerformed : function(e){
resultLabel.setText("Buttonが押されました!")
}
})
);
//
//PanelにButton追加
basicPanel.add(obutton);
//FrameにPanel追加
basicframe.add(basicPanel);
JOptionPane.showMessageDialog(null, "Success");
// basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.java.awt.event.ActionEvent);
importClass(Packages.java.awt.event.ActionListener);
importClass(Packages.java.awt.Color);
importClass(Packages.java.awt.event.MouseEvent);
importClass(Packages.java.awt.event.MouseListener);
importClass(Packages.java.awt.event.WindowAdapter);
importClass(Packages.java.awt.event.WindowEvent);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JPanel);
//importClass(Packages.javax.swing.border.LineBorder); // <= error
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,300,100); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame");
// Display Frame
basicframe.setVisible(true);
//Panel
basicPanel = new JPanel();
// oline = new LineBorder(Color.black, 1, true); // <= error
// Initialize Label
resultLabel = new JLabel("このLabelをClickして下さい!!");
// resultLabel.setBorder(oline);
//Labelが押された場合の処理
resultLabel.addMouseListener(MouseListener({
mouseClicked : function(e){
resultLabel.setText("LabelがClickされました!!")
}
})
);
//
//PanelにLabel追加
basicPanel.add(resultLabel);
//FrameにPanel追加
basicframe.add(basicPanel);
JOptionPane.showMessageDialog(null, "Success");
// basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.java.awt.event.ActionEvent);
importClass(Packages.java.awt.event.ActionListener);
importClass(Packages.java.awt.Color);
importClass(Packages.java.awt.event.MouseEvent);
importClass(Packages.java.awt.event.MouseListener);
importClass(Packages.java.awt.event.WindowAdapter);
importClass(Packages.java.awt.event.WindowEvent);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JPanel);
//importClass(Packages.javax.swing.border.LineBorder); // <= error
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,300,100); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame");
// Display Frame
basicframe.setVisible(true);
//Panel
basicPanel = new JPanel();
// oline = new LineBorder(Color.black, 1, true); // <= error
// Initialize Label
resultLabel = new JLabel("このLabelをClickして下さい!!");
// resultLabel.setBorder(oline);
//Labelが押された場合の処理
resultLabel.addMouseListener(MouseListener({
mouseClicked : function(e){
resultLabel.setText("LabelがClickされました!!")
}
})
);
//Mouse CursorがLabel上に乗った時の処理
resultLabel.addMouseListener(MouseListener({
mouseEntered : function(e){
resultLabel.setText("Lavel上にmouse cursorが乗っています。")
}
})
);
//
//PanelにLabel追加
basicPanel.add(resultLabel);
//FrameにPanel追加
basicframe.add(basicPanel);
JOptionPane.showMessageDialog(null, "Success");
// basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.java.awt.event.ActionEvent);
importClass(Packages.java.awt.event.ActionListener);
importClass(Packages.java.awt.Color);
importClass(Packages.java.awt.event.MouseEvent);
importClass(Packages.java.awt.event.MouseListener);
importClass(Packages.java.awt.event.WindowAdapter);
importClass(Packages.java.awt.event.WindowEvent);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JPanel);
//importClass(Packages.javax.swing.border.LineBorder); // <= error
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,300,100); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame");
// Display Frame
basicframe.setVisible(true);
//Panel
basicPanel = new JPanel();
// oline = new LineBorder(Color.black, 1, true); // <= error
// Initialize Label
resultLabel = new JLabel("このLabelをClickして下さい!!");
// resultLabel.setBorder(oline);
//Labelが押された場合の処理
resultLabel.addMouseListener(
new MouseListener({
mouseClicked : function(e){
resultLabel.setText("LabelがClickされました!!")
}
})
);
//Mouse CursorがLabel上に乗った時の処理
resultLabel.addMouseListener(MouseListener({
mouseEntered : function(e){
resultLabel.setText("Lavel上にmouse cursorが乗っています。")
}
})
);
//PanelにLabel追加
basicPanel.add(resultLabel);
//FrameにPanel追加
basicframe.add(basicPanel);
//Mouse CursorがLabel上から外れたの処理
resultLabel.addMouseListener(MouseListener({
mouseExited : function(e){
resultLabel.setText("Lavel上からmouse cursorが外れました。")
}
})
);
//
JOptionPane.showMessageDialog(null, "Success");
// basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
//Swing
importClass(Packages.javax.swing.JFrame);
importClass(Packages.java.awt.event.ActionEvent);
importClass(Packages.java.awt.event.ActionListener);
importClass(Packages.java.awt.Color);
importClass(Packages.java.awt.event.MouseEvent);
importClass(Packages.java.awt.event.MouseListener);
importClass(Packages.java.awt.event.WindowAdapter);
importClass(Packages.java.awt.event.WindowEvent);
importClass(Packages.javax.swing.JButton);
importClass(Packages.javax.swing.JLabel);
importClass(Packages.javax.swing.JPanel);
//importClass(Packages.javax.swing.border.LineBorder); // <= error
importClass(Packages.javax.swing.JOptionPane);
//
try{
//Basic Frame
basicframe = new JFrame();
basicframe.setBounds(10,10,300,100); // (表示位置x,y, Frame Width, Height)
// Titel of Frame
basicframe.setTitle("Title of Frame");
// Display Frame
basicframe.setVisible(true);
//Panel
basicPanel = new JPanel();
// oline = new LineBorder(Color.black, 1, true); // <= error
// Initialize Label
resultLabel = new JLabel("このLabelをClickして下さい!!");
// resultLabel.setBorder(oline);
//LabelがClickされた場合の処理
resultLabel.addMouseListener(
new MouseListener({
mouseClicked : function(e){
resultLabel.setText("LabelがClickされました!!")
}
})
);
//Mouse CursorがLabel上に乗った時の処理
resultLabel.addMouseListener(MouseListener({
mouseEntered : function(e){
resultLabel.setText("Lavel上にmouse cursorが乗っています。")
}
})
);
//PanelにLabel追加
basicPanel.add(resultLabel);
//FrameにPanel追加
basicframe.add(basicPanel);
//Mouse CursorがLabel上から外れたの処理
resultLabel.addMouseListener(MouseListener({
mouseExited : function(e){
resultLabel.setText("Lavel上からmouse cursorが外れました。")
}
})
);
//MouseがLabel上で押されている時の処理
resultLabel.addMouseListener(MouseListener({
mousePressed : function(e){
resultLabel.setText("mouse buttonが押されています")
}
})
);
//MouseがLabel上で押された後に離された時の処理
// resultLabel.addMouseListener(MouseListener({
// mouseReleased : function(e){
// resultLabel.setText("mouse buttonが離されました")
// }
// })
// );
//
JOptionPane.showMessageDialog(null, "Success");
// basicframe.setVisible(false)
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
// Applet Listener
importClass(Packages.javax.swing.JFrame);
importClass(Packages.javax.swing.JOptionPane);
button = new Packages.javax.swing.JButton("ボタンを押して!");
button.addActionListener(new java.awt.event.ActionListener({
actionPerformed : function(e) {
JOptionPane.showMessageDialog(e.source,"Pushed Bottun!!")
}
})
)
frame = new JFrame("JavaScriptでSwing");
frame.setSize(250,100);
frame.defaultCloseOperation = Packages.javax.swing.JFrame.DISPOSE_ON_CLOSE;
frame.contentPane.add( button );
frame.visible = true;
// Context.exit();
NetWork
//Local Network information importClass(Packages.java.net.InetAddress); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ // get information fof Host myadd =InetAddress.getLocalHost(); // get IP Address myIPAdd = myadd.getHostAddress(); // get Host name myHostname = myadd.getHostName(); // Display JOptionPane.showMessageDialog(null, "IP Address : " + myIPAdd + "\n" + "HOST Name : " + myHostname); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//Network information importClass(Packages.java.net.InetAddress); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ // get information fof Host host1 = InetAddress.getByName("www.yahoo.co.jp"); // get IP Address hostIPAdd1 = host1.getHostAddress(); // get Host name Hostname1 = host1.getHostName(); // Display JOptionPane.showMessageDialog(null, "IP Address : " + hostIPAdd1 + "\n" + "HOST Name : " + Hostname1); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//Network information importClass(Packages.java.net.InetAddress); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ // get information fof Host host1 = InetAddress.getByName("208.71.106.43"); // get IP Address hostIPAdd1 = host1.getHostAddress(); // get Host name Hostname1 = host1.getHostName(); // Display JOptionPane.showMessageDialog(null, "IP Address : " + hostIPAdd1 + "\n" + "HOST Name : " + Hostname1); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//Network informations importClass(Packages.java.net.InetAddress); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ // get informations fof Host oHostName= "www.yahoo.com" host = new Array() host = InetAddress.getAllByName(oHostName); // get IP Address oDisp = "[ " + oHostName + " ]" + "\n" + "\n" for(i = 0; i < host.length; i++){ oDisp = oDisp + "IP-" + (i+1) + " : " + host[i].getHostAddress() + "\n"; } // Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.yahoo.com/"); oDisp = "取得成功 => " + url1; oDisp = oDisp + "\n"; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); // try{ //URL構成要素から作成 url1 = new URL("http", "www.google.com", "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); oDisp = "取得成功 => " + url1; oDisp = oDisp + "\n"; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); oDisp = "取得成功 => " + url1; oDisp = oDisp + "\n"; //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); oDisp = oDisp + "取得成功 => " + url2; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getProtocol(); oDisp = " protocol => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getHost(); oDisp = " Host name => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getPort(); oDisp = " Port No. => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getQuery(); oDisp = " Query文字列 => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getPath(); oDisp = " Path => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getFile(); oDisp = " File Name => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getRef(); oDisp = " Anchor name => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.google.com/"); //作成したObjectを利用してURL Objectを作成 url2 = new URL(url1, "webhp?sourceid=mozclient&ie=utf-8&oe=utf-8"); //get protocol oNetInfo = url2.getUserInfo(); oDisp = " UserInfo => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL object infomation importClass(Packages.java.net.URL); importClass(Packages.java.net.MalformedURLException); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org/servlets/StartPage"); //get information oNetInfo = url1.getAuthority(); oDisp = " User権限 => " + oNetInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "取得失敗"); } JOptionPane.showMessageDialog(null, "Success")
//URL Connect importClass(Packages.java.net.URL); importClass(Packages.java.net.URLConnection); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org"); //URL Connection Objectの生成 oURLCon =url1.openConnection(); oDisp = " 接続確立 : " + oURLCon; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (e){ // Error処理 JOptionPane.showMessageDialog(null, "例外発生"); } JOptionPane.showMessageDialog(null, "Success")
//URL Connect Contnt Info importClass(Packages.java.net.URL); importClass(Packages.java.net.URLConnection); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org"); //URL Connection Objectの生成 oURLCon =url1.openConnection() oConInfo = oURLCon.getContentType(); oDisp = " 接続確立 : " + oConInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (e){ // Error処理 JOptionPane.showMessageDialog(null, "Error発生"); } JOptionPane.showMessageDialog(null, "Success")
//URL Connect Contnt Info importClass(Packages.java.net.URL); importClass(Packages.java.net.URLConnection); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org"); //URL Connection Objectの生成 oURLCon =url1.openConnection() oConInfo = oURLCon.getContentLength(); oDisp = " Cobtent Size : " + oConInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (e){ // Error処理 JOptionPane.showMessageDialog(null, "Error発生"); } JOptionPane.showMessageDialog(null, "Success")
//URL Connect Contnt Info importClass(Packages.java.net.URL); importClass(Packages.java.net.URLConnection); importClass(Packages.javax.swing.JOptionPane); try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org"); //URL Connection Objectの生成 oURLCon =url1.openConnection() oConInfo = oURLCon.getContentEncoding(); oDisp = " Cobtent Encoding : " + oConInfo; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (e){ // Error処理 JOptionPane.showMessageDialog(null, "Error発生"); } JOptionPane.showMessageDialog(null, "Success")
//URL Connect Contnt Info importClass(Packages.java.net.URL); importClass(Packages.java.net.URLConnection); importClass(Packages.java.text.SimpleDateFormat); importClass(Packages.java.util.Calendar); importClass(Packages.javax.swing.JOptionPane); //Long type => Date Type function convertDate(lo){ cal = Calendar.getInstance(); sf = new SimpleDateFormat("yyyy/MM/dd"); return sf.format(cal.getTime()); } // try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org"); //URL Connection Objectの生成 oURLCon =url1.openConnection() oConInfo = oURLCon.getDate(); oURLDate = convertDate(oConInfo); oDisp = " Cobtent 送信日 : " + oURLDate; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (e){ // Error処理 JOptionPane.showMessageDialog(null, "Error発生"); } JOptionPane.showMessageDialog(null, "Success")
//URL Connect Contnt Info importClass(Packages.java.net.URL); importClass(Packages.java.net.URLConnection); importClass(Packages.java.text.SimpleDateFormat); importClass(Packages.java.util.Calendar); importClass(Packages.javax.swing.JOptionPane); //Long type => Date Type function convertDate(lo){ cal = Calendar.getInstance(); sf = new SimpleDateFormat("yyyy/MM/dd"); return sf.format(cal.getTime()); } // try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org"); //URL Connection Objectの生成 oURLCon =url1.openConnection() oConInfo = oURLCon.getExpiration(); oURLDate = convertDate(oConInfo); oDisp = " Content 有効期限 : " + oURLDate; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (e){ // Error処理 JOptionPane.showMessageDialog(null, "Error発生"); } JOptionPane.showMessageDialog(null, "Success")
//URL Connect Contnt Info importClass(Packages.java.net.URL); importClass(Packages.java.net.URLConnection); importClass(Packages.java.text.SimpleDateFormat); importClass(Packages.java.util.Calendar); importClass(Packages.javax.swing.JOptionPane); //Long type => Date Type function convertDate(lo){ cal = Calendar.getInstance(); sf = new SimpleDateFormat("yyyy/MM/dd"); return sf.format(cal.getTime()); } // try{ //URLの文字列から生成 url1 = new URL("http://www.openoffice.org"); //URL Connection Objectの生成 oURLCon =url1.openConnection() oConInfo = oURLCon.getLastModified(); oURLDate = convertDate(oConInfo); oDisp = " Content 最終更新日 : " + oURLDate; //Display JOptionPane.showMessageDialog(null, oDisp); }catch (e){ // Error処理 JOptionPane.showMessageDialog(null, "Error発生"); } JOptionPane.showMessageDialog(null, "Success")
//Socket importClass(Packages.java.io.IOException); importClass(Packages.java.net.Socket); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ //Socket Connection作成 sc = new Socket("http://www.openoffice.org", 80); oDisp = "Connection 確立" + sc //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "UnknownHostException発生"); // }catch (IOException){ // 例外処理 // JOptionPane.showMessageDialog(null, "IOException発生"); } JOptionPane.showMessageDialog(null, "Success")
//Socket IP Address importClass(Packages.java.io.IOException); importClass(Packages.java.net.Socket); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ //Socket Connection作成 sc = new Socket("http://www.openoffice.org", 80); sIP = sc.getInetAddress(); oDisp = "接続先Address : " + sIP //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "UnknownHostException発生"); } JOptionPane.showMessageDialog(null, "Success")
//Socket Port importClass(Packages.java.io.IOException); importClass(Packages.java.net.Socket); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ //Socket Connection作成 sc = new Socket("http://www.openoffice.org", 80); sIP = sc.getPort(); oDisp = "接続先Port : " + sIP //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "UnknownHostException発生"); } JOptionPane.showMessageDialog(null, "Success")
//Socket Local Host importClass(Packages.java.io.IOException); importClass(Packages.java.net.Socket); importClass(Packages.java.net.UnknownHostException); importClass(Packages.javax.swing.JOptionPane); try{ //Socket Connection作成 sc = new Socket("http://www.openoffice.org", 80); sIP = sc.getLocalAddress(); oDisp = "Local Address : " + sIP //Display JOptionPane.showMessageDialog(null, oDisp); }catch (UnknownHostException){ // 例外処理 JOptionPane.showMessageDialog(null, "UnknownHostException発生"); } JOptionPane.showMessageDialog(null, "Success")
Shell関数
//Notepad in JavaScript Macro
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.system.XSystemShellExecute);
//
xCompCont = XSCRIPTCONTEXT.getComponentContext();
xMultiComponentFactory = xCompCont.getServiceManager();
//
shell = xMultiComponentFactory.createInstanceWithContext('com.sun.star.system.SystemShellExecute', xCompCont);
xshell = UnoRuntime.queryInterface(XSystemShellExecute, shell);
//
xshell.execute('notepad.exe','C:/temp/dummy.txt',0);
//
JOptionPane.showMessageDialog(null, "Success");
Rhino定義関数
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.org.mozilla.javascript.tools.shell.Global);
s = new Array;
s[0] = "http://openoffice3.web.fc2.com/";
try {
str = Global.readUrl(null, null, s, null);
JOptionPane.showMessageDialog(null, str.substr(0, 100));
} catch(e) {
JOptionPane.showMessageDialog(null, e.toString());
}
XMLHttpRequest()
//XMLHttpRequest
importClass(Packages.javax.swing.JOptionPane);
importClass(Packages.org.mozilla.javascript.tools.shell.Global);
// importPackage(java.org.w3c.dom);
//
function XMLHttpRequest() {
this.requestURL = "";
this.responseText = "";
this.readyState = 0;
this.status = 0;
this.open = function(method, url, async) {
if (method != "GET") {
throw new Error("This XMLHttpRequest supports GET method only.");
}
if (async != false) {
throw new Error("This XMLHttpRequest cannot perform asynchronous read.");
}
this.requestURL = url;
this.responseText = "";
this.readyState = 0;
this.status = 0;
}
this.send = function(query) {
var s;
try {
s = readUrl(this.requestURL);// readUrl is Rhino's primitive
this.status = 200;
} catch (e) {
s = "";
this.status = 1;
}
this.responseText = s;
this.requestURL = null;
this.readyState = 4;
}
}
//XMLHttp Object作成
function createXMLHttpRequest(){
var XMLhttpobject = null;
try{
XMLhttpobject = new XMLHttpRequest();
}catch(e){
try{
}catch(e){
try{
XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
return null;
}
}
}
return XMLhttpObject;
}
//
//
try{
JOptionPane.showMessageDialog(null, "success");
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
Mouse
//mouse importClass(Packages.javax.swing.JOptionPane); importClass(Packages.java.awt.MouseInfo); importClass(Packages.java.awt.PointerInfo); importClass(Packages.org.mozilla.javascript.Context); importClass(Packages.org.mozilla.javascript.Scriptable); try{ ctx = Context.enter(); scope = ctx.initStandardObjects(); pointerInfo = MouseInfo.getPointerInfo(); pos = pointerInfo.getLocation(); JOptionPane.showMessageDialog(null, pos); JOptionPane.showMessageDialog(null, "Success"); }catch(e){ //Error処理 errorname = e.name; errormsg = e.message; eDisp= errorname + "\n" + errormsg; JOptionPane.showMessageDialog(null, eDisp); }
ScriptEngine
//Script Engine importClass(Packages.javax.script.Invocable); importClass(Packages.javax.script.ScriptEngine); importClass(Packages.javax.script.ScriptEngineManager); importClass(Packages.javax.script.ScriptException); // スクリプトエンジンマネージャの生成 manager = new ScriptEngineManager(); // スクリプトエンジンの取得 (JavaScript) engine = manager.getEngineByName("JavaScript"); // スクリプトの評価 engine.eval("print('Hello!!')"); importClass(Packages.javax.swing.JOptionPane); JOptionPane.showMessageDialog(null, "Success");
Tips
[ 入手元 ] 以下のSiteよりExtensionをDL、Install URL : http://hermione.s41.xrea.com/pukiwiki/pukiwiki.php?OOo%2FExt%2FMRI [ 使用例 ] oDoc = XSCRIPTCONTEXT.getDocument(); oDoc.~ に続くMethods / Properties / Interface を調べる。 [ Sample Code ] importClass(Packages.com.sun.star.uno.UnoRuntime); importClass(Packages.com.sun.star.beans.XIntrospection); oDoc = XSCRIPTCONTEXT.getDocument(); xContext = XSCRIPTCONTEXT.getComponentContext(); xMCF = xContext.getServiceManager(); oMRI = xMCF.createInstanceWithContext("mytools.Mri", xContext); xIntrospection = UnoRuntime.queryInterface(XIntrospection, oMRI); xIntrospection.inspect(oDoc);
更に MRI Windowに表示されているmethods,propertiesを調べる時(getDrawPagesの場合) getDrawPages をDouble Clickするとそれに対応するコードが表示される。 macroを扱うものにとって余りに素晴らし過ぎる拡張機能である。
<html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <title></title> </head> <body> <script type="text/Javascript"><!-- //importClass(Packages.javax.swing.JOptionPane); function LaunchApp() { if (!document.all) { alert ("Available only with Internet Explorer."); return; } var ws = new ActiveXObject("WScript.Shell"); ws.Exec("C:\\WINDOWS\\NOTEPAD.EXE"); } //--></script> <form> <Input type="button" value="notepad" OnClick="LaunchApp()"> </form> </body> </html>
[ Rhino Shell ] cd \ set CLASSPATH=Down_load/Rhino/rhino1_7R2/js.jar java org.mozilla.javascript.tools.shell.Main [ quit() ] js>quit() [ JavaScript Macro Execute ] java org.mozilla.javascript.tools.shell.Main -f c:\temp\Rhino\OOo_test2.js
[ Rhino Debugger ]
cd \
set CLASSPATH=Down_load/Rhino/rhino1_7R2/js.jar
java org.mozilla.javascript.tools.debugger.Main
// Error Handling in JavaScript
importClass(Packages.javax.swing.JOptionPane);
try{
var a= CInt(10/3);
JOptionPane.showMessageDialog(null, "a=" + a);
}catch(e){
//Error処理
errorname = e.name;
errormsg = e.message;
eDisp= errorname + "\n" + errormsg;
JOptionPane.showMessageDialog(null, eDisp);
}
RhinoからOpenOffice.org起動
//Boot from Rhino
// コードを実行する前に OOo は次のような引数付きで起動済みです。
// "-accept=socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"
// つまり
// "c:/Program Files\OpenOffice.org 3/program/soffice" "-accept=socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"
//とする
//
importClass(Packages.com.sun.star.uno.XComponentContext);
importClass(Packages.com.sun.star.comp.helper.Bootstrap);
//importClass(Packages.com.sun.star.lang.XMultiComponentFactory);
//importClass(Packages.ooo.connector.BootstrapSocketConnector);
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.bridge.XUnoUrlResolver);
importClass(Packages.com.sun.star.frame.XComponentLoader);
importClass(Packages.com.sun.star.beans.PropertyValue);
importClass(Packages.com.sun.star.text.XTextDocument);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.javax.swing.JOptionPane);
try{
sURL = "uno:socket,host=localhost,port=2083;urp;StarOffice.ComponentContext"
oooFolder = "C:/Program Files/OpenOffice.org 3/program/";
// xRemoteContext = BootstrapSocketConnector.bootstrap(oooFolder);
xRemoteContext = XSCRIPTCONTEXT.getComponentContext();
xRemoteMcf = xRemoteContext.getServiceManager();
xResolver = UnoRuntime.queryInterface(
XUnoUrlResolver, xRemoteMcf.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xRemoteContext));
context = xResolver.resolve(sURL);
if (context != null) {
//java.lang.System.out.println("connected.");
xContext = UnoRuntime.queryInterface(XComponentContext, context);
xMcf = xContext.getServiceManager();
desktop = xMcf.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
xLoader = UnoRuntime.queryInterface(XComponentLoader, desktop);
doc = xLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, new Array());
xTextDoc = UnoRuntime.queryInterface(XTextDocument, doc);
xText = xTextDoc.getText();
xTextRange = UnoRuntime.queryInterface(XTextRange, xText);
xTextRange.setString("Hello.");
}
JOptionPane.showMessageDialog(null, "Success");
}catch (e){
JOptionPane.showMessageDialog(null, "Error");
}