import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
@
SuppressWarnings("serial")
public class MainFrame extends JFrame {
private JTextField textField;
private PopUpKeyboard keyboard;
public MainFrame() {
super("pop-up keyboard");
setDefaultCloseOperation(EXIT_ON_CLOSE);
textField = new JTextField(20);
keyboard = new PopUpKeyboard(textField);
textField.addMouseListener(new MouseAdapter() {@
Override
public void mouseClicked(MouseEvent e) {
Point p = textField.getLocationOnScreen();
p.y += 30;
keyboard.setLocation(p);
keyboard.setVisible(true);
}
});
setLayout(new FlowLayout());
add(textField);
pack();
setLocationByPlatform(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {@
Override
public void run() {
new MainFrame().setVisible(true);
}
});
}
private class PopUpKeyboard extends JDialog implements ActionListener {
private JTextField textField;
public PopUpKeyboard(JTextField textField) {
this.textField=textField;
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1000, 250));
this.setLocation(50, 50);
keyboardVariables();
}
private void keyboardVariables() {
String firstRow[] = {
"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "Back Space"
};
String secondRow[] = {
"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "|"
};
String thirdRow[] = {
"A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"
};
String fourthRow[] = {
"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?"
};
String fifthRow[] = {
"Space"
};
JButton first[];
JButton second[];
JButton third[];
JButton fourth[];
JButton fifth[];
setLayout(new BorderLayout());
Font font;
float size;
JPanel jpNorth = new JPanel();
JPanel jpCenter = new JPanel();
JPanel jpKeyboard = new JPanel();
JPanel jpNote = new JPanel();
add(jpNorth, BorderLayout.NORTH);
add(jpCenter, BorderLayout.CENTER);
add(jpKeyboard, BorderLayout.SOUTH);
jpNorth.setLayout(new BorderLayout());
jpCenter.setLayout(new BorderLayout());
jpKeyboard.setLayout(new GridLayout(5, 1));
pack();
first = new JButton[firstRow.length];
JPanel p = new JPanel(new GridLayout(1, firstRow.length));
for (int i = 0; i < firstRow.length; i++) {
JButton b = new JButton(firstRow[i]);
b.setPreferredSize(new Dimension(100, 50));
b.addActionListener(this);
first[i] = b;
p.add(first[i]);
}
jpKeyboard.add(p);
second = new JButton[secondRow.length];
p = new JPanel(new GridLayout(1, secondRow.length));
for (int i = 0; i < secondRow.length; ++i) {
second[i] = new JButton(secondRow[i]);
second[i].addActionListener(this);
p.add(second[i]);
}
jpKeyboard.add(p);
third = new JButton[thirdRow.length];
p = new JPanel(new GridLayout(1, thirdRow.length));
JToggleButton toggleButton = new JToggleButton("CapsLock");
toggleButton.addActionListener(this);
p.add(toggleButton);
for (int i = 0; i < thirdRow.length; ++i) {
third[i] = new JButton(thirdRow[i]);
third[i].addActionListener(this);
p.add(third[i]);
}
jpKeyboard.add(p);
fourth = new JButton[fourthRow.length];
p = new JPanel(new GridLayout(1, fourthRow.length));
for (int i = 0; i < fourthRow.length; ++i) {
fourth[i] = new JButton(fourthRow[i]);
fourth[i].addActionListener(this);
p.add(fourth[i]);
}
p.add(new JPanel());
jpKeyboard.add(p);
fifth = new JButton[fifthRow.length];
p = new JPanel(new GridLayout(1, fifthRow.length));
for (int i = 0; i < fifthRow.length; ++i) {
JButton b = new JButton(fifthRow[i]);
fifth[i] = b;
fifth[i].addActionListener(this);
b.setPreferredSize(new Dimension(20, 10));
p.add(fifth[i]);
}
jpKeyboard.add(p);
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
boolean capslock = false;
boolean shift = true;
if(action == "Back Space"){
textField.setText(textField.getText().substring(0, textField.getText().length()-1));
}else if (action == "Tab"){
textField.setText(textField.getText() + "\t");
}else if (action == "Space"){
textField.setText(textField.getText() + " ");
}else if (action == "Enter"){
textField.setText(textField.getText() + "\n");
}else if(action == "Shift"){
shift = false; //here
}else if(action == "CapsLock"){
textField.setText(textField.getText().toUpperCase()); //here
}
else{
textField.setText(textField.getText() + action.toLowerCase());
}
}
}
}