月曜日, 1月 21, 2008

Suppose you want to use my OnScreenKeyboard class.

What is good about Java is, as they say -- write once, run anywhere. It means that the everything you write won't be wasted in a way that not only you can have it but also that you have it on your way. Java classes are extendable. Only thing that the programmer must be aware of is to set the classpath right.

In case you want to use my OnScreenKeyboard class, download (easai.jar).
The OnScreenKeyboard class is in easai.util package so import the package to use the class.

import easai.util.*;

...

OnScreenKeyboard keyboard
=new OnScreenKeyboard("Taiwanese",this);
keyboard.init();

OnScreenKeyboard constructor takes a string argument that specifies the language and an ActionListener. This ActionListener is a wonderful thing that makes this component usable for any of the application you wish.
You just have to set the ActionListener to receive all the output from the OnScreenKeyboard application.


public void actionPerformed (ActionEvent e)
{
String command=e.getActionCommand();
String ch=command.substring(0,1);
int pos=textArea.getCaretPosition();
textArea.insert(ch,pos);
}



Please have a look at this application that uses the OnScreenKeyboard class if what is describe above is not entirely clear to you.

/* TaiwaneseApplet.java -- Zhuyin on screen keyboard
*
* Copyright (C) 2008 Erica Asai

* This program is free software;
* you can redistribute it and/or
* modify it under the terms of the
* GNU General Public License
* as published by the Free Software
* Foundation; either version 2
* of the License, or (at your option)
* any later version.
*
* This program is distributed in
* the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without
* even the implied warranty of
* MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the
* GNU General Public License
* for more details.
*
* You should have received a copy
* of the GNU General Public License
* along with this program; if not,
* write to the Free Software
* Foundation, Inc., 59 Temple Place
* - Suite 330, Boston, MA 02111-1307, USA.
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import easai.util.*;

public class TaiwaneseApplet extends JApplet implements ActionListener
{
JTextArea textArea=new JTextArea(15,50);
JButton clear=new JButton("Clear");
JButton larger=new JButton("Font Size (+)");
JButton smaller=new JButton("Font Size (-)");
OnScreenKeyboard keyboard
=new OnScreenKeyboard("Taiwanese",this);

public void init()
{
keyboard.nButtons=10;
keyboard.init();

clear.addActionListener(this);
larger.addActionListener(this);
smaller.addActionListener(this);

JPanel control=new JPanel();
control.setLayout(new FlowLayout());
control.add(larger);
control.add(smaller);

JPanel textPanel=new JPanel();
JScrollPane textScroll=new JScrollPane(textArea);
textPanel.add(textScroll);
textPanel.add(clear);

JPanel panel=new JPanel();
BorderLayout layout=new BorderLayout();
panel.setLayout(layout);

JPanel keyboardPanel=new JPanel();
keyboardPanel.setLayout(new BorderLayout());
keyboardPanel.add(keyboard,BorderLayout.CENTER);
keyboardPanel.add(control,BorderLayout.SOUTH);

panel.add(keyboardPanel,BorderLayout.NORTH);
panel.add(textPanel,BorderLayout.CENTER);

JScrollPane scroll=new JScrollPane(panel);
add(scroll);
}

public void actionPerformed(ActionEvent e)
{
String command=e.getActionCommand();
if(0<command.length())
{
if(command.equals("Clear"))
{
textArea.setText("");
}
else if(command.equals("Font Size (+)"))
{
keyboard.resizeFont(2);
}
else if(command.equals("Font Size (-)"))
{
keyboard.resizeFont(-2);
}
else
{
String ch=command.substring(0,1);
int pos=textArea.getCaretPosition();
textArea.insert(ch,pos);
}
}
}
}



The way to set the classpath right is a bit technical. The package name of the OnScreenKeyboard class is "easai.util". As that all the Java classes are organized in the most familiar way of directories, You must store the class file in the directory named easai/util as follows.


+--easai--util
--+
+--your Java application directory


Then compile your Java application with a proper classpath option.


javac -classpath ".;.." *.java


Visit this site (http://easai.00freehost.com/Zhuyin/index.html) to see how it runs.

Qt: 外部プログラムを起動する

  Qt/C++ のアプリは、外部へ直接アクセスできます。これはネットアプリでは不可能な Qt のメリットです。 外部プログラムを起動することもできます。QProcess::startDetached() を使うと独立したプロセスを立ち上げることができます。 この QProces...