Results 1 to 3 of 3
  1. #1
    quafflepunch is offline Member
    Join Date
    Jan 2011
    Location
    Canada
    Posts
    6
    Rep Power
    0

    Default Help, Setting Key Code

    Sorry if this is in the wrong area, but I am new to the forum.
    I am trying to have it so that when you click on a button, whatever you type next will appear on the button and be set as the keyCode for the corresponding place.
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    public class OptionsForm extends JFrame implements MouseListener, KeyListener  {
      ImageIcon optionsScreen = new ImageIcon("J:\\this course\\assets\\art\\backgrounds\\OptionsMenu.JPG");
      static KeyEvent[] up=new KeyEvent[2];
      KeyEvent[] right=new KeyEvent[2];
      
      KeyEvent[] left=new KeyEvent[2];
      KeyEvent[] attack=new KeyEvent[2];
      KeyEvent[] crouch=new KeyEvent[2];
      JButton oneUp=new JButton("Up");
      JButton twoUp=new JButton("8");
      JButton oneRight=new JButton("Right");
      JButton twoRight=new JButton("6");
      JButton oneLeft=new JButton("Left");
      JButton twoLeft=new JButton("4");
      JButton oneAttack=new JButton("Space");
      JButton twoAttack=new JButton("0");
      JButton oneCrouch=new JButton("Down");
      JButton twoCrouch=new JButton("5");
      JButton audioBrowse=new JButton("Browse");
      JButton charBrowse=new JButton("Browse");
      MouseEvent clicked;
      
      
      JFileChooser charInput=new JFileChooser();
      JFileChooser audioInput;  
      public OptionsForm() {
        up[0].setKeyCode(KeyEvent.VK_UP); //this is my error line
        this.setFocusable(true);
        setBounds(100,50,600,300);
        setLayout(null);     
        this.setTitle("Options");
        optionsImage = new JLabel(optionsScreen,0);
        //constructs Form with layout and title
        optionsImage.setBounds(0,0,600,280);
        oneUp.setBounds(360,93,107,34);
        twoUp.setBounds(467,93,107,34);
        oneRight.setBounds(360,127,107,34);
        twoRight.setBounds(467,127,107,34);
        oneLeft.setBounds(360,161,107,34);
        twoLeft.setBounds(467,161,107,34);
        oneAttack.setBounds(360,195,107,34);
        twoAttack.setBounds(467,195,107,34);
        oneCrouch.setBounds(360,229,107,34);
        twoCrouch.setBounds(467,229,107,34);
        //add Mouselistener
        oneUp.addMouseListener(this); oneUp.setFocusable(false);
        twoUp.addMouseListener(this); twoUp.setFocusable(false);
        oneRight.addMouseListener(this); oneRight.setFocusable(false);
        twoRight.addMouseListener(this); twoRight.setFocusable(false);
        oneLeft.addMouseListener(this); oneLeft.setFocusable(false);
        twoLeft.addMouseListener(this); twoLeft.setFocusable(false);
        oneAttack.addMouseListener(this); oneAttack.setFocusable(false);
        twoAttack.addMouseListener(this); twoAttack.setFocusable(false);
        oneCrouch.addMouseListener(this); oneCrouch.setFocusable(false);
        twoCrouch.addMouseListener(this); twoCrouch.setFocusable(false);
        this.addKeyListener(this);
        //adds objects to form
        add(oneUp);
        add(twoUp);
        add(oneRight);
        add(twoRight);
        add(oneLeft);
        add(twoLeft);
        add(oneAttack);
        add(twoAttack);
        add(oneCrouch);
        add(twoCrouch);
        add(optionsImage);
       }
      
      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}
      public void mouseClicked(MouseEvent e) {
           clicked=e;
           this.addKeyListener(this);
           this.setFocusable(true);
           
      }
      
     // public static KeyEvent[] getUp (){return up;}
      
      
      public void keyTyped(KeyEvent kevt) {
        if (clicked!=null){
        if (clicked.getSource()==oneUp){
          up[0]=kevt;
          oneUp.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==twoUp){
          up[1]=kevt;
          twoUp.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==oneRight){
          right[0]=kevt;
          oneRight.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==twoRight){
          right[1]=kevt;
          twoRight.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==oneLeft){
          left[0]=kevt;
          oneLeft.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==twoLeft){
          left[1]=kevt;
          twoLeft.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==oneAttack){
          attack[0]=kevt;
          oneAttack.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==twoAttack){
          attack[1]=kevt;
          twoAttack.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==oneCrouch){
          crouch[0]=kevt;
          oneCrouch.setText(Character.toString(kevt.getKeyChar()));}
        else if (clicked.getSource()==twoCrouch){
          crouch[1]=kevt;
          twoCrouch.setText(Character.toString(kevt.getKeyChar()));}
        }
      }
      public void keyPressed(KeyEvent evt) {}
      public void keyReleased (KeyEvent ke) {}
      
      
      
        public javax.swing.JLabel optionsImage;
    }
    any help would be appreciated

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

  3. #3
    quafflepunch is offline Member
    Join Date
    Jan 2011
    Location
    Canada
    Posts
    6
    Rep Power
    0

Similar Threads

  1. setting classpath
    By ravikumar9 in forum New To Java
    Replies: 2
    Last Post: 08-05-2009, 12:36 PM
  2. setting up javadocs?
    By PersonUsingEclipse in forum Eclipse
    Replies: 0
    Last Post: 01-29-2009, 03:42 AM
  3. setting and getting array
    By jgonzalez14 in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 06:54 AM
  4. Setting the DSN
    By tim in forum JDBC
    Replies: 1
    Last Post: 02-14-2008, 09:55 PM
  5. Help with setting up please
    By BlitzA in forum New To Java
    Replies: 6
    Last Post: 12-29-2007, 12:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •