***************这是主界面
package ch01.setion3.other;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class Japanese extends JFrame {
private JButton buttonQuestion = new JButton("点击出题");
private JButton buttonAnswer = new JButton("显示答案");
private JButton buttonChange = new JButton("清空内容");
private JTextArea areaQuestion = new JTextArea(10,200);
private JTextArea areaReturn = new JTextArea(10,200);
private JTextArea areaAnswer = new JTextArea(10,200);
private JTextArea areaInput = new JTextArea();
private JButton buttonComfire = new JButton("确定");
private JButton buttonConver = new JButton("取消");
private WriteJapanese wj = new WriteJapanese();
private NumUtil nu=new NumUtil();
private JapanPotity jp=new JapanPotity();
// private
public Japanese() {
this.setLayout(new GridBagLayout());
// JPanel panelNoth = new JPanel();
// panelNoth.add(buttonQuestion);
// panelNoth.add(buttonChange);
// panelNoth.add(buttonAnswer);
// this.add(panelNoth, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
// GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(
// 0, 5, 0, 0), 0, 0));
/**
* 这里最好不要用Japanel面板,因为用到的布局是不等网格布局,用面板的话,下面的组间网格布局就困难了
*/
this.add(buttonQuestion, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,
5, 0, 0), 90, 0));
this.add(buttonChange, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,
5, 0, 0), 0, 0));
this.add(buttonAnswer, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,
5, 0, 0), 90, 0));
// JScrollPane p1 = new JScrollPane();
// JScrollPane p2 = new JScrollPane();
// JScrollPane p3 = new JScrollPane();
// areaQuestion.setSize(780,1500);
// areaReturn.setSize(10,20);
// areaAnswer.setSize(10,20);
// p1.add(areaQuestion);
// p2.add(areaReturn);
// p3.add(areaAnswer);
// JPanel panelCenter = new JPanel();
// panelCenter.add(p1);
// panelCenter.add(p2);
// panelCenter.add(p3);
// this.add(areaQuestion, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
// GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3,
// 5, 0, 0), 40, 150));
// this.add(areaReturn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
// GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3,
// 5, 0, 0), 40, 150));
// this.add(areaAnswer, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
// GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3,
// 5, 0, 0), 40, 150));
areaQuestion.setEditable(false);
areaReturn.setEditable(false);
areaAnswer.setEditable(false);
this.add(new JScrollPane(areaQuestion), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3,
5, 0, 0), 40, 150));
this.add(new JScrollPane(areaReturn), new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3,
5, 0, 0), 40, 150));
this.add(new JScrollPane(areaAnswer), new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(3,
5, 0, 0), 40, 150));
this.add(new JScrollPane(areaInput), new GridBagConstraints(0, 2,
GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 2.0,
0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH,
new Insets(3, 5, 5, 5), 10, 50));
this.add(buttonComfire, new GridBagConstraints(2, 2,
GridBagConstraints.REMAINDER, GridBagConstraints.RELATIVE, 0.0,
0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(3, 5, 5, 5), 0, 0));
this.add(buttonConver, new GridBagConstraints(2, 3,
GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER,
0.0, 0.0, GridBagConstraints.SOUTHWEST,
GridBagConstraints.HORIZONTAL, new Insets(3, 5, 5, 5), 0, 0));
buttonQuestion.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
showQuestion1();
}
});
buttonChange.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
areaReturn.setText("");
}
});
buttonAnswer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
showQuestion2();
}
});
Action send=new AbstractAction(){
public void actionPerformed(ActionEvent e) {
sendmeg();
}
};
/**
* 1,getInputMap是获得组间焦点后的Map,keyStore是设置键盘相应的操作键
* 2,getActionMap是获得事件处理的action操作,value等于send,说明send是Action
* 3,其实上面两部是将Action和键盘绑定在一起。
* 4,组间获得Action,sendAction(Action)
*/
areaInput.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "send");
areaInput.getActionMap().put("send", send);
buttonComfire.setAction(send);
buttonComfire.setText("確定");
buttonConver.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
areaInput.setText("");
}
});
this.setTitle("日语片名学习器 V1.0");
// this.setSize(450, 305);
this.setSize(650, 320);
// this.pack();
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
int[] num;
private void showQuestion1(){
num=nu.createNum(jp.returnJapan());
Object[] obj = wj.show1(num);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < obj.length; i++) {
/**
* 这里用此算法还是比较好的分行
*/
sb.append(obj[i].toString()+" ");
if (i > 3 && (i+1) % 5 == 0) {
sb.append("\n");
}
}
areaQuestion.setText(sb.toString());
}
private void showQuestion2(){
if(num==null){
areaAnswer.setText("请先点击出题");
}else{
Object[] obj = wj.show2(num);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < obj.length; i++) {
sb.append(obj[i].toString()+" ");
if (i > 3 && (i+1) % 5 == 0) {
sb.append("\n");
}
}
areaAnswer.setText(sb.toString());
}
}
protected void sendmeg() {
areaReturn.insert(areaInput.getText()+"\n", areaReturn.getDocument().getLength());
areaInput.setText("");
}
public static void main(String[] args) {
new Japanese();
}
}
*********************这是基本类
package ch01.setion3.other;
public class JapanPotity {
private Object[] objJapan={
"か","き","く","け","こ",
"さ","し","す","せ","そ",
"た","ち","つ","て","と",
"は","ひ","ふ","へ","ほ",
"が","ぎ","ぐ","げ","ご",
"ざ","じ","ず","ぜ","ぞ",
"だ","ぢ","づ","で","ど",
"ば","び","ぶ","べ","ぼ",
"ぱ","ぴ","ぷ","ぺ","ぽ",
"きゃ","しゃ","ちゃ","にゃ","ひゃ","みゃ","りゃ",
"きゅ","しゅ","ぢゅ","にゅ","ひゅ","みゅ","りゅ",
"きょ","しょ","ちょ","にょ","ひょ","みょ","りょ",
"ぎゃ","ぎゆ","ぎょ",
"じゃ","じゅ","じょ",
"ぢや","ぢゆ","ぢよ",
"びゃ","びゅ", "びょ",
"ぴゃ","ぴゅ","ぴょ"
};
private Object[] objSpeak={
"ka","ki","ku","ke","ko",
"sa","shi","su","se","so",
"ta","chi","tsu","te","to",
"ha","hi","hu","he","ho",
"ga","gi","gu","ge","go",
"za","chi","tsu","ze","zo",
"da","di","du","de","do",
"ba","bi","bu","be","bo",
"pa","pi","pu","pe","po",
"kya","sha","cha","nya","hya","mya","rya",
"kyu","shu","chu","nyu","hyu","myu","ryu",
"kyo","sho","cho","nyo","hyo","myo","ryo",
"gya","gyu","gyo",
"ja","ju","jo",
"ja","ju","jo",
"bya","byu","byo",
"pya","pyu","pyo"
};
public Object[] returnJapan(){
return objJapan;
}
public Object[] returnSpeak(){
return objSpeak;
}
}
******************这是工具类
package ch01.setion3.other;
public class NumUtil {
public int[] createNum(Object[] obj){
int[]num=new int[25];
for(int i=0;i<num.length;i++){
num[i]=(int)(Math.random()*obj.length);
}
return num;
}
}
***************************这是业务逻辑类
package ch01.setion3.other;
public class WriteJapanese {
private JapanPotity jp=new JapanPotity();
private NumUtil nu=new NumUtil();
public Object[] show1(int[] num){
Object[]obj1=jp.returnJapan();
Object[]obj2=new Object[25];
for(int i=0;i<obj2.length;i++){
obj2[i]=obj1[num[i]];
}
return obj2;
}
public Object[] show2(int[] num){
Object[]obj1=jp.returnSpeak();
Object[]obj2=new Object[25];
for(int i=0;i<obj2.length;i++){
obj2[i]=obj1[num[i]];
}
return obj2;
}
}