**The output output would be like this:
import java.awt.event.*;
import javax.swing.*;
class VirtualKeyboard extends JFrame{
JTextArea textField;
String news = null;
String choice;
JToggleButton caps;
VirtualKeyboard(){
super("Keyboard");//
Container c =getContentPane();
c.setBackground(Color.lightGray);//sets the background color
setLayout( new FlowLayout() );
textField = new JTextArea(10,55);/////textfield
textField.setEditable(true);
textField.setWrapStyleWord(true);
textField.setLineWrap(true); ///end of textfield
JLabel wawas = new JLabel("Rock N Roll to the world");
add(wawas);
add( textField , BorderLayout.CENTER);
JButton one = new JButton("1");
add(one);
one.setForeground(Color.blue);
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("1");
}
});
JButton two = new JButton("2");
add(two);
two.setForeground(Color.blue);
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("2");
}
});
JButton three = new JButton("3");
add(three);
three.setForeground(Color.blue);
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("3");
}
});
JButton four = new JButton ("4");
add(four);
four.setForeground(Color.blue);
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("4");
}
});
JButton five = new JButton ("5");
add(five);
five.setForeground(Color.blue);
five.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("5");
}
});
JButton six = new JButton("6");
add(six);
six.setForeground(Color.blue);
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("6");
}
});
JButton seven = new JButton("7");
add(seven);
seven.setForeground(Color.blue);
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("7");
}
});
JButton eight = new JButton ("8");
add(eight);
eight.setForeground(Color.blue);
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("8");
}
});
JButton nine = new JButton("9");
add(nine);
nine.setForeground(Color.blue);
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("9");
}
});
JButton zero = new JButton("0");
add(zero);
zero.setForeground(Color.blue);
zero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("0");
}
});
JButton Backspace = new JButton( "Backspace" );
add(Backspace);
Backspace.setForeground(Color.red);
Backspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (textField.getText().length() > 0) { //f1 is a textfield
StringBuilder strB = new StringBuilder(textField.getText());
strB.deleteCharAt(textField.getText().length() -1);
news = strB.toString(); }
textField.setText(news);/////set the updated text
}
});
JButton keys = new JButton( " Tab " );
add(keys);
keys.setForeground(Color.red);
keys.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append(" ");
}
});
JButton Q = new JButton( "Q" );
add(Q);
Q.setForeground(Color.blue);
Q.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("q");
}
});
JButton W = new JButton( "W" );
add(W);
W.setForeground(Color.blue);
W.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("w");
}
});
JButton E = new JButton( "E" );
add(E);
E.setForeground(Color.blue);
E.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("e");
}
});
JButton R = new JButton( "R" );
add(R);
R.setForeground(Color.blue);
R.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("r");
}
});
JButton T = new JButton( "T" );
add(T);
T.setForeground(Color.blue);
T.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("t");
}
});
JButton Y = new JButton( "Y" );
add(Y);
Y.setForeground(Color.blue);
Y.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("y");
}
});
JButton U = new JButton( "U" );
add(U);
U.setForeground(Color.blue);
U.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("u");
}
});
JButton I = new JButton( "I" );
add(I);
I.setForeground(Color.blue);
I.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("i");
}
});
JButton O = new JButton( "O" );
add(O);
O.setForeground(Color.blue);
O.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("o");
}
});
JButton P = new JButton( "P" );
add(P);
P.setForeground(Color.blue);
P.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("p");
}
});
JButton enter = new JButton ( "Clear");
add(enter);
enter.setForeground(Color.red);
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("");
}
});
JToggleButton caps = new JToggleButton("Caps Lock");
add(caps);
caps.setForeground(Color.red);
caps.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//for(JToggleButton caps: Button){
// caps(!caps.isSelected());
// }
}
});
JButton A = new JButton( "A" );
add(A);
A.setForeground(Color.blue);
A.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("a");
}
});
JButton S = new JButton( "S" );
add(S);
S.setForeground(Color.blue);
S.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("s");
}
});
JButton D = new JButton( "D" );
add(D);
D.setForeground(Color.blue);
D.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("d");
}
});
JButton F = new JButton( "F" );
add(F);
F.setForeground(Color.blue);
F.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("f");
}
});
JButton G = new JButton( "G" );
add(G);
G.setForeground(Color.blue);
G.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("g");
}
});
JButton H = new JButton( "H" );
add(H);
H.setForeground(Color.blue);
H.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("h");
}
});
JButton J = new JButton( "J" );
add(J);
J.setForeground(Color.blue);
J.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("j");
}
});
JButton K = new JButton( "K" );
add(K);
K.setForeground(Color.blue);
K.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("k");
}
});
JButton L = new JButton( "L" );
add(L);
L.setForeground(Color.blue);
L.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("l");
}
});
JButton Enter = new JButton ( "Enter" );
add(Enter);
Enter.setForeground(Color.red);
Enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("\n");
}
});
JButton shift = new JButton(" Shift ");
add(shift);
shift.setForeground(Color.red);
shift.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
JButton Z = new JButton( "Z" );
add(Z);
Z.setForeground(Color.blue);
Z.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("z");
}
});
JButton X = new JButton( "X" );
add(X);
X.setForeground(Color.blue);
X.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("x");
}
});
JButton C = new JButton( "C" );
add(C);
C.setForeground(Color.blue);
C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("c");
}
});
JButton V = new JButton( "V" );
add(V);
V.setForeground(Color.blue);
V.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("v");
}
});
JButton B = new JButton( "B" );
add(B);
B.setForeground(Color.blue);
B.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("b");
}
});
JButton N = new JButton( "N" );
add(N);
N.setForeground(Color.blue);
N.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("n");
}
});
JButton M = new JButton( "M" );
add(M);
M.setForeground(Color.blue);
M.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append("m");
}
});
JButton shift1 = new JButton(" Shift ");
add(shift1);
shift1.setForeground(Color.red);
JButton Space = new JButton(" ");
add(Space);
Space.setForeground(Color.red);
Space.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.append(" ");
}
});
}
}//end of class VirtualKeyboard
No comments:
Post a Comment