自己写的2048小游戏,仅支持鼠标操作
主要是我不知道怎么添加键盘监听
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JF2048 extends JFrame { /** * */ private static final long serialVersionUID = 1L; private Ja2048 ja; public JButton b[] = { new JButton(), new JButton(), new JButton(), new JButton() }; public JButton back = new JButton("back"); private ActionListener b0 = new ActionListener(){ public void actionPerformed(ActionEvent e){ ja.cp0(); }}; private ActionListener b1 = new ActionListener(){ public void actionPerformed(ActionEvent e){ ja.cp1(); }}; private ActionListener b2 = new ActionListener(){ public void actionPerformed(ActionEvent e){ ja.cp2(); }}; private ActionListener b3 = new ActionListener(){ public void actionPerformed(ActionEvent e){ ja.cp3(); }}; private ActionListener back1 = new ActionListener(){ public void actionPerformed(ActionEvent e){ ja.back(); }}; public JLabel[][] la ={ {new JLabel(),new JLabel(),new JLabel(),new JLabel()}, {new JLabel(),new JLabel(),new JLabel(),new JLabel()}, {new JLabel(),new JLabel(),new JLabel(),new JLabel()}, {new JLabel(),new JLabel(),new JLabel(),new JLabel()}, }; public JF2048(){ super("2048"); //this.addKeyListener(x); b[0].setBounds(3,20,16,156); b[1].setBounds(178,20,16,156); b[2].setBounds(20,3,156,16); b[3].setBounds(20,178,156,16); back.setBounds(3,3,16,16); b[0].addActionListener(b0); b[1].addActionListener(b1); b[2].addActionListener(b2); b[3].addActionListener(b3); back.addActionListener(back1); for(int i =0;i<4;i++) for(int j =0;j<4;j++){ la[i][j].setBounds(20+40*i,20+40*j,36,36); la[i][j].setOpaque(true); //la[i][j].setFont(new Font("幼圆",1,24)); la[i][j].setHorizontalAlignment(SwingConstants.CENTER); } this.setSize(217,238); this.add(b[0]); this.add(b[1]); this.add(b[2]); this.add(b[3]); this.add(back); for(int i =0;i<4;i++) for(int j =0;j<4;j++) this.add(la[i][j]); JLabel p = new JLabel(); p.setBackground(new Color(127,127,127)); p.setOpaque(true); this.add(p); } public static void main(String[] args){ JF2048 jf = new JF2048(); jf.ja=new Ja2048(jf); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } }