java编写一个APPLET小程序!猜数字
发布网友
发布时间:2024-09-29 08:00
我来回答
共1个回答
热心网友
时间:2024-11-24 19:47
按这这个步骤走,里面自动生成的代码我有改过,
这样自己比较好去认识.
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);
}
}