求一个Java程序代码要运用图形界面
编写一个“猜数”的程序:该程序随机在1到100的范围内选择一个供用户猜测的整数,然后该程序显示提示信息,要求用户输入一个1到100之间的整数,根据输入的数偏大,偏小,正确...
编写一个“猜数”的程序:该程序随机在1到100的范围内选择一个供用户猜测的整数,然后该程序显示提示信息,要求用户输入一个1到100之间的整数,根据输入的数偏大,偏小,正确,程序将显示不同的图标。
展开
4个回答
展开全部
package gussNumber;
import java.awt.*;
import java.awt.event.*;
public class GussNumber extends Frame{
private static GussNumber mainFrame = new GussNumber();
private static Button yes = new Button("确定输入");
private static Button reTry = new Button("重新测试");
private static Label lb = new Label("请输入你猜测的数字(0~100):");
private static Label ts = new Label("欢迎使用......");
private static Double r_num = new Double(Math.random()*100);
private static Integer num = new Integer(r_num.intValue());
private static int t_num;
private static TextField tf = new TextField("",5);
private static String ct_img = "welcome.jpg";
public void paint(Graphics g){
Image img = getToolkit().getImage(ct_img);
g.drawImage(img,100,30,this);
}
public GussNumber(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main (String[] args) {
yes.addActionListener(new ButtonListener());
reTry.addActionListener(new ButtonListener());
System.out.println("Starting GussNumber...");
mainFrame.setBackground(Color.WHITE);
mainFrame.setSize(400,200);
mainFrame.setLayout(null);
mainFrame.setLocationRelativeTo(null);
mainFrame.setTitle("猜数字");
mainFrame.add(lb);
lb.setBounds(70,150,200,20);
mainFrame.add(tf);
tf.setBounds(270,150,50,20);
mainFrame.add(yes);
yes.setBounds(240,170,60,25);
mainFrame.add(ts);
ts.setBounds(70,130,200,20);
mainFrame.add(reTry);
reTry.setBounds(100,170,60,25);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==yes){
try{
t_num = Integer.parseInt(tf.getText());
}catch(NumberFormatException e){
Graphics g = mainFrame.getGraphics();
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
ct_img = "welcome.jpg";
ts.setText("请输入正常的数字!");
g.drawImage(img,100,30,mainFrame);
}
if(t_num == num){
ct_img = "right.jpg";
ts.setText("答对啦! 恭喜你~");
Image img = mainFrame.getToolkit().getImage("right.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
if(t_num > num){
ct_img = "tooBig.jpg";
ts.setText("大了点哦! 继续努力~");
Image img = mainFrame.getToolkit().getImage("tooBig.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
if(t_num < num){
ct_img = "tooSmall.jpg";
ts.setText("试试换个大点的数字~");
Image img = mainFrame.getToolkit().getImage("tooSmall.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
}
if(event.getSource()==reTry){
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
r_num = new Double(Math.random()*100);
num = new Integer(r_num.intValue());
tf.setText("");
}
}
}
}
记得将图片换成自己的图片;
并且布局修改得美观点。。。。
import java.awt.*;
import java.awt.event.*;
public class GussNumber extends Frame{
private static GussNumber mainFrame = new GussNumber();
private static Button yes = new Button("确定输入");
private static Button reTry = new Button("重新测试");
private static Label lb = new Label("请输入你猜测的数字(0~100):");
private static Label ts = new Label("欢迎使用......");
private static Double r_num = new Double(Math.random()*100);
private static Integer num = new Integer(r_num.intValue());
private static int t_num;
private static TextField tf = new TextField("",5);
private static String ct_img = "welcome.jpg";
public void paint(Graphics g){
Image img = getToolkit().getImage(ct_img);
g.drawImage(img,100,30,this);
}
public GussNumber(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main (String[] args) {
yes.addActionListener(new ButtonListener());
reTry.addActionListener(new ButtonListener());
System.out.println("Starting GussNumber...");
mainFrame.setBackground(Color.WHITE);
mainFrame.setSize(400,200);
mainFrame.setLayout(null);
mainFrame.setLocationRelativeTo(null);
mainFrame.setTitle("猜数字");
mainFrame.add(lb);
lb.setBounds(70,150,200,20);
mainFrame.add(tf);
tf.setBounds(270,150,50,20);
mainFrame.add(yes);
yes.setBounds(240,170,60,25);
mainFrame.add(ts);
ts.setBounds(70,130,200,20);
mainFrame.add(reTry);
reTry.setBounds(100,170,60,25);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==yes){
try{
t_num = Integer.parseInt(tf.getText());
}catch(NumberFormatException e){
Graphics g = mainFrame.getGraphics();
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
ct_img = "welcome.jpg";
ts.setText("请输入正常的数字!");
g.drawImage(img,100,30,mainFrame);
}
if(t_num == num){
ct_img = "right.jpg";
ts.setText("答对啦! 恭喜你~");
Image img = mainFrame.getToolkit().getImage("right.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
if(t_num > num){
ct_img = "tooBig.jpg";
ts.setText("大了点哦! 继续努力~");
Image img = mainFrame.getToolkit().getImage("tooBig.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
if(t_num < num){
ct_img = "tooSmall.jpg";
ts.setText("试试换个大点的数字~");
Image img = mainFrame.getToolkit().getImage("tooSmall.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
}
}
if(event.getSource()==reTry){
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
r_num = new Double(Math.random()*100);
num = new Integer(r_num.intValue());
tf.setText("");
}
}
}
}
记得将图片换成自己的图片;
并且布局修改得美观点。。。。
展开全部
package test;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Guess extends Applet implements ActionListener
{
Panel p1,p2,p3,p4;
Button b1,b2;
Label l1,l2;
TextField t1;
TextArea t2;
int num;
public void init()
{
setSize(230,300);
setBackground(Color.pink);
l1=new Label("这是一个猜数字程序(0-100)",Label.CENTER);
l2=new Label("begin:");
t1=new TextField(10);
t2=new TextArea("",10,30);
b1=new Button("猜测");
b2=new Button("查看数字");
Random rand=new Random();
num=rand.nextInt(100);
add(l1);
add(l2);
add(t1);
add(t2);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent j)
{
int n;
try{
if(j.getSource()==b1)
{
String s;
s=String.valueOf(t1);
if(s.compareTo("1")<0||s.compareTo("9")>0)
t2.setText("输入有误!\n请重新输入!");
n=Integer.parseInt(t1.getText());
if(n<num)
t2.setText("小了,请输入个大点的数字");
else if(n>num)
t2.setText("大了,请输入个小点的数字");
else
{
t2.setText("恭喜你,猜对了");
Random rand1=new Random();
num=rand1.nextInt(100);
}
}
if(j.getSource()==b2)
{
t2.setText(""+num);
}
}catch(Exception e){}
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Guess extends Applet implements ActionListener
{
Panel p1,p2,p3,p4;
Button b1,b2;
Label l1,l2;
TextField t1;
TextArea t2;
int num;
public void init()
{
setSize(230,300);
setBackground(Color.pink);
l1=new Label("这是一个猜数字程序(0-100)",Label.CENTER);
l2=new Label("begin:");
t1=new TextField(10);
t2=new TextArea("",10,30);
b1=new Button("猜测");
b2=new Button("查看数字");
Random rand=new Random();
num=rand.nextInt(100);
add(l1);
add(l2);
add(t1);
add(t2);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent j)
{
int n;
try{
if(j.getSource()==b1)
{
String s;
s=String.valueOf(t1);
if(s.compareTo("1")<0||s.compareTo("9")>0)
t2.setText("输入有误!\n请重新输入!");
n=Integer.parseInt(t1.getText());
if(n<num)
t2.setText("小了,请输入个大点的数字");
else if(n>num)
t2.setText("大了,请输入个小点的数字");
else
{
t2.setText("恭喜你,猜对了");
Random rand1=new Random();
num=rand1.nextInt(100);
}
}
if(j.getSource()==b2)
{
t2.setText(""+num);
}
}catch(Exception e){}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么没有main()函数啊
这怎么运行啊
这怎么运行啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2008-06-10
展开全部
有init方法了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询