java编写一个APPLET小程序!猜数字 5
随机生成1~100之间的一个整数,2个LABEL第一行显示:猜数游戏,第2行显示提示语,在没有开始游戏的时候显示:“输入数字开始游戏”,当猜测的数字大于正确答案时,显示“...
随机生成1~100之间的一个整数,
2个LABEL 第一行显示:猜数游戏,第2行显示提示语,在没有开始游戏的时候显示:“输入数字开始游戏”,当猜测的数字大于正确答案时,显示“输入过大”,小于正确答案时,显示“输入过小”,正确则显示“猜对了”。
文本框一个,用来接受从键盘输入的数字。
按钮一个,在没有开始游戏前(就是文本框中没有输入数字之前)不可用,当游戏结束时(输入的数字等于正确答案)文本框不可用(且清空),此时按钮可用。
对JAVAX,Swing组件不了解,最好能用AWT组件编写. 展开
2个LABEL 第一行显示:猜数游戏,第2行显示提示语,在没有开始游戏的时候显示:“输入数字开始游戏”,当猜测的数字大于正确答案时,显示“输入过大”,小于正确答案时,显示“输入过小”,正确则显示“猜对了”。
文本框一个,用来接受从键盘输入的数字。
按钮一个,在没有开始游戏前(就是文本框中没有输入数字之前)不可用,当游戏结束时(输入的数字等于正确答案)文本框不可用(且清空),此时按钮可用。
对JAVAX,Swing组件不了解,最好能用AWT组件编写. 展开
1个回答
展开全部
按这这个步骤走,里面自动生成的代码我有改过,
这样自己比较好去认识.
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame1
extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JTextField txtNum = new JTextField();
JLabel jLabel1 = new JLabel();
JButton btnSC = new JButton();
JButton btnC = new JButton();
int num;//存放生成的数
int count;//存放猜了几次
JPasswordField txtPw = new JPasswordField();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
cleanTxt();
this.txtNum.setEnabled(false);
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(xYLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
txtNum.setText("jTextField1");
jLabel1.setText("Input:");
btnSC.setToolTipText("");
btnSC.setText("生成一个数");
btnSC.addActionListener(new Frame1_btnSC_actionAdapter(this));
btnC.setEnabled(false);
btnC.setText("猜");
btnC.addActionListener(new Frame1_btnC_actionAdapter(this));
txtPw.setText("jPasswordField1");
contentPane.add(txtNum, new XYConstraints(139, 72, 121, 29));
contentPane.add(btnSC, new XYConstraints(211, 183, 100, 35));
contentPane.add(btnC, new XYConstraints(94, 183, 100, 35));
contentPane.add(jLabel1, new XYConstraints(77, 79, 83, 24));
contentPane.add(txtPw, new XYConstraints(141, 126, 120, -1));
}
public void btnSC_actionPerformed(ActionEvent e) {
this.count=0;
this.txtNum.setEnabled(true);
this.num=(int)(Math.random()*100);
this.btnSC.setEnabled(false);
this.btnC.setEnabled(true);
JOptionPane.showMessageDialog(this,"已经生成的随机数,可以开始猜了!!");
}
private void cleanTxt(){
this.txtNum.setText("");
this.txtNum.setText("");
this.txtNum.requestFocus();
}
public void btnC_actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(this,this.txtPw.getText());
String pw=String.valueOf( this.txtPw.getPassword() );
JOptionPane.showMessageDialog(this,pw);
try {
int cai = Integer.parseInt(this.txtNum.getText());
if(cai>this.num){
JOptionPane.showMessageDialog(this,"大了");
}else if(cai<this.num){
JOptionPane.showMessageDialog(this,"小了");
}else{
JOptionPane.showMessageDialog(this,"恭喜你猜对了!!"+this.count);
this.btnC.setEnabled(false);
this.btnSC.setEnabled(true);
return ;
}
count++;
cleanTxt();
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,"输入的不是数字,请重新输入");
cleanTxt();
}
}
}
class Frame1_btnC_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_btnC_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnC_actionPerformed(e);
}
}
class Frame1_btnSC_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_btnSC_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnSC_actionPerformed(e);
}
}
这样自己比较好去认识.
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame1
extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JTextField txtNum = new JTextField();
JLabel jLabel1 = new JLabel();
JButton btnSC = new JButton();
JButton btnC = new JButton();
int num;//存放生成的数
int count;//存放猜了几次
JPasswordField txtPw = new JPasswordField();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
cleanTxt();
this.txtNum.setEnabled(false);
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(xYLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
txtNum.setText("jTextField1");
jLabel1.setText("Input:");
btnSC.setToolTipText("");
btnSC.setText("生成一个数");
btnSC.addActionListener(new Frame1_btnSC_actionAdapter(this));
btnC.setEnabled(false);
btnC.setText("猜");
btnC.addActionListener(new Frame1_btnC_actionAdapter(this));
txtPw.setText("jPasswordField1");
contentPane.add(txtNum, new XYConstraints(139, 72, 121, 29));
contentPane.add(btnSC, new XYConstraints(211, 183, 100, 35));
contentPane.add(btnC, new XYConstraints(94, 183, 100, 35));
contentPane.add(jLabel1, new XYConstraints(77, 79, 83, 24));
contentPane.add(txtPw, new XYConstraints(141, 126, 120, -1));
}
public void btnSC_actionPerformed(ActionEvent e) {
this.count=0;
this.txtNum.setEnabled(true);
this.num=(int)(Math.random()*100);
this.btnSC.setEnabled(false);
this.btnC.setEnabled(true);
JOptionPane.showMessageDialog(this,"已经生成的随机数,可以开始猜了!!");
}
private void cleanTxt(){
this.txtNum.setText("");
this.txtNum.setText("");
this.txtNum.requestFocus();
}
public void btnC_actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(this,this.txtPw.getText());
String pw=String.valueOf( this.txtPw.getPassword() );
JOptionPane.showMessageDialog(this,pw);
try {
int cai = Integer.parseInt(this.txtNum.getText());
if(cai>this.num){
JOptionPane.showMessageDialog(this,"大了");
}else if(cai<this.num){
JOptionPane.showMessageDialog(this,"小了");
}else{
JOptionPane.showMessageDialog(this,"恭喜你猜对了!!"+this.count);
this.btnC.setEnabled(false);
this.btnSC.setEnabled(true);
return ;
}
count++;
cleanTxt();
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,"输入的不是数字,请重新输入");
cleanTxt();
}
}
}
class Frame1_btnC_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_btnC_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnC_actionPerformed(e);
}
}
class Frame1_btnSC_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_btnSC_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnSC_actionPerformed(e);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询