I want both players to play at the same time, not in turn. If possible - easier
public class OneClass implements KeyListener, ActionListener{ JFrame frame; JButton button1,button2,button_cel; String s=""; OneClass(){ //Стандарт, можно не читать frame=new JFrame(); frame.setFocusable(true); frame.addKeyListener(this); button1=new JButton(); button2=new JButton(); button1.addActionListener(this); button2.addActionListener(this); button_cel=new JButton(); button_cel.setSize(20, 20); button1.setFocusable(false); button2.setFocusable(false); frame.setLayout(null); frame.add(button1); frame.add(button2); button1.setBackground(Color.RED); button2.setBackground(Color.GREEN); frame.setLocation(0,0); frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height-35); button1.setSize(10,10); button2.setSize(10, 10); button1.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2, Toolkit.getDefaultToolkit().getScreenSize().height/2-5); button2.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-10, Toolkit.getDefaultToolkit().getScreenSize().height/2-5); frame.setVisible(true); frame.getContentPane().setBackground(new Color(240,240,240)); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new OneClass(); } }); } //Вот тут-самое интерестное public void keyPressed(KeyEvent e) { button1.setSize(10, 10); button1.setText(""); if(e.getKeyCode()==87){button2.setLocation(button2.getLocation().x, button2.getLocation().y-10);} if(e.getKeyCode()==68){button2.setLocation(button2.getLocation().x+10, button2.getLocation().y);} if(e.getKeyCode()==83){button2.setLocation(button2.getLocation().x, button2.getLocation().y+10);} if(e.getKeyCode()==65){button2.setLocation(button2.getLocation().x-10, button2.getLocation().y);} if(e.getKeyCode()==38){button1.setLocation(button1.getLocation().x, button1.getLocation().y-10);} if(e.getKeyCode()==39){button1.setLocation(button1.getLocation().x+10, button1.getLocation().y);} if(e.getKeyCode()==40){button1.setLocation(button1.getLocation().x, button1.getLocation().y+10);} if(e.getKeyCode()==37){button1.setLocation(button1.getLocation().x-10, button1.getLocation().y);} } public void keyReleased(KeyEvent e) { System.out.println(e.getKeyCode()); } public void keyTyped(KeyEvent e) { } } }
Here is a jar
Imports missed