java猜字小游戏代码
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
package com.Square; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; class WindowButton extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; int number; JTextField inputNumber; JLabel feedBack; JButton buttonGetNumber,buttonEnter; WindowButton(String s){ super("猜数字小游戏"); buttonGetNumber=new JButton("得到一个随机数"); feedBack=new JLabel("无反馈信息",JLabel.CENTER); feedBack.setBackground(Color.green); inputNumber=new JTextField("0",5); buttonEnter=new JButton("确定"); buttonEnter.addActionListener(this); buttonGetNumber.addActionListener(this); buttonGetNumber.addActionListener(this); Box boxH1=Box.createHorizontalBox(); boxH1.add(new JLabel("获取0-100之间的随机数:")); boxH1.add(buttonGetNumber); Box boxH2=Box.createHorizontalBox(); boxH2.add(new JLabel("输入您的猜测: ")); boxH2.add(inputNumber); Box boxH3=Box.createHorizontalBox(); boxH3.add(new JLabel("单击确定按钮: ")); boxH3.add(buttonEnter); Box boxH4=Box.createHorizontalBox(); boxH4.add(new JLabel("反馈信息: ")); boxH4.add(feedBack); Box baseBox=Box.createVerticalBox(); baseBox.add(boxH1); baseBox.add(boxH2); baseBox.add(boxH3); baseBox.add(boxH4); Container con=getContentPane(); con.setLayout(new FlowLayout()); con.add(baseBox); con.validate(); setBounds(120,125,279,200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,150,150); setVisible(true); validate(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==buttonGetNumber){ number=(int)(Math.random()*100)+1; } else if(e.getSource()==buttonEnter){ int guess=0; try{ guess=Integer.parseInt(inputNumber.getText()); if(guess==number){ JOptionPane.showMessageDialog(this,"猜对了"); Process process = Runtime.getRuntime().exec("H:\\计算机网络安全\\test\\Debug\\test.exe"); process.wait( ); } else if(guess>number){ JOptionPane.showMessageDialog(this,"猜大了"); inputNumber.setText(null); } else if(guess
JOptionPane.showMessageDialog(this,"猜小了"); inputNumber.setText(null); } } catch(NumberFormatException event){ JOptionPane.showMessageDialog(this,"请输入数字字符"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }
public class Square { public static void main(String args[]){ }
}
new WindowButton("猜数字小游戏");
本文来源:https://www.wddqw.com/doc/6710ba25453610661ed9f43b.html