java编程 设计如下图所示的窗口,完成猜数小游戏的设计。
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
展开全部
界面不是完全一致,但是可行。
package ex1;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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(s);
buttonGetNumber = new JButton("得到一个新的随机数");
feedBack = new JLabel("无反馈信息",JLabel.CENTER);
feedBack.setBackground(Color.red);
inputNumber = new JTextField("0",5);
buttonEnter = new JButton("确定");
buttonEnter.addActionListener(WindowButton.this);
//code 1:按钮buttonEnter增加ActionEvent事件监视器,监视器为当前窗口
buttonGetNumber.addActionListener(WindowButton.this);
//code 2:按钮buttonGetNumber增加ActionEvent事件监视器,监视器为当前窗口
Box boxH1 = Box.createHorizontalBox();
boxH1.add(new JLabel("获取1-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(230,225,400,200);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonGetNumber) //code 3:判断事件是否是buttonGetNumber
number = (int)(Math.random()*100)+1;
else if(e.getSource() == buttonEnter) //code 4:判断事件是否是buttonEnter
{
int guess = 0;
try{
guess = Integer.parseInt(inputNumber.getText());
//code 5:返回inputNumber中的文本
if(guess == number)
{
feedBack.setText("猜对了~");
//code 6:feedBack设置提示文本:"猜对了"
}
else if(guess > number)
{
feedBack.setText("猜大了~");
inputNumber.setText(null);
}
else if(guess < number)
{
feedBack.setText("猜小了~");
inputNumber.setText(null);
}
}
catch(NumberFormatException event)
{
feedBack.setText("请输入数字字符");
//code 7:feedBack设置提示文本:"请输入数字字符"
}
}
}
}
public class GuessNumberGame {
public static void main(String[] args) {
new WindowButton("猜数子小游戏");
}
}
package ex1;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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(s);
buttonGetNumber = new JButton("得到一个新的随机数");
feedBack = new JLabel("无反馈信息",JLabel.CENTER);
feedBack.setBackground(Color.red);
inputNumber = new JTextField("0",5);
buttonEnter = new JButton("确定");
buttonEnter.addActionListener(WindowButton.this);
//code 1:按钮buttonEnter增加ActionEvent事件监视器,监视器为当前窗口
buttonGetNumber.addActionListener(WindowButton.this);
//code 2:按钮buttonGetNumber增加ActionEvent事件监视器,监视器为当前窗口
Box boxH1 = Box.createHorizontalBox();
boxH1.add(new JLabel("获取1-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(230,225,400,200);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonGetNumber) //code 3:判断事件是否是buttonGetNumber
number = (int)(Math.random()*100)+1;
else if(e.getSource() == buttonEnter) //code 4:判断事件是否是buttonEnter
{
int guess = 0;
try{
guess = Integer.parseInt(inputNumber.getText());
//code 5:返回inputNumber中的文本
if(guess == number)
{
feedBack.setText("猜对了~");
//code 6:feedBack设置提示文本:"猜对了"
}
else if(guess > number)
{
feedBack.setText("猜大了~");
inputNumber.setText(null);
}
else if(guess < number)
{
feedBack.setText("猜小了~");
inputNumber.setText(null);
}
}
catch(NumberFormatException event)
{
feedBack.setText("请输入数字字符");
//code 7:feedBack设置提示文本:"请输入数字字符"
}
}
}
}
public class GuessNumberGame {
public static void main(String[] args) {
new WindowButton("猜数子小游戏");
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询