Results 1 to 3 of 3
Thread: Help, Setting Key Code
- 01-04-2011, 11:06 PM #1
Member
- Join Date
- Jan 2011
- Location
- Canada
- Posts
- 6
- Rep Power
- 0
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.
any help would be appreciatedJava 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; }
- 01-05-2011, 12:57 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
You are using Swing, you can use Key Binding instead.
See this page How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- 01-05-2011, 01:37 AM #3
Member
- Join Date
- Jan 2011
- Location
- Canada
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
setting classpath
By ravikumar9 in forum New To JavaReplies: 2Last Post: 08-05-2009, 12:36 PM -
setting up javadocs?
By PersonUsingEclipse in forum EclipseReplies: 0Last Post: 01-29-2009, 03:42 AM -
setting and getting array
By jgonzalez14 in forum New To JavaReplies: 6Last Post: 11-26-2008, 06:54 AM -
Setting the DSN
By tim in forum JDBCReplies: 1Last Post: 02-14-2008, 09:55 PM -
Help with setting up please
By BlitzA in forum New To JavaReplies: 6Last Post: 12-29-2007, 12:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks