public class Jframe extends JFrame implements KeyListener {
JButton[][] **s = new JButton[4][4]; // 定义二维数组来存储行列值
ArrayList<JButton> als = new ArrayList<JButton>(); // 创建一个集合来存储16个按钮
int i = 16; // 剩余方块数
int score = 0; // 分数
int num = 0; // 分数的一半
int l1, l2, l3, l4; // 定义标记
Random random = new Random(); // 定义一个随机数
private JPanel contentPane;
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton button10;
JButton button11;
JButton button12;
JButton button13;
JButton button14;
JButton button15;
private JTextField textField;
JLabel lblTips;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Jframe frame = new Jframe();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 窗口布局
*/
public Jframe() {
setTitle("2048");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 550);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
setResizable(false);
JLabel label = new JLabel("\u5206\u6570");
label.setBounds(348, 13, 30, 15);
contentPane.add(label);
button0 = new JButton("");
button0.setFont(new Font("楷体", Font.PLAIN, 30));
button0.setBounds(43, 57, 100, 100);
contentPane.add(button0);
button1 = new JButton("");
button1.setFont(new Font("楷体", Font.PLAIN, 30));
button1.setBounds(143, 57, 100, 100);
contentPane.add(button1);
button2 = new JButton("");
button2.setFont(new Font("楷体", Font.PLAIN, 30));
button2.setBounds(242, 57, 100, 100);
contentPane.add(button2);
button3 = new JButton("");
button3.setFont(new Font("楷体", Font.PLAIN, 30));
button3.setBounds(342, 57, 100, 100);
contentPane.add(button3);
button4 = new JButton("");
button4.setFont(new Font("楷体", Font.PLAIN, 30));
button4.setBounds(43, 158, 100, 100);
contentPane.add(button4);
button5 = new JButton("");
button5.setFont(new Font("楷体", Font.PLAIN, 30));
button5.setBounds(143, 158, 100, 100);
contentPane.add(button5);
button6 = new JButton("");
button6.setFont(new Font("楷体", Font.PLAIN, 30));
button6.setBounds(242, 158, 100, 100);
contentPane.add(button6);
button7 = new JButton("");
button7.setFont(new Font("楷体", Font.PLAIN, 30));
button7.setBounds(342, 158, 100, 100);
contentPane.add(button7);
button8 = new JButton("");
button8.setFont(new Font("楷体", Font.PLAIN, 30));
button8.setBounds(43, 259, 100, 100);
contentPane.add(button8);
button9 = new JButton("");
button9.setFont(new Font("楷体", Font.PLAIN, 30));
button9.setBounds(143, 259, 100, 100);
contentPane.add(button9);
button10 = new JButton("");
button10.setFont(new Font("楷体", Font.PLAIN, 30));
button10.setBounds(242, 259, 100, 100);
contentPane.add(button10);
button11 = new JButton("");
button11.setFont(new Font("楷体", Font.PLAIN, 30));
button11.setBounds(342, 259, 100, 100);
contentPane.add(button11);
button12 = new JButton("");
button12.setFont(new Font("楷体", Font.PLAIN, 30));
button12.setBounds(43, 360, 100, 100);
contentPane.add(button12);
button13 = new JButton("");
button13.setFont(new Font("楷体", Font.PLAIN, 30));
button13.setBounds(143, 360, 100, 100);
contentPane.add(button13);
button14 = new JButton("");
button14.setFont(new Font("楷体", Font.PLAIN, 30));
button14.setBounds(242, 360, 100, 100);
contentPane.add(button14);
button15 = new JButton("");
button15.setFont(new Font("楷体", Font.PLAIN, 30));
button15.setBounds(342, 360, 100, 100);
contentPane.add(button15);
textField = new JTextField();
textField.setEditable(false);
textField.setFont(new Font("楷体", Font.PLAIN, 15));
textField.setText("0");
textField.setBounds(376, 10, 66, 21);
contentPane.add(textField);
textField.setColumns(10);
lblTips = new JLabel("Tips:\u6309\u4E0A\u3001\u4E0B\u3001\u5DE6\u3001\u53F3\u8FDB\u884C\u6E38\u620F");
lblTips.setBounds(141, 470, 201, 15);
contentPane.add(lblTips);
this.Store();
this.Randomizer();
this.Randomizer();
textField.addKeyListener(this); // 注册键盘监听事件
}
JButton[][] **s = new JButton[4][4]; // 定义二维数组来存储行列值
ArrayList<JButton> als = new ArrayList<JButton>(); // 创建一个集合来存储16个按钮
int i = 16; // 剩余方块数
int score = 0; // 分数
int num = 0; // 分数的一半
int l1, l2, l3, l4; // 定义标记
Random random = new Random(); // 定义一个随机数
private JPanel contentPane;
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton button10;
JButton button11;
JButton button12;
JButton button13;
JButton button14;
JButton button15;
private JTextField textField;
JLabel lblTips;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Jframe frame = new Jframe();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 窗口布局
*/
public Jframe() {
setTitle("2048");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 550);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
setResizable(false);
JLabel label = new JLabel("\u5206\u6570");
label.setBounds(348, 13, 30, 15);
contentPane.add(label);
button0 = new JButton("");
button0.setFont(new Font("楷体", Font.PLAIN, 30));
button0.setBounds(43, 57, 100, 100);
contentPane.add(button0);
button1 = new JButton("");
button1.setFont(new Font("楷体", Font.PLAIN, 30));
button1.setBounds(143, 57, 100, 100);
contentPane.add(button1);
button2 = new JButton("");
button2.setFont(new Font("楷体", Font.PLAIN, 30));
button2.setBounds(242, 57, 100, 100);
contentPane.add(button2);
button3 = new JButton("");
button3.setFont(new Font("楷体", Font.PLAIN, 30));
button3.setBounds(342, 57, 100, 100);
contentPane.add(button3);
button4 = new JButton("");
button4.setFont(new Font("楷体", Font.PLAIN, 30));
button4.setBounds(43, 158, 100, 100);
contentPane.add(button4);
button5 = new JButton("");
button5.setFont(new Font("楷体", Font.PLAIN, 30));
button5.setBounds(143, 158, 100, 100);
contentPane.add(button5);
button6 = new JButton("");
button6.setFont(new Font("楷体", Font.PLAIN, 30));
button6.setBounds(242, 158, 100, 100);
contentPane.add(button6);
button7 = new JButton("");
button7.setFont(new Font("楷体", Font.PLAIN, 30));
button7.setBounds(342, 158, 100, 100);
contentPane.add(button7);
button8 = new JButton("");
button8.setFont(new Font("楷体", Font.PLAIN, 30));
button8.setBounds(43, 259, 100, 100);
contentPane.add(button8);
button9 = new JButton("");
button9.setFont(new Font("楷体", Font.PLAIN, 30));
button9.setBounds(143, 259, 100, 100);
contentPane.add(button9);
button10 = new JButton("");
button10.setFont(new Font("楷体", Font.PLAIN, 30));
button10.setBounds(242, 259, 100, 100);
contentPane.add(button10);
button11 = new JButton("");
button11.setFont(new Font("楷体", Font.PLAIN, 30));
button11.setBounds(342, 259, 100, 100);
contentPane.add(button11);
button12 = new JButton("");
button12.setFont(new Font("楷体", Font.PLAIN, 30));
button12.setBounds(43, 360, 100, 100);
contentPane.add(button12);
button13 = new JButton("");
button13.setFont(new Font("楷体", Font.PLAIN, 30));
button13.setBounds(143, 360, 100, 100);
contentPane.add(button13);
button14 = new JButton("");
button14.setFont(new Font("楷体", Font.PLAIN, 30));
button14.setBounds(242, 360, 100, 100);
contentPane.add(button14);
button15 = new JButton("");
button15.setFont(new Font("楷体", Font.PLAIN, 30));
button15.setBounds(342, 360, 100, 100);
contentPane.add(button15);
textField = new JTextField();
textField.setEditable(false);
textField.setFont(new Font("楷体", Font.PLAIN, 15));
textField.setText("0");
textField.setBounds(376, 10, 66, 21);
contentPane.add(textField);
textField.setColumns(10);
lblTips = new JLabel("Tips:\u6309\u4E0A\u3001\u4E0B\u3001\u5DE6\u3001\u53F3\u8FDB\u884C\u6E38\u620F");
lblTips.setBounds(141, 470, 201, 15);
contentPane.add(lblTips);
this.Store();
this.Randomizer();
this.Randomizer();
textField.addKeyListener(this); // 注册键盘监听事件
}