程序随机产生20—50根火柴,由人与计算机轮流拿,每次拿的数量不超过3根,拿到最后一根为胜。(使用图形界面)
程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;public class GetMatch extends JFrame implements ActionListener,MouseListener{
public static int sumMatch;
private JLabel lbPer,lbCom,lbLast;
private JButton qd;
private JRadioButton rdbOne,rdbTwo,rdbThree;
private JTextField tfPcshow,tfLastshow;
private JPanel jps;
private Container Container;
private GridBagLayout Layout;
final ButtonGroup group;
public GetMatch(){
super("火柴游戏");
this.setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
group=new ButtonGroup();
}
public void setLayout(){
Container contrainer=getContentPane();
lbPer=new JLabel("Person取数");
lbPer.setBounds(20,25,100,25);
lbCom=new JLabel("Computer取数");
lbCom.setBounds(20,70,100,25);
lbLast=new JLabel("剩余火柴数");
lbLast.setBounds(20,115,100,25);
qd=new JButton("PC启动");
qd.setBounds(310,70,50,25);
qd.addActionListener(this);
getContentPane().add(qd);
rdbOne=new JRadioButton("1");
rdbOne.setBounds(135,25,35,35);
rdbOne.addMouseListener(this);
getContentPane().add(rdbOne);
rdbTwo=new JRadioButton("2");
rdbTwo.setBounds(170,25,35,35);
rdbTwo.addMouseListener(this);
getContentPane().add(rdbTwo);
rdbThree=new JRadioButton("3");
rdbThree.setBounds(205,25,35,35);
rdbThree.addMouseListener(this);
getContentPane().add(rdbThree);
tfPcshow=new JTextField();
tfPcshow.setBounds(135,70,150,25);
tfLastshow=new JTextField();
tfLastshow.setBounds(135,115,150,25);
JPanel jpS=new JPanel();
jpS.setVisible(true);
jpS.setBounds(135,25,130,40);
group.add(rdbOne);
group.add(rdbTwo);
group.add(rdbThree);
jpS.add(rdbOne);
jpS.add(rdbTwo);
jpS.add(rdbThree);
contrainer.add(lbPer);
contrainer.add(jpS);
contrainer.add(lbCom);
contrainer.add(tfPcshow);
contrainer.add(qd);
contrainer.add(lbLast);
contrainer.add(tfLastshow);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof JButton){
int sumMatch =(int)(Math.random()*(50-20))+20;
tfPcshow.setText("计算机随机取了"+sumMatch+"根火柴");
}
}
public void mouseClicked(MouseEvent e){
if(e.getSource() instanceof JRadioButton){
int k=0;
if(rdbOne.isSelected()){
k=Integer.parseInt(rdbOne.getText());
}
else if(rdbTwo.isSelected()){
k=Integer.parseInt(rdbOne.getText());
}
else if(rdbThree.isSelected()){
k=Integer.parseInt(rdbThree.getText());
}
sumMatch-=k;
if(sumMatch==0){
JOptionPane.showMessageDialog(null, "你赢啦","TEST",JOptionPane.INFORMATION_MESSAGE);
}
else if(sumMatch<0){
JOptionPane.showMessageDialog(null, "出错啦!重新来过","TEST",JOptionPane.INFORMATION_MESSAGE);
}
tfPcshow.setText("计算机取了"+Computer()+"根");
tfLastshow.setText("剩余火柴"+sumMatch+"根");
}
}
public int Computer(){
if((sumMatch<=3)&&(sumMatch>0)){
JOptionPane.showMessageDialog(null, "你输了!", "TEST",JOptionPane.INFORMATION_MESSAGE);
return sumMatch;}
else if(sumMatch!=0){
int a =(int)(Math.random()*(3-1))+1;
sumMatch-=a;
return sumMatch;
}
return 0;
}
public void mouseReleased(MouseEvent e){
//用户松开鼠标调用此方法
}
public void mouseEntered(MouseEvent e){
//鼠标进入调用此方法
}
public void mouseExited(MouseEvent e){
//鼠标退出此窗口调用此方法
}
public void mousePressed(MouseEvent e){
//按下鼠标并移动鼠标时调用此方法
} public static void main(String[] args){
GetMatch frame = new GetMatch();
frame.setLayout();
frame.setVisible(true);
}}
程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;public class GetMatch extends JFrame implements ActionListener,MouseListener{
public static int sumMatch;
private JLabel lbPer,lbCom,lbLast;
private JButton qd;
private JRadioButton rdbOne,rdbTwo,rdbThree;
private JTextField tfPcshow,tfLastshow;
private JPanel jps;
private Container Container;
private GridBagLayout Layout;
final ButtonGroup group;
public GetMatch(){
super("火柴游戏");
this.setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
group=new ButtonGroup();
}
public void setLayout(){
Container contrainer=getContentPane();
lbPer=new JLabel("Person取数");
lbPer.setBounds(20,25,100,25);
lbCom=new JLabel("Computer取数");
lbCom.setBounds(20,70,100,25);
lbLast=new JLabel("剩余火柴数");
lbLast.setBounds(20,115,100,25);
qd=new JButton("PC启动");
qd.setBounds(310,70,50,25);
qd.addActionListener(this);
getContentPane().add(qd);
rdbOne=new JRadioButton("1");
rdbOne.setBounds(135,25,35,35);
rdbOne.addMouseListener(this);
getContentPane().add(rdbOne);
rdbTwo=new JRadioButton("2");
rdbTwo.setBounds(170,25,35,35);
rdbTwo.addMouseListener(this);
getContentPane().add(rdbTwo);
rdbThree=new JRadioButton("3");
rdbThree.setBounds(205,25,35,35);
rdbThree.addMouseListener(this);
getContentPane().add(rdbThree);
tfPcshow=new JTextField();
tfPcshow.setBounds(135,70,150,25);
tfLastshow=new JTextField();
tfLastshow.setBounds(135,115,150,25);
JPanel jpS=new JPanel();
jpS.setVisible(true);
jpS.setBounds(135,25,130,40);
group.add(rdbOne);
group.add(rdbTwo);
group.add(rdbThree);
jpS.add(rdbOne);
jpS.add(rdbTwo);
jpS.add(rdbThree);
contrainer.add(lbPer);
contrainer.add(jpS);
contrainer.add(lbCom);
contrainer.add(tfPcshow);
contrainer.add(qd);
contrainer.add(lbLast);
contrainer.add(tfLastshow);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof JButton){
int sumMatch =(int)(Math.random()*(50-20))+20;
tfPcshow.setText("计算机随机取了"+sumMatch+"根火柴");
}
}
public void mouseClicked(MouseEvent e){
if(e.getSource() instanceof JRadioButton){
int k=0;
if(rdbOne.isSelected()){
k=Integer.parseInt(rdbOne.getText());
}
else if(rdbTwo.isSelected()){
k=Integer.parseInt(rdbOne.getText());
}
else if(rdbThree.isSelected()){
k=Integer.parseInt(rdbThree.getText());
}
sumMatch-=k;
if(sumMatch==0){
JOptionPane.showMessageDialog(null, "你赢啦","TEST",JOptionPane.INFORMATION_MESSAGE);
}
else if(sumMatch<0){
JOptionPane.showMessageDialog(null, "出错啦!重新来过","TEST",JOptionPane.INFORMATION_MESSAGE);
}
tfPcshow.setText("计算机取了"+Computer()+"根");
tfLastshow.setText("剩余火柴"+sumMatch+"根");
}
}
public int Computer(){
if((sumMatch<=3)&&(sumMatch>0)){
JOptionPane.showMessageDialog(null, "你输了!", "TEST",JOptionPane.INFORMATION_MESSAGE);
return sumMatch;}
else if(sumMatch!=0){
int a =(int)(Math.random()*(3-1))+1;
sumMatch-=a;
return sumMatch;
}
return 0;
}
public void mouseReleased(MouseEvent e){
//用户松开鼠标调用此方法
}
public void mouseEntered(MouseEvent e){
//鼠标进入调用此方法
}
public void mouseExited(MouseEvent e){
//鼠标退出此窗口调用此方法
}
public void mousePressed(MouseEvent e){
//按下鼠标并移动鼠标时调用此方法
} public static void main(String[] args){
GetMatch frame = new GetMatch();
frame.setLayout();
frame.setVisible(true);
}}
解决方案 »
- java中的inputmap将keystroke对象映射到任意对象而不是动作对象有什么意义
- wml文件中调用wmls文件中的脚本
- 关于properties读取路径问题
- 晕了,又是个问题.....Log4j的
- 不会写用递归的1+2+3...+n的算法!请教~~谢谢了~~
- 大姐大哥帮帮小弟
- 还是关于String的问题,为什么两个结果不一样
- 怎样用java实现ftp远程建立文件夹,急等!100分
- 生成执行文件的问题?
- Java源代码打成可执行jar包后连接不上sql2000数据库。在eclipse里能连上。求大神帮助,感谢
- 谁能帮我做个作业?可能是考试题 !!谢谢啦。。
- [求助] 正则表达式加塞字符串
楼主【raymond304】截止到2008-07-04 10:48:08的历史汇总数据(不包括此帖):
发帖的总数量:2 发帖的总分数:40
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:2 未结的总分数:40
结贴的百分比:0.00 % 结分的百分比:0.00 %
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
首先:button大小设置不对,文字显示不全
其次:页面刷新不对,有时候控件显示不出来
还有:public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
int sumMatch = (int) (Math.random() * (50 - 20)) + 20;
tfPcshow.setText("计算机随机取了" + sumMatch + "根火柴");
}
}
sumMatch应该用成员变量吧?不能用局部变量阿,否则,按哪个JRadioButton都会走到出错处理里,因为成员变量sumMatch的值一直为0
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;public class GetMatch extends JFrame implements ActionListener
{
private static final long serialVersionUID = 920921981358099067L; public int sumMatch; private JLabel lbPer, lbCom, lbLast; private JButton btnStart; private JRadioButton rdbOne, rdbTwo, rdbThree; private JTextField tfPcshow, tfLastshow; public GetMatch()
{
super("火柴游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);
setResizable(false);
initUI();
} private void initUI()
{
Container contrainer = getContentPane();
ButtonGroup group = new ButtonGroup();
contrainer.setLayout(null);
lbPer = new JLabel("Person取数");
lbPer.setBounds(20, 25, 100, 25);
lbCom = new JLabel("Computer取数");
lbCom.setBounds(20, 70, 100, 25);
lbLast = new JLabel("剩余火柴数");
lbLast.setBounds(20, 115, 100, 25);
btnStart = new JButton("PC启动");
btnStart.setBounds(310, 70, 100, 25);
rdbOne = new JRadioButton("1");
rdbOne.setBounds(135, 25, 35, 35);
rdbTwo = new JRadioButton("2");
rdbTwo.setBounds(170, 25, 35, 35);
rdbThree = new JRadioButton("3");
rdbThree.setBounds(205, 25, 35, 35);
tfPcshow = new JTextField();
tfPcshow.setBounds(135, 70, 150, 25);
tfLastshow = new JTextField();
tfLastshow.setBounds(135, 115, 150, 25);
btnStart.addActionListener(this);
rdbOne.addActionListener(this);
rdbTwo.addActionListener(this);
rdbThree.addActionListener(this);
group.add(rdbOne);
group.add(rdbTwo);
group.add(rdbThree);
contrainer.add(btnStart);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(lbPer);
contrainer.add(lbCom);
contrainer.add(tfPcshow);
contrainer.add(btnStart);
contrainer.add(lbLast);
contrainer.add(tfLastshow);
} public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnStart)
{
sumMatch = (int)(Math.random() * (50 - 20)) + 20;
tfPcshow.setText("计算机随机取了" + sumMatch + "根火柴");
}
else if(e.getSource() instanceof JRadioButton)
{
int k = Integer.parseInt(((JRadioButton)e.getSource()).getText());;
sumMatch -= k;
if(sumMatch == 0)
{
JOptionPane.showMessageDialog(this, "你赢啦", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
else if(sumMatch < 0)
{
JOptionPane.showMessageDialog(this, "出错啦!重新来过", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
tfPcshow.setText("计算机取了" + Computer() + "根");
tfLastshow.setText("剩余火柴" + sumMatch + "根");
}
} private int Computer()
{
if(sumMatch <= 3 && sumMatch > 0)
{
JOptionPane.showMessageDialog(this, "你输了!", "TEST", JOptionPane.INFORMATION_MESSAGE);
return sumMatch;
}
else if(sumMatch != 0)
{
int a = (int)(Math.random() * (3 - 1)) + 1;
sumMatch -= a;
return sumMatch;
}
return 0;
} public static void main(String[] args)
{
GetMatch frame = new GetMatch();
frame.setVisible(true);
}
}
1、Container contrainer = getContentPane();后面加一行contrainer.setLayout(null);
2、int sumMatch =(int)(Math.random()*(50-20))+20;一行把前面的int 删除修改了以上两处可以保证程序正常运行,不过你写的东东太缺乏优化了
if(e.getSource() instanceof JButton){
int sumMatch =(int)(Math.random()*(50-20))+20;
tfPcshow.setText("计算机随机取了"+sumMatch+"根火柴");
}
} 把int去掉 public int Computer(){
if((sumMatch <=3)&&(sumMatch>0)){
JOptionPane.showMessageDialog(null, "你输了!", "TEST",JOptionPane.INFORMATION_MESSAGE);
return sumMatch;}
else if(sumMatch!=0){
int a =(int)(Math.random()*(3-1))+1;
sumMatch-=a;
return sumMatch;
}
return 0;
}
改为return a;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;public class GetMatch extends JFrame implements ActionListener
{
private static final long serialVersionUID = 920921981358099067L; public int sumMatch; private JLabel lbPer, lbCom, lbLast; private JButton btnStart; private JRadioButton rdbOne, rdbTwo, rdbThree; private JTextField tfPcshow, tfLastshow; private boolean win = true; public GetMatch()
{
super("火柴游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);
setResizable(false);
initUI();
} private void initUI()
{
Container contrainer = getContentPane();
ButtonGroup group = new ButtonGroup();
contrainer.setLayout(null); lbPer = new JLabel("Person取数");
lbPer.setBounds(20, 25, 100, 25);
lbCom = new JLabel("Computer取数");
lbCom.setBounds(20, 70, 100, 25);
lbLast = new JLabel("剩余火柴数");
lbLast.setBounds(20, 115, 100, 25);
btnStart = new JButton("PC启动");
btnStart.setBounds(310, 70, 100, 25);
rdbOne = new JRadioButton("1");
rdbOne.setBounds(135, 25, 35, 35);
rdbTwo = new JRadioButton("2");
rdbTwo.setBounds(170, 25, 35, 35);
rdbThree = new JRadioButton("3");
rdbThree.setBounds(205, 25, 35, 35);
tfPcshow = new JTextField();
tfPcshow.setBounds(135, 70, 150, 25);
tfLastshow = new JTextField();
tfLastshow.setBounds(135, 115, 150, 25); btnStart.addActionListener(this);
rdbOne.addActionListener(this);
rdbTwo.addActionListener(this);
rdbThree.addActionListener(this); group.add(rdbOne);
group.add(rdbTwo);
group.add(rdbThree); contrainer.add(btnStart);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(lbPer);
contrainer.add(lbCom);
contrainer.add(tfPcshow);
contrainer.add(btnStart);
contrainer.add(lbLast);
contrainer.add(tfLastshow);
} public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnStart)
{
sumMatch = (int)(Math.random() * (50 - 20)) + 20;
tfPcshow.setText("计算机随机取了" + sumMatch + "根火柴");
tfLastshow.setText("");
}
else if(e.getSource() instanceof JRadioButton)
{
int k = Integer.parseInt(((JRadioButton)e.getSource()).getText());
;
sumMatch -= k; if(sumMatch == 0)
{
JOptionPane.showMessageDialog(this, "你赢啦", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
else if(sumMatch < 0)
{
JOptionPane.showMessageDialog(this, "出错啦!重新来过", "TEST", JOptionPane.INFORMATION_MESSAGE);
} tfPcshow.setText("计算机取了" + computer() + "根");
tfLastshow.setText("剩余火柴" + sumMatch + "根"); if(!win)
{
JOptionPane.showMessageDialog(this, "你输啦!", "TEST", JOptionPane.INFORMATION_MESSAGE);
win = true;
}
}
} private int computer()
{
int ret = 0; if(sumMatch <= 3 && sumMatch > 0)
{
ret = sumMatch;
sumMatch = 0;
win = false;
}
else if(sumMatch != 0)
{
ret = (int)(Math.random() * (3 - 1)) + 1;
sumMatch -= ret;
} return ret;
} public static void main(String[] args)
{
GetMatch frame = new GetMatch();
frame.setVisible(true);
}
}