Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1)Use <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html" rel="nofollow">KeyBindings</a> KeyListener has 2 big issues,first you listen to all keys and second you have to have focus and be focusable. Instead KeyBinding you bind for a key and you don't have to be in focus.</p> <p>Simple Example:</p> <pre><code>AbstractAction escapeAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { //code here example ((JComponent)e.getSource()).setVisible(Boolean.FALSE); }}; String key = "ESCAPE"; KeyStroke keyStroke = KeyStroke.getKeyStroke(key); component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, key); component.getActionMap().put(key, escapeAction); </code></pre> <p>You can use these JComponent constants</p> <pre><code>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT WHEN_FOCUSED WHEN_IN_FOCUSED_WINDOW </code></pre> <p>2) Don't use concrete inheritance if it isn't necessary at all.</p> <p>3) Don't implement ActionListener in top classes, see <a href="http://www.oodesign.com/single-responsibility-principle.html" rel="nofollow">Single Responsability Principle</a> Example Change this:</p> <pre><code>public class Board extends JPanel implements ActionListener{ </code></pre> <p>to:</p> <pre><code> public class Board{ private JPanel panel; private class MyActionListener implements ActionListener{ //code here } } </code></pre> <p>4) Don't use inheritance if it's just the same for example in your <code>KeyAdapter</code> , you don't add nothing to it, just use <code>KeyAdapter</code> (Now you are gonna to use keybinding so this is useless but to know :) ). </p> <p>5) Add @Override annotation when you do overriding , also you should override <code>paintComponent(..)</code> instead of <code>paint(..)</code> in swing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload