import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
class SimpleGameFrame extends JFrame {
private JLabel one;
private JLabel two;
private JLabel[] jl;
private JButton start;
private JButton output;
private JTextField input;
private int[] arry = new int[5];;
SimpleGameFrame() {
this.setSize(500, 400);
this.setLocation(200, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle( "数字小游戏 ");
this.addComponent();
this.setVisible(true);
}
private void addComponent() {
JPanel jp = (JPanel)this.getContentPane();
jp.setLayout(null);
one = new JLabel( "考验考验你的数字意识 ");
one.setBounds(180, 70, 180, 20);
jp.add(one);
jl = new JLabel[4];
for(int i=0;i <jl.length;i++) {
jl[i] = new JLabel();
jl[i].setBorder(new LineBorder(Color.black));
jl[i].setBounds(60+i*100, 160, 90, 30);
jp.add(jl[i]);
}
two = new JLabel( "请找出规律填入第五个数字: ");
two.setBounds(80, 230, 180, 20);
jp.add(two);
input = new JTextField();
input.setBounds(260, 225, 100, 30);
jp.add(input);
start = new JButton( "开始 ");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setNum();
for(int i=0;i <jl.length;i++) {
jl[i].setText(String.valueOf(arry[i]));
}
input.setText( " ");
input.requestFocus();
}
});
start.setBounds(100, 300, 120, 30);
jp.add(start);
output = new JButton( "看结果 ");
output.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
judgeRes();
}
});
output.setBounds(260, 300, 120, 30);
jp.add(output);
}
private void setNum() {
Random ran = new Random();
int num = Math.abs(ran.nextInt()%9);
arry[0] = num;
for(int i=1;i <arry.length;i++) {
arry[i] = 2*arry[i-1]+1;
}
}
private void judgeRes() {
if(input.getText().equals(String.valueOf(arry[4]))) {
JOptionPane.showMessageDialog(null, "哇,答对了,你好聪明! ");
}else if(input.getText().equals( " ")) {
JOptionPane.showMessageDialog(null, "你还没有填答案呢! ");
input.requestFocus();
}else {
JOptionPane.showMessageDialog(null, "你笨笨哦,再试一次吧! ");
input.setText(String.valueOf(arry[4]));
}
start.setText( "再试一次 ");
}
}
------解决方案--------------------
好像没看到对象 :)
------解决方案--------------------
不能说没有面向对象的思想……