软键盘(包含大小写转换)

收藏别人的,试了可以用。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ruanjianpan extends Frame implements ActionListener{
Frame f;
TextField bx;
Panel p1,p2,p3,p4;
int a=1;
int c;
String m=" ";
String r=" ";
String s=" ";
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,
b24,b25,b26,b27,b28,b29,b30,b31,b32,b33,b34,b35,b36,b37,b38,b39,b40,b41,b42,b43,b44,b45,b46,b47,b48,b49;
// 定义变量
public static void main(String args[]){
ruanjianpan s=new ruanjianpan();
s.go();
}
public void go(){
f=new Frame("软件盘");
bx=new TextField(80);
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
b0=new Button("7 &");
b1=new Button("8 *");
b2=new Button("9 (");
b3=new Button("0 )");
b4=new Button("` ~");
b5=new Button("1 !");
b6=new Button("2 @");
b7=new Button("3 #");
b8=new Button("4 $");
b9=new Button("5 %");
b10=new Button("6 ^");
b11=new Button("←");
b12=new Button("Shift");
b13=new Button("[ {");
b14=new Button("] }");
b15=new Button("; :");
b16=new Button("' “");
b17=new Button(", <");
b18=new Button(". >");
b19=new Button("/ ?");
b20=new Button("- _");
b21=new Button("= +");
b22=new Button("、 |");
b23=new Button("CapsLock");
b24=new Button("k");
b25=new Button("l");
b26=new Button("m");
b27=new Button("a");
b28=new Button("b");
b29=new Button("c");
b30=new Button("d");
b31=new Button("e");
b32=new Button("f");
b33=new Button("g");
b34=new Button("h");
b35=new Button("i");
b36=new Button("j");
b37=new Button("p");
b38=new Button("q");
b39=new Button("r");
b40=new Button("s");
b41=new Button("t");
b42=new Button("u");
b43=new Button("v");
b44=new Button("w");
b45=new Button("x");
b46=new Button("y");
b47=new Button("z");
b48=new Button("n");
b49=new Button("o");
//实例化
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
b20.addActionListener(this);
b21.addActionListener(this);
b22.addActionListener(this);
b23.addActionListener(this);
b24.addActionListener(this);
b25.addActionListener(this);
b26.addActionListener(this);
b27.addActionListener(this);
b28.addActionListener(this);
b29.addActionListener(this);
b30.addActionListener(this);
b31.addActionListener(this);
b32.addActionListener(this);
b33.addActionListener(this);
b34.addActionListener(this);
b35.addActionListener(this);
b36.addActionListener(this);
b37.addActionListener(this);
b38.addActionListener(this);
b39.addActionListener(this);
b40.addActionListener(this);
b41.addActionListener(this);
b42.addActionListener(this);
b43.addActionListener(this);
b44.addActionListener(this);
b45.addActionListener(this);
b46.addActionListener(this);
b47.addActionListener(this);
b48.addActionListener(this);
b49.addActionListener(this);
//添加监听
f.setLayout(new GridLayout(5,1));
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
f.add(bx);p1.add(b0);p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p1.add(b5);p1.add(b6);p1.add(b7);p1.add(b8);p1.add(b9);p1.add(b10);p1.add(b11);
p2.add(b12);p2.add(b13);p2.add(b14);p2.add(b15);p2.add(b16);p2.add(b17);p2.add(b18);p2.add(b19);p2.add(b20);p2.add(b21);p2.add(b22);p2.add(b23);
p3.add(b24);p3.add(b25);p3.add(b26);p3.add(b27);p3.add(b28);p3.add(b29);p3.add(b30);p3.add(b31);p3.add(b32);p3.add(b33);p3.add(b34);p3.add(b35);p3.add(b36);
p4.add(b37);p4.add(b38);p4.add(b39);p4.add(b40);p4.add(b41);p4.add(b42);p4.add(b43);p4.add(b44);p4.add(b45);p4.add(b46);p4.add(b47);p4.add(b48);p4.add(b49);
f.add(p1);f.add(p2);f.add(p3);f.add(p4);
//定义布局
bx.setEnabled(false);
f.setSize(500,180);
f.setVisible(true);
//定义框口
f.addWindowListener(new WindowAdapter() //为了关闭窗口
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

}

public void actionPerformed(ActionEvent e){

if(e.getActionCommand().equals("7 &")){
m=m+"7";
bx.setText(m);
}
if(e.getActionCommand().equals("8 *")){
m=m+"8";
bx.setText(m);
}
if(e.getActionCommand().equals("9 (")){
m=m+"9";
bx.setText(m);
}
if(e.getActionCommand().equals("0 )")){
m=m+"0";
bx.setText(m);
}
if(e.getActionCommand().equals("` ~")){
m=m+"`";
bx.setText(m);
}
if(e.getActionCommand().equals("1 !")){
m=m+"1";
bx.setText(m);
}
if(e.getActionCommand().equals("2 @")){
m=m+"2";
bx.setText(m);
}
if(e.getActionCommand().equals("3 #")){
m=m+"3";
bx.setText(m);
}
if(e.getActionCommand().equals("4 $")){
m=m+"4";
bx.setText(m);
}
if(e.getActionCommand().equals("5 %")){
m=m+"5";
bx.setText(m);
}
if(e.getActionCommand().equals("6 ^")){
m=m+"6";
bx.setText(m);
}
if(e.getActionCommand().equals("[ {")){
m=m+"[";
bx.setText(m);
}
if(e.getActionCommand().equals("] }")){
m=m+"]";
bx.setText(m);
}
if(e.getActionCommand().equals("; :")){
m=m+";";
bx.setText(m);
}
if(e.getActionCommand().equals("' “")){
m=m+"'";
bx.setText(m);
}
if(e.getActionCommand().equals(", <")){
m=m+",";
bx.setText(m);
}
if(e.getActionCommand().equals(". >")){
m=m+".";
bx.setText(m);
}
if(e.getActionCommand().equals("/ ?")){
m=m+"/";
bx.setText(m);
}
if(e.getActionCommand().equals("- _")){
m=m+"-";
bx.setText(m);
}
if(e.getActionCommand().equals("= +")){
m=m+"=";
bx.setText(m);
}
if(e.getActionCommand().equals("、 |")){
m=m+"、";
bx.setText(m);
}

if(a==1){
if(e.getActionCommand().equals("k")){
m=m+"k";
bx.setText(m);
}
if(e.getActionCommand().equals("l")){
m=m+"l";
bx.setText(m);
}
if(e.getActionCommand().equals("m")){
m=m+"m";
bx.setText(m);
}
if(e.getActionCommand().equals("a")){
m=m+"a";
bx.setText(m);
}
if(e.getActionCommand().equals("b")){
m=m+"b";
bx.setText(m);
}
if(e.getActionCommand().equals("c")){
m=m+"c";
bx.setText(m);
}
if(e.getActionCommand().equals("d")){
m=m+"d";
bx.setText(m);
}
if(e.getActionCommand().equals("e")){
m=m+"e";
bx.setText(m);
}
if(e.getActionCommand().equals("f")){
m=m+"f";
bx.setText(m);
}
if(e.getActionCommand().equals("g")){
m=m+"g";
bx.setText(m);
}
if(e.getActionCommand().equals("h")){
m=m+"h";
bx.setText(m);
}
if(e.getActionCommand().equals("i")){
m=m+"i";
bx.setText(m);
}
if(e.getActionCommand().equals("j")){
m=m+"j";
bx.setText(m);
}
if(e.getActionCommand().equals("p")){
m=m+"p";
bx.setText(m);
}
if(e.getActionCommand().equals("q")){
m=m+"q";
bx.setText(m);
}
if(e.getActionCommand().equals("r")){
m=m+"r";
bx.setText(m);
}
if(e.getActionCommand().equals("s")){
m=m+"s";
bx.setText(m);
}
if(e.getActionCommand().equals("t")){
m=m+"t";
bx.setText(m);
}
if(e.getActionCommand().equals("u")){
m=m+"u";
bx.setText(m);
}
if(e.getActionCommand().equals("v")){
m=m+"v";
bx.setText(m);
}
if(e.getActionCommand().equals("w")){
m=m+"w";
bx.setText(m);
}
if(e.getActionCommand().equals("x")){
m=m+"x";
bx.setText(m);
}
if(e.getActionCommand().equals("y")){
m=m+"y";
bx.setText(m);
}
if(e.getActionCommand().equals("z")){
m=m+"z";
bx.setText(m);
}
if(e.getActionCommand().equals("n")){
m=m+"n";
bx.setText(m);
}
if(e.getActionCommand().equals("o")){
m=m+"o";
bx.setText(m);
}
}


if(a==2){
if(e.getActionCommand().equals("k")){
m=m+"K";
bx.setText(m);
}
if(e.getActionCommand().equals("l")){
m=m+"L";
bx.setText(m);
}
if(e.getActionCommand().equals("m")){
m=m+"M";
bx.setText(m);
}
if(e.getActionCommand().equals("a")){
m=m+"A";
bx.setText(m);
}
if(e.getActionCommand().equals("b")){
m=m+"B";
bx.setText(m);
}
if(e.getActionCommand().equals("c")){
m=m+"C";
bx.setText(m);
}
if(e.getActionCommand().equals("d")){
m=m+"D";
bx.setText(m);
}
if(e.getActionCommand().equals("e")){
m=m+"E";
bx.setText(m);
}
if(e.getActionCommand().equals("f")){
m=m+"F";
bx.setText(m);
}
if(e.getActionCommand().equals("g")){
m=m+"G";
bx.setText(m);
}
if(e.getActionCommand().equals("h")){
m=m+"H";
bx.setText(m);
}
if(e.getActionCommand().equals("i")){
m=m+"I";
bx.setText(m);
}
if(e.getActionCommand().equals("j")){
m=m+"J";
bx.setText(m);
}
if(e.getActionCommand().equals("p")){
m=m+"P";
bx.setText(m);
}
if(e.getActionCommand().equals("q")){
m=m+"Q";
bx.setText(m);
}
if(e.getActionCommand().equals("r")){
m=m+"R";
bx.setText(m);
}
if(e.getActionCommand().equals("s")){
m=m+"S";
bx.setText(m);
}
if(e.getActionCommand().equals("t")){
m=m+"T";
bx.setText(m);
}
if(e.getActionCommand().equals("u")){
m=m+"U";
bx.setText(m);
}
if(e.getActionCommand().equals("v")){
m=m+"V";
bx.setText(m);
}
if(e.getActionCommand().equals("w")){
m=m+"W";
bx.setText(m);
}
if(e.getActionCommand().equals("x")){
m=m+"X";
bx.setText(m);
}
if(e.getActionCommand().equals("y")){
m=m+"Y";
bx.setText(m);
}
if(e.getActionCommand().equals("z")){
m=m+"Z";
bx.setText(m);
}
if(e.getActionCommand().equals("n")){
m=m+"N";
bx.setText(m);
}
if(e.getActionCommand().equals("o")){
m=m+"O";
bx.setText(m);
}
}


//事件触发


if(e.getActionCommand().equals("CapsLock")){
switch(a){
case 1:a=2;break;
case 2:a=1;break;
}
}
//定义"CapsLock"

if(e.getActionCommand().equals("←")){
r=bx.getText();
c=r.length();
s=r.substring(0,c-1);
m=s;
bx.setText(s);
}
//定义"←"键
}
}

你可能感兴趣的:(Swing)