无法将keyListener添加到按钮

时间:2018-07-30 15:27:09

标签: java swing awt

我正在尝试创建具有一些基本功能的键盘GUI。我遇到的问题在我的draw(...){...}函数中,该函数突出显示了addKeyListener()方法,并显示错误: The method addKeyListener(KeyListener) in the type Component is not applicable for the arguments (a3_keyboard)

我不确定我在做什么错。我尝试过修改代码,

getContentPane().addKeyListener(this);
for (JButton button : first)  { button.addKeyListener(this); }
for (JButton button : second) { button.addKeyListener(this); } 
for (JButton button : third)  { button.addKeyListener(this); }
for (JButton button : fourth) { button.addKeyListener(this); }
for (JButton button : fifth)  { button.addKeyListener(this); }

但是那使我犯了同样的错误。完整代码如下:

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class a3_keyboard extends JFrame implements KeyListener {
    //input
    String input;
    //default color
    Color defaultColor = new JButton().getBackground();
    //main rows of keys
    private String rowOne[] = {
        "~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "h"
    };
    private String rowTwo[] = {
        "Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"
    };
    private String rowThree[] = {
        "Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "'", "Enter"
    };
    private String rowFour[] = {
        "Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "   ^"
    };
    private String rowFive[] = {
        "       ", "<", "v", ">"
    };
    /** Account for chars with no shift:
        ** Program toggles Shift key, meaning
        ** if a user clicks on it, all keys will be
        ** toggled to their respective shift value. The
        ** user can tap the shift key again to 
        ** change back to regular value */
    private String shiftless[] = {
        "1","2","3","4","5","6","7","8","9","0",
        "-","=","q","w","e","r","t","y","u","i","o","p",
        "[","]","\\","a","s","d","f","g","h","j","k","l",
        ";","z","x","c","v","b","n","m",",",".","/"
    };
    //Account for special chars
    private String specialChars[] = {
        "~", "-", "+", "[", "]", "\\", ";", ".", "?"
    };
    private JLabel context = new JLabel("Type some text using your keyboard. "
        + "The keys you press will be highlighed and the text will be displayed.\n "
        + "Note: Clicking the buttons with your mouse will not perform any action.")
        .setFont(new Font("Verdana",Font.BOLD,14));
    //declare rows of buttons
    private JButton buttons_rowOne[], buttons_rowTwo[], buttons_rowThree[], buttons_rowFour[], buttons_rowFive[];
    //ctor
    public a3_keyboard() {
        super("Typing Tutor");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.getContentPane().setPreferredSize(new Dimension(1000,600));
        this.setLocation(50,50);
        this.setVisible(true);
        __init__();
    }
    public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox) {
        setLayout(new BorderLayout());
        add(top, BorderLayout.NORTH);
        add(contextBox);
        add(middle, BorderLayout.CENTER);
        add(bottom, BorderLayout.SOUTH);
    }
    private void __init__body() {
        JTextArea body = new JTextArea().setPreferredSize(new Dimension(600, 150));
    }
    public void __init__panels() {
        JPanel top = new JPanel();
        JPanel middle = new JPanel();
        JPanel bottom = new JPanel();
        JPanel contextBox = new JPanel();
        __init__layout(top, middle, bottom, contextBox);
        top.setLayout(new BorderLayout());
        bottom.setLayout(new GridLayout(5,1));
        top.add(info, BorderLayout.WEST);
        top.add(info, BorderLayout.SOUTH);
        middle.setLayout( new BorderLayout());
        middle.add(text, BorderLayout.WEST);
        middle.add(text, BorderLayout.CENTER);
    }
    private void __init__() {
        //text area
            __init__body();
        //panels for layout
            __init__panels();
        pack();
        //get length of row strings
            int length_rowOne = rowOne.length;
            int length_rowTwo = rowTwo.length;
            int length_rowThree = rowThree.length;
            int length_rowFour = rowFour.length;
            int length_rowFive = rowFive.length;
        //create array for each row of buttons
            buttons_rowOne = new JButton[length_rowOne];
            buttons_rowTwo = new JButton[length_rowTwo];
            buttons_rowThree = new JButton[length_rowThree];
            buttons_rowFour = new JButton[length_rowFour];
            buttons_rowFive = new JButton[length_rowFive];
        //create panel for each row of buttons
            JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
            JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
            JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
            JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
            JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
        //draw out the rows of buttons
            draw(
                r1, length_rowOne,
                r2, length_rowTwo,
                r3, length_rowThree,
                r4, length_rowFour,
                r5, length_rowFive
            );
    }
    //draw rows of buttons
    public void draw(JPanel r1, int s1, 
                    JPanel r2, int s2, 
                    JPanel r3, int s3, 
                    JPanel r4, int s4, 
                    JPanel r5, int s5) {
        for (int i = 0; i < s1; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowOne[i] = currentButton;
            buttons_rowOne[i].addKeyListener(this);
            r1.add(buttons_rowOne[i]);
        }
        for (int i = 0; i < s2; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowTwo[i] = currentButton;
            buttons_rowTwo[i].addKeyListener(this);
            r1.add(buttons_rowTwo[i]);
        }
        for (int i = 0; i < s3; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowThree[i] = currentButton;
            buttons_rowThree[i].addKeyListener(this);
            r1.add(buttons_rowThree[i]);
        }
        for (int i = 0; i < s4; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowFour[i] = currentButton;
            buttons_rowFour[i].addKeyListener(this);
            r1.add(buttons_rowFour[i]);
        }
        for (int i = 0; i < s5; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            //account for space bar
            if (i == 1) {
                currentButton = new JButton(rowFive[i]);
                currentButton.setPreferredSize(new Dimension(400,10));
                currentButton.setBounds(10, 10, 600, 100);
                buttons_rowFive[i] = currentButton;
            } else {
                currentButton.setPreferredSize(new Dimension(100, 50));
                buttons_rowFive[i] = currentButton;
                buttons_rowFive[i].addKeyListener(this);
            }
            r1.add(buttons_rowFive[i]);
        }
        bottom.add(r1);
        bottom.add(r2);
        bottom.add(r3);
        bottom.add(r4);
        bottom.add(r5);
    }//!draw(...)
    //called when a button is pressed
    @Override
    public void activated(KeyEvent press) {
        int index = press.getKeyCode();
        input = String.format("%s", index);
    }//!activated(...)
    //called when a button is released
    @Override
    public void deactivated(KeyEvent release) {
        int index = release.getKeyCode();
        input = String.format( "%s"+KeyEvent.getKeyText(keyCode) );
        this.setBackground(defaultColor);;
    }//!deactivated(...)
    //main method
    public static void main(Strings[] args) {
        new a3_keyboard();
    }//!main method
    //obtain the JButton’s original background colour before you change its colour
}//!main class

如果我缺少什么,请告诉我,我将其添加到问题中。


编辑:

新代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class a3_keyboard extends JFrame implements KeyListener {
    //input
    String input;
    //input body
    JTextArea body;
    //panels
    JPanel top, middle, bottom, contextBox = new JPanel();
    //default color
    Color defaultColor = new JButton().getBackground();
    //main rows of keys
    public String rowOne[] = {
        "~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "h"
    };
    public String rowTwo[] = {
        "Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"
    };
    public String rowThree[] = {
        "Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "'", "Enter"
    };
    public String rowFour[] = {
        "Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "   ^"
    };
    public String rowFive[] = {
        "       ", "<", "v", ">"
    };
    /** Account for chars with no shift:
        ** Program toggles Shift key, meaning
        ** if a user clicks on it, all keys will be
        ** toggled to their respective shift value. The
        ** user can tap the shift key again to 
        ** change back to regular value */
    public String shiftless[] = {
        "1","2","3","4","5","6","7","8","9","0",
        "-","=","q","w","e","r","t","y","u","i","o","p",
        "[","]","\\","a","s","d","f","g","h","j","k","l",
        ";","z","x","c","v","b","n","m",",",".","/"
    };
    //Account for special chars
    public String specialChars[] = {
        "~", "-", "+", "[", "]", "\\", ";", ".", "?"
    };

    //declare rows of buttons
    public JButton buttons_rowOne[], buttons_rowTwo[], buttons_rowThree[], buttons_rowFour[], buttons_rowFive[];
    //ctor
    public a3_keyboard() {
        super("Typing Tutor");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.getContentPane().setPreferredSize(new Dimension(1000,600));
        this.setLocation(50,50);
        this.setVisible(true);
        __init__();
    }
    public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox) {
        setLayout(new BorderLayout());
        add(top, BorderLayout.NORTH);
        add(contextBox);
        add(middle, BorderLayout.CENTER);
        add(bottom, BorderLayout.SOUTH);
    }
    public void __init__body() {
        JTextArea body = new JTextArea();
        body.setPreferredSize(new Dimension(600, 150));
    }
    public void __init__panels() {
        JLabel context = new JLabel("Type some text using your keyboard. "
            + "The keys you press will be highlighed and the text will be displayed.\n "
                + "Note: Clicking the buttons with your mouse will not perform any action.");
        context.setFont(new Font("Verdana",Font.BOLD,14));
        __init__layout(top, middle, bottom, contextBox);
        top.setLayout(new BorderLayout());
        bottom.setLayout(new GridLayout(5,1));
        top.add(context, BorderLayout.WEST);
        top.add(context, BorderLayout.SOUTH);
        middle.setLayout( new BorderLayout());
        middle.add(body, BorderLayout.WEST);
        middle.add(body, BorderLayout.CENTER);
    }
    public void __init__() {
        //text area
            __init__body();
        //panels for layout
            __init__panels();
        pack();
        //get length of row strings
            int length_rowOne = rowOne.length;
            int length_rowTwo = rowTwo.length;
            int length_rowThree = rowThree.length;
            int length_rowFour = rowFour.length;
            int length_rowFive = rowFive.length;
        //create array for each row of buttons
            buttons_rowOne = new JButton[length_rowOne];
            buttons_rowTwo = new JButton[length_rowTwo];
            buttons_rowThree = new JButton[length_rowThree];
            buttons_rowFour = new JButton[length_rowFour];
            buttons_rowFive = new JButton[length_rowFive];
        //create panel for each row of buttons
            JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
            JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
            JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
            JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
            JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
        //draw out the rows of buttons
            draw(
                r1, length_rowOne,
                r2, length_rowTwo,
                r3, length_rowThree,
                r4, length_rowFour,
                r5, length_rowFive
            );

    }
    //draw rows of buttons
    public void draw(JPanel r1, int s1, 
                    JPanel r2, int s2, 
                    JPanel r3, int s3, 
                    JPanel r4, int s4, 
                    JPanel r5, int s5) {
        for (int i = 0; i < s1; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowOne[i] = currentButton;
            buttons_rowOne[i].addKeyListener(this);
            r1.add(buttons_rowOne[i]);
        }
        for (int i = 0; i < s2; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowTwo[i] = currentButton;
            buttons_rowTwo[i].addKeyListener(this);
            r1.add(buttons_rowTwo[i]);
        }
        for (int i = 0; i < s3; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowThree[i] = currentButton;
            buttons_rowThree[i].addKeyListener(this);
            r1.add(buttons_rowThree[i]);
        }
        for (int i = 0; i < s4; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            currentButton.setPreferredSize(new Dimension(100, 50));
            buttons_rowFour[i] = currentButton;
            buttons_rowFour[i].addKeyListener(this);
            r1.add(buttons_rowFour[i]);
        }
        for (int i = 0; i < s5; i++) {
            JButton currentButton = new JButton(rowOne[i]);
            //account for space bar
            if (i == 1) {
                currentButton = new JButton(rowFive[i]);
                currentButton.setPreferredSize(new Dimension(400,10));
                currentButton.setBounds(10, 10, 600, 100);
                buttons_rowFive[i] = currentButton;
                buttons_rowFive[i].addKeyListener(this);
            } else {
                currentButton.setPreferredSize(new Dimension(100, 50));
                buttons_rowFive[i] = currentButton;
                buttons_rowFive[i].addKeyListener(this);
            }
            r1.add(buttons_rowFive[i]);
        }
        bottom.add(r1);
        bottom.add(r2);
        bottom.add(r3);
        bottom.add(r4);
        bottom.add(r5);
    }//!draw(...)

    // called when a button is pressed
    @Override
    public void keyPressed(KeyEvent pressed) {
        JButton current = (JButton) pressed.getSource();
        current.setBackground(Color.GRAY);
        body.append(toString(current.getKeyChar()));
    }//!keyPressed(...)
    // called when a button is released
    @Override
    public void keyReleased(KeyEvent released) {
        JButton current = (JButton) released.getSource();
        current.setBackground(defaultColor);
    }//!keyReleased(...)
    @Override
    public void keyTyped(KeyEvent typed) {
        JButton current = (JButton) typed.getSource();
    }

    //main method
    public static void main(String[] args) {
        new a3_keyboard();
    }//!main method
    private static final long serialVersionUID = 999;
}//!main class

编辑2:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class ButtonInPane extends JFrame implements KeyListener {
  //redacted, see below

  @Override
  public void keyPressed(KeyEvent press) {
      JButton current = (JButton) press.getSource();
      current.setBackground(Color.GRAY);
      StringBuilder sb = new StringBuilder();
      sb.append(press.getKeyChar());
      body.append(sb.toString());
  } // !keyPressed(...)
  // called when a button is released

  @Override
  public void keyReleased(KeyEvent release) {
      JButton current = (JButton) release.getSource();
      current.setBackground(defaultColor);
  } // !keyReleased(...)

  @Override
  public void keyTyped(KeyEvent typed) {
      JButton current = (JButton) typed.getSource();
  }

  // main method
  public static void main(String[] args) {
      new ButtonInPane();
  } // !main method

  private static final long serialVersionUID = 999;
} // !main class

编辑3:

@Override
public void keyPressed(KeyEvent press) {
    Object current = press.getSource().toString();
    for (int i = 0; i < 14; i++) {
    if (current == rowOne[i]) {
        buttons_rowOne[i].setBackground(Color.GRAY);
    } else if (current == rowTwo[i]) {
        buttons_rowTwo[i].setBackground(Color.GRAY);
    } else if (current == rowThree[i]) {
        buttons_rowThree[i].setBackground(Color.GRAY);
    } else if (current == rowFour[i]) {
        buttons_rowFour[i].setBackground(Color.GRAY);
    } else if (current == rowFive[i]) {
        buttons_rowFive[i].setBackground(Color.GRAY);
    }
    }
} // !keyPressed(...)
// called when a button is released

@Override
public void keyReleased(KeyEvent release) {
    Object current = release.getSource().toString();
    for (int i = 0; i < 14; i++) {
    if (current == rowOne[i]) {
        buttons_rowOne[i].setBackground(defaultColor);
    } else if (current == rowTwo[i]) {
        buttons_rowTwo[i].setBackground(defaultColor);
    } else if (current == rowThree[i]) {
        buttons_rowThree[i].setBackground(defaultColor);
    } else if (current == rowFour[i]) {
        buttons_rowFour[i].setBackground(defaultColor);
    } else if (current == rowFive[i]) {
        buttons_rowFive[i].setBackground(defaultColor);
    }
    }
} // !keyReleased(...)

@Override
public void keyTyped(KeyEvent typed) {
    // Object current = typed.getSource().toString();
    // StringBuilder sb = new StringBuilder();
    // for (int i = 0; i < 14; i++) {
    //   if (current == rowOne[i]) {
    //     sb.append(typed.getKeyCode());
    //     body.append(sb.toString());
    //   } else if (current == rowTwo[i]) {
    //     sb.append(typed.getKeyCode());
    //     body.append(sb.toString());
    //   } else if (current == rowThree[i]) {
    //     sb.append(typed.getKeyCode());
    //     body.append(sb.toString());
    //   } else if (current == rowFour[i]) {
    //     sb.append(typed.getKeyCode());
    //     body.append(sb.toString());
    //   } else if (current == rowFive[i]) {
    //     sb.append(typed.getKeyCode());
    //     body.append(sb.toString());
    //   }
    // }      
}

1 个答案:

答案 0 :(得分:1)

此版本将键侦听器放置在正确的组件上。但是,鉴于您的新了解,您现在需要重新设计此内容...

由于现在的代码是正确的,因此jtext区域主体在您按它们时只是获得了重复的键。有几种方法可以解决此问题。

有些蛮力,有些更复杂但优雅。如果您仍然需要帮助,请联系我。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

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

public class ButtonInPane extends JFrame implements KeyListener {
  // input
  String input;
  //context
  JLabel context1, context2;
  // default color
  Color defaultColor = new JButton().getBackground();
  // main rows of keys
  public String rowOne[] = {
      "~",
      "1",
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "0",
      "-",
      "+",
      "h"
  };
  public String rowTwo[] = {
      "Tab",
      "Q",
      "W",
      "E",
      "R",
      "T",
      "Y",
      "U",
      "I",
      "O",
      "P",
      "[",
      "]",
      "\\"
  };
  public String rowThree[] = {
      "Caps",
      "A",
      "S",
      "D",
      "F",
      "G",
      "H",
      "J",
      "K",
      "L",
      ":",
      "'",
      "Enter"
  };
  public String rowFour[] = {
      "Shift",
      "Z",
      "X",
      "C",
      "V",
      "B",
      "N",
      "M",
      ",",
      ".",
      "?",
      "   ^"
  };
  public String rowFive[] = {
      "       ",
      "<",
      "v",
      ">"
  };
  /**
   * Account for chars with no shift: Program toggles Shift key, meaning if a
   * user clicks on it, all keys will be toggled to their respective shift
   * value. The user can tap the shift key again to change back to regular
   * value
   */
  public String shiftless[] = {
      "1",
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "0",
      "-",
      "=",
      "q",
      "w",
      "e",
      "r",
      "t",
      "y",
      "u",
      "i",
      "o",
      "p",
      "[",
      "]",
      "\\",
      "a",
      "s",
      "d",
      "f",
      "g",
      "h",
      "j",
      "k",
      "l",
      ";",
      "z",
      "x",
      "c",
      "v",
      "b",
      "n",
      "m",
      ",",
      ".",
      "/"
  };
  // Account for special chars
  public String specialChars[] = {
      "~",
      "-",
      "+",
      "[",
      "]",
      "\\",
      ";",
      ".",
      "?"
  };

  // declare rows of buttons
  public JButton buttons_rowOne[], buttons_rowTwo[], buttons_rowThree[], buttons_rowFour[], buttons_rowFive[];
  private JTextArea body;
  private JPanel top;
  private JPanel middle;
  private JPanel bottom;
  private JPanel contextBox;

  // ctor
  public ButtonInPane() {
      super("Typing Tutor");
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setResizable(false);
      this.getContentPane().setPreferredSize(new Dimension(2000, 600));
      this.setLocation(50, 50);
      this.setVisible(true);
      __init__();
  }

  public void __init__layout(JPanel top, JPanel middle, JPanel bottom, JPanel contextBox) {
      setLayout(new BorderLayout());
      add(top, BorderLayout.NORTH);
      add(contextBox);
      add(middle, BorderLayout.CENTER);
      add(bottom, BorderLayout.SOUTH);
  }

  public void __init__body() {
      body = new JTextArea();
      body.setPreferredSize(new Dimension(600, 150));
      body.addKeyListener(this);
  }

  public void __init__panels() {
      context1 = new JLabel("Type some text using your keyboard. " +
          "The keys you press will be highlighed and the text will be displayed.");
      context2 = new JLabel("\nNote: Clicking the buttons with your mouse will not perform any action.");
      context1.setFont(new Font("Verdana", Font.BOLD, 14));
      context2.setFont(new Font("Verdana", Font.BOLD, 14));
      top = new JPanel();
      top.setSize(new Dimension(500, 500));
      middle = new JPanel();
      bottom = new JPanel();
      contextBox = new JPanel();
      __init__layout(top, middle, bottom, contextBox);
      top.setLayout(new BorderLayout());
      bottom.setLayout(new GridLayout(5, 1));
      top.add(context2);
      top.add(context1);
      middle.setLayout(new BorderLayout());
      middle.add(body, BorderLayout.WEST);
      middle.add(body, BorderLayout.CENTER);
  }

  public void __init__() {
      // text area
      __init__body();
      // panels for layout
      __init__panels();
      pack();
      // get length of row strings
      int length_rowOne = rowOne.length;
      int length_rowTwo = rowTwo.length;
      int length_rowThree = rowThree.length;
      int length_rowFour = rowFour.length;
      int length_rowFive = rowFive.length;
      // create array for each row of buttons
      buttons_rowOne = new JButton[length_rowOne];
      buttons_rowTwo = new JButton[length_rowTwo];
      buttons_rowThree = new JButton[length_rowThree];
      buttons_rowFour = new JButton[length_rowFour];
      buttons_rowFive = new JButton[length_rowFive];
      // create panel for each row of buttons
      JPanel r1 = new JPanel(new GridLayout(1, length_rowOne));
      JPanel r2 = new JPanel(new GridLayout(1, length_rowTwo));
      JPanel r3 = new JPanel(new GridLayout(1, length_rowThree));
      JPanel r4 = new JPanel(new GridLayout(1, length_rowFour));
      JPanel r5 = new JPanel(new GridLayout(1, length_rowFive));
      // draw out the rows of buttons
      draw(r1, length_rowOne, r2, length_rowTwo, r3, length_rowThree, r4, length_rowFour, r5, length_rowFive);

  }

  // draw rows of buttons
  public void draw(JPanel r1, int s1, JPanel r2, int s2, JPanel r3, int s3, JPanel r4, int s4, JPanel r5, int s5) {
      for (int i = 0; i < s1; i++) {
          JButton currentButton = new JButton(rowOne[i]);
          currentButton.setPreferredSize(new Dimension(100, 50));
          buttons_rowOne[i] = currentButton;
          r1.add(buttons_rowOne[i]);
      }
      for (int i = 0; i < s2; i++) {
          JButton currentButton = new JButton(rowTwo[i]);
          currentButton.setPreferredSize(new Dimension(100, 50));
          buttons_rowTwo[i] = currentButton;
          r2.add(buttons_rowTwo[i]);
      }
      for (int i = 0; i < s3; i++) {
          JButton currentButton = new JButton(rowThree[i]);
          currentButton.setPreferredSize(new Dimension(100, 50));
          buttons_rowThree[i] = currentButton;
          r3.add(buttons_rowThree[i]);
      }
      for (int i = 0; i < s4; i++) {
          JButton currentButton = new JButton(rowFour[i]);
          currentButton.setPreferredSize(new Dimension(100, 50));
          buttons_rowFour[i] = currentButton;
          r4.add(buttons_rowFour[i]);
      }
      for (int i = 0; i < s5; i++) {
          JButton currentButton = new JButton(rowFive[i]);
          // account for space bar
          if (i == 1) {
              currentButton = new JButton(rowFive[i]);
              currentButton.setPreferredSize(new Dimension(400, 10));
              currentButton.setBounds(10, 10, 600, 100);
              buttons_rowFive[i] = currentButton;
          } else {
              currentButton.setPreferredSize(new Dimension(100, 50));
              buttons_rowFive[i] = currentButton;
          }
          r5.add(buttons_rowFive[i]);
      }
      bottom.add(r1);
      bottom.add(r2);
      bottom.add(r3);
      bottom.add(r4);
      bottom.add(r5);
  } // !draw(...)
  // called when a button is pressed

  @Override
  public void keyPressed(KeyEvent press) {
      StringBuilder sb = new StringBuilder();
      sb.append(press.getKeyChar());
      body.append(sb.toString());
  } // !keyPressed(...)
  // called when a button is released

  @Override
  public void keyReleased(KeyEvent release) {
       Object current = release.getSource();
  } // !keyReleased(...)

  @Override
  public void keyTyped(KeyEvent typed) {
      Object current = typed.getSource();
  }

  // main method
  public static void main(String[] args) {
      new ButtonInPane();
  } // !main method

  private static final long serialVersionUID = 999;
} // !main class