Buscar este blog

jueves, 23 de mayo de 2013

TECLADO JAVA ENTORNO GRAFICO

package editor;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Editor extends JFrame implements MouseListener{
    String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","<<<<<"};
    String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"};
    String thirdRow[] = {"Caps","A","S","D","F","G","H","J","K","L",":","\"","Enter"};
    String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?","   ^" };
    String fifthRow[]={"      " ,"<" ,"\\/",">" };
    String noShift="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
    String specialChars ="~-+[]\\;',.?";
   
    JButton first[];
    JButton second[];
    JButton third[];
    JButton fourth[];
    JButton fifth[];
    Color cc = new JButton().getBackground();
    JTextArea  text;
    Boolean mayusculas = false;

   
    public static void main(String[] args) {
        new Editor().setVisible(true);
    }

    public Editor()
    {
        this.setResizable(false);
         this.getContentPane().setPreferredSize(new Dimension(1000,600));
        this.setLocation(50,50);
        iniciar();
    }

    private void iniciar()
    {

        text = new JTextArea();
        text.setPreferredSize(new Dimension(800,200));
        JLabel info = new JLabel("");
        setLayout(new BorderLayout());
        JPanel jpNorth = new JPanel();
        JPanel jpCenter = new JPanel();
        JPanel jpKeyboard = new JPanel();
        JPanel jpNote = new JPanel();
        add( jpNorth, BorderLayout.NORTH);
        add( jpNote);
        add( jpCenter, BorderLayout.CENTER);
        add(jpKeyboard, BorderLayout.SOUTH);

        jpNorth.setLayout(new BorderLayout());
        jpNorth.add(info, BorderLayout.WEST);
        jpNorth.add(info, BorderLayout.SOUTH);

        jpCenter.setLayout( new BorderLayout());
        jpCenter.add(text, BorderLayout.WEST);
        jpCenter.add(text, BorderLayout.CENTER);

        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));
            first[i] = b;
            first[i].addMouseListener(this);
            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].addMouseListener(this);
            p.add(second[i]);

        }
        jpKeyboard.add(p);
        third = new JButton[thirdRow.length];
        p = new JPanel(new GridLayout(1, thirdRow.length));
        for(int i = 0; i < thirdRow.length; ++i)
        {
            third[i] = new JButton(thirdRow[i]);
            third[i].addMouseListener(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].addMouseListener(this);
            p.add(fourth[i]);
            if(i==fourthRow.length-2)
                p.add(new JPanel());

        }
        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 < 1; ++i)
        {
            JPanel  spacePanel = new JPanel();
            p.add(spacePanel);
        }
        for(int i = 0; i < fifthRow.length; ++i)
        {
            if(i==1)
            {
                JButton b = new JButton(fifthRow[i]);
                b.setPreferredSize(new Dimension(400,10));
                b.setBounds(10, 10, 600, 100);
                fifth[i]=b;
                fifth[i].addMouseListener(this);
                p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());p.add(new JPanel());
            }
            else
            {
                fifth[i]=new JButton(fifthRow[i]);
                fifth[i].addMouseListener(this);
            }
            if(i==0)
            {
                   JPanel  spacePanel = new JPanel();
                   p.add(spacePanel);
            }
            p.add(fifth[i]);
        }
        jpKeyboard.add(p);
        }

    @Override
    public void mouseClicked(MouseEvent e) {
            JButton btn = (JButton) e.getSource();
            switch(btn.getText()) {
            case "Enter":
            case "\\/":
                text.setText(""+text.getText()+btn.getText()+"\n");
                break;
               
            case "<<<<<":
               String sCadena = text.getText();
               text.setText(sCadena.substring(0,text.getDocument().getLength()-1));
               break;
            case ">":          
                 text.grabFocus();
                break;
            case "<":
                    text.grabFocus();
                    int i = text.getDocument().getLength();
                    text.setCaretPosition(i - 1);
                    break;
              
            case "Caps":
            case "Shift":
                if (mayusculas){
                    mayusculas = false;
                    btn.setBackground(cc);               
                } else{
                mayusculas = true;
                btn.setBackground(Color.DARK_GRAY);
                }
                break;

               
            default:
                if (!mayusculas) {
                    String texto = (btn.getText()).toLowerCase();
                    text.setText(""+text.getText()+texto);   
                } else {
                    text.setText(""+text.getText()+btn.getText());
                }
            }
           
        }


    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Apéndice de método generado automáticamente
       
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Apéndice de método generado automáticamente
       
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Apéndice de método generado automáticamente
       
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Apéndice de método generado automáticamente
       
    }

    }

No hay comentarios:

Publicar un comentario