posted on 2009-12-26 22:01 五歌 阅读(1528) 评论(2) 编辑 收藏
asdasd 回复 更多评论
package ch10;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JLabel;import java.awt.Font;import javax.swing.JTextField;import javax.swing.JButton;import java.awt.Color;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.util.Random;public class MyGuessNumberGame { private JFrame frame; //主界面 private JTextField textField; //输入数字的单行文本框 private JLabel jlbResult; //显示结果和提示信息的标签 private JButton jbtGuessButton; // 猜数字按钮 private JButton button; private int number; //生成的待猜随机数 /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MyGuessNumberGame window = new MyGuessNumberGame(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public MyGuessNumberGame() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { //产生一个随机数字 this.getRandomInteger(); frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.getContentPane().setLayout(null);//把布局管理器设为null,可以自由拖放控件 JLabel label = new JLabel("请输入数字"); label.setBounds(49, 61, 96, 15); frame.getContentPane().add(label); JLabel label_1 = new JLabel("猜数字"); label_1.setFont(new Font("微软雅黑", Font.PLAIN, 24)); label_1.setBounds(131, 10, 134, 35); frame.getContentPane().add(label_1); textField = new JTextField(); textField.setBounds(155, 58, 66, 21); frame.getContentPane().add(textField); textField.setColumns(10); JButton jbtGuessButton = new JButton("我猜一猜"); //自定义一个事件监听类,并注册到猜数字的按钮上去 GuessListener guessListener=new GuessListener(); jbtGuessButton.addActionListener(guessListener); jbtGuessButton.setBounds(248, 57, 66, 23); frame.getContentPane().add(jbtGuessButton); JButton button = new JButton("再来一次"); GuessListener1 guessListener1=new GuessListener1(); button.addActionListener(guessListener1); button.setBounds(248, 57, 66, 23); frame.getContentPane().add(button); jlbResult = new JLabel(); jlbResult.setForeground(Color.RED); jlbResult.setFont(new Font("宋体", Font.PLAIN, 18)); jlbResult.setBounds(91, 104, 199, 35); frame.getContentPane().add(jlbResult); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void getRandomInteger(){ Random rdm=new Random(System.currentTimeMillis()); this.number=rdm.nextInt(100); } protected void isBingo(){ String str=textField.getText(); if(str.length()==0){ jlbResult.setText("请输入一个0~100之间的整数,包括0但是不包括100"); return; }else { try{ int tmp=Integer.parseInt(str); if(tmp>0){ if(tmp==this.number){ jlbResult.setText("恭喜你猜对了"); jbtGuessButton.setVisible(false); button.setVisible(true); getRandomInteger(); }else if(tmp<this.number){ jlbResult.setText("小了点,你再试试"); }else{ jlbResult.setText("大了点,你再试试"); } } else{ jlbResult.setText("请输入一个0~100之间的整数"); } } catch(NumberFormatException x) { jlbResult.setText("请输入数字!!!"); } } } //定义内部类,实现猜数字按钮的事件监听器 class GuessListener implements ActionListener{ public void actionPerformed(ActionEvent e) { isBingo(); } } class GuessListener1 implements ActionListener{ public void actionPerformed(ActionEvent e) { jbtGuessButton.setVisible(true); button.setVisible(false); jlbResult.setText(" "); textField = new JTextField("0"); textField.setBounds(155, 58, 66, 21); frame.getContentPane().add(textField); textField.setColumns(10); } }} 回复 更多评论
Powered by: BlogJava Copyright © 五歌