最近安裝新版樹莓派 Raspbian Stretch (2018-11-13) 後注意到 Raspbian 除了內建 Python2 與 Python3 外, 也內建了 JDK 8, 版本是 1.8.0_65 :
pi@raspberrypi:~ $ java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode)
pi@raspberrypi:~ $ javac -version
javac 1.8.0_65
我自 2014 年後就沒有再碰 Java 了 (記得那年六月陪姊姊去南二中參加美術班術科考試時我正忙著測試 Java Swing 的 Data Grid 呢), 因為後來都在用 Javascript 與 Python, 五年多沒用 Java 真的變生疏了. 原本想說就此與 Java 說掰掰分道揚鑣, 但後來覺得藝不嫌多, 已經有的又何必丟掉呢?
既然樹莓派必載 Java, 那麼今天就來複習一下 Java 吧!
首先用 nano 編輯一個 HelloWorld.java 如下 :
pi@raspberrypi:~ $ nano HelloWorld.java
pi@raspberrypi:~ $ cat HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
然後馬上用 javac 編譯為 HelloWorld.class, 再用 java 執行它 :
pi@raspberrypi:~ $ javac HelloWorld.java
pi@raspberrypi:~ $ java HelloWorld
Hello, world!
參考 :
# 葉難: Raspberry Pi與Java 8:Hello World
下面測試 Swing 功能, 參考之前的文章 :
# Java Swing 測試 : JOptionPane 對話框
開啟 nano 貼入範例程式 JOptionPane7.java :
pi@raspberrypi:~ $ nano JOptionPane7.java
pi@raspberrypi:~ $ cat JOptionPane7.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JOptionPane7 implements ActionListener {
JFrame f;
JMenuBar mb;
JMenu jop,messages,options;
JMenuItem[] dialog;
JRadioButtonMenuItem[] mType;
JRadioButtonMenuItem[] oType;
public static void main(String argv[]) {
new JOptionPane7();
}
public JOptionPane7() {
//Setup JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
f=new JFrame("JOptionPane Test");
f.setSize(400,300);
f.setLocationRelativeTo(null);
Container cp=f.getContentPane();
cp.setLayout(null);
f.setVisible(true);
//Build Elements
mb=new JMenuBar();
jop=new JMenu("JOptionPane");
messages=new JMenu("Message Type");
options=new JMenu("Option Type");
dialog=new JMenuItem[4];
dialog[0]=new JMenuItem("setMessageDialog()");
dialog[1]=new JMenuItem("setConfirmDialog()");
dialog[2]=new JMenuItem("setInputDialog()");
dialog[3]=new JMenuItem("setOptionDialog()");
mType=new JRadioButtonMenuItem[5];
mType[0]=new JRadioButtonMenuItem("ERROR_MESSAGE");
mType[1]=new JRadioButtonMenuItem("INFORMATION_MESSAGE");
mType[2]=new JRadioButtonMenuItem("WARNING_MESSAGE");
mType[3]=new JRadioButtonMenuItem("QUESTION_MESSAGE");
mType[4]=new JRadioButtonMenuItem("PLAIN_MESSAGE");
oType=new JRadioButtonMenuItem[4];
oType[0]=new JRadioButtonMenuItem("DEFAULT_OPTION");
oType[1]=new JRadioButtonMenuItem("YES_NO_OPTION");
oType[2]=new JRadioButtonMenuItem("YES_NO_CANCEL_OPTION");
oType[3]=new JRadioButtonMenuItem("OK_CANCEL_OPTION");
for (int i=0; i<dialog.length; i++) {
jop.add(dialog[i]);
dialog[i].addActionListener(this);
}
jop.addSeparator();
ButtonGroup mgroup=new ButtonGroup();
for (int i=0; i<mType.length; i++) {
messages.add(mType[i]);
mgroup.add(mType[i]);
mType[i].addActionListener(this);
if (i==0) {mType[i].setSelected(true);}
}
ButtonGroup ogroup=new ButtonGroup();
for (int i=0; i<oType.length; i++) {
options.add(oType[i]);
ogroup.add(oType[i]);
oType[i].addActionListener(this);
if (i==0) {oType[i].setSelected(true);}
}
jop.add(messages);
jop.add(options);
mb.add(jop);
f.setJMenuBar(mb);
//Close JFrame
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowHandler(f));
}
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
if (cmd.equals("setMessageDialog()")) {
int mtype=getMessageType();
JOptionPane.showMessageDialog(f,"Hello World! 您好","資訊",mtype);
}
if (cmd.equals("setConfirmDialog()")) {
int mtype=getMessageType();
int otype=getOptionType();
int opt=JOptionPane.showConfirmDialog(f,cmd,"確認",otype,mtype);
showSelectedOption(opt);
}
if (cmd.equals("setInputDialog()")) {
int mtype=getMessageType();
String input=JOptionPane.showInputDialog(f,cmd,"輸入",mtype);
JOptionPane.showMessageDialog(f,"您輸入的是 : " + input);
}
if (cmd.equals("setOptionDialog()")) {
String[] options={"接受","不接受","取消"};
int mtype=getMessageType();
int otype=getOptionType();
int opt=JOptionPane.showOptionDialog(f,cmd,"請選擇",otype,mtype,null,
options,"接受");
showSelectedOption(opt);
}
}
public int getMessageType() {
int type=1;
for (int i=0; i<mType.length; i++) {
if (mType[i].isSelected()) {
switch (mType[i].getText()) {
case "ERROR_MESSAGE" : type=0;break;
case "INFORMATION_MESSAGE" : type=1;break;
case "WARNING_MESSAGE" : type=2;break;
case "QUESTION_MESSAGE" : type=3;break;
case "PLAIN_MESSAGE" : type=-1;break;
}
}
}
return type;
}
public int getOptionType() {
int type=1;
for (int i=0; i<oType.length; i++) {
if (oType[i].isSelected()) {
switch (oType[i].getText()) {
case "DEFAULT_OPTION" : type=-1;break;
case "YES_NO_OPTION" : type=0;break;
case "YES_NO_CANCEL_OPTION" : type=1;break;
case "OK_CANCEL_OPTION" : type=2;break;
}
}
}
return type;
}
public void showSelectedOption(int opt) {
if (opt==JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(f,"您按了 YES (是) " + opt);
}
else if (opt==JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(f,"您按了 NO (否) " + opt);
}
else if (opt==JOptionPane.CANCEL_OPTION) {
JOptionPane.showMessageDialog(f,"您按了 CANCEL (取消) " + opt);
}
else if (opt==JOptionPane.CLOSED_OPTION) {
JOptionPane.showMessageDialog(f,"您按了 CLOSE (關閉) " + opt);
}
else if (opt==JOptionPane.OK_OPTION) {
JOptionPane.showMessageDialog(f,"您按了 OK (確定) " + opt);
}
}
}
class WindowHandler extends WindowAdapter {
JFrame f;
public WindowHandler(JFrame f) {this.f=f;}
public void windowClosing(WindowEvent e) {
int result=JOptionPane.showConfirmDialog(f,
"確定要結束程式嗎?",
"確認訊息",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result==JOptionPane.YES_OPTION) {System.exit(0);}
}
}
pi@raspberrypi:~ $ javac JOptionPane7.java
pi@raspberrypi:~ $ java JOptionPane7
Swing 發展已經非常成熟, 我那時是在 Java 6 上寫的測試程式, 過這麼久了在 Java 8 也是沒問題的.
沒有留言 :
張貼留言