麦高芬� 2018-12-04 15:09 采纳率: 0%
浏览 3437

Java如何通过匿名内部类给外部变量赋值

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.lang.Math;
public class GuessWindow extends JFrame //implements ActionListener
{

GuessWindow()
{ 
  JFrame win=new JFrame("猜数字小游戏"); 
  setLocation(200, 200);
    setSize(250, 250); 
    win.validate();
  win.setVisible(true);
  GridLayout grid = new GridLayout(2,2,25,15);
  JPanel p = new JPanel(grid);
  p.add(new JLabel("获取[1,10)之间的随机数:"));
  JButton button =new JButton("得到一个随机数");
  p.add(button);
  p.add(new JLabel("请输入您的猜测:"));
  JTextField guess = new JTextField(20);
  p.add(guess);
  Box bb,p1,p2;
  bb=Box.createVerticalBox();
  p1=Box.createHorizontalBox();
  bb.add(p);
  p1.add(new JLabel("单击确定按钮"));
  p1.add(Box.createHorizontalStrut(10));
  JButton queding =new JButton("确定");
  p1.add(queding);
  bb.add(p1);
  win.add(bb,BorderLayout.CENTER);
  JLabel result =new JLabel("无反馈信息");
  result.setForeground(Color.blue);
  win.add(result,BorderLayout.SOUTH);
  int da=0;
  int guessa=0;
  String a = null;
  button.addActionListener(new ActionListener()
                  {
                  public void actionPerformed(ActionEvent e)
                     { 
                         int da=0;
                         da=(int)(1+Math.random()*(10-1));
                     }
                  });
 queding.addActionListener(new ActionListener()
                  {
                  public void actionPerformed(ActionEvent e)
                     { 
                         int guessa=0;
                         String a = null;
                         a=guess.getText();
                         guessa=a.charAt(0);
                         if (guessa == da)
                         {

                             JLabel resultr=new JLabel("猜测正确");
                             resultr.setForeground(Color.blue);
                             win.add(resultr,BorderLayout.SOUTH);
                         }else if(guessa<da)
                         {
                             JLabel resultx=new JLabel("猜小了");
                             resultx.setForeground(Color.blue);
                             win.add(resultx,BorderLayout.SOUTH);
                         }else
                         {
                             JLabel resultd=new JLabel("猜大了");
                             resultd.setForeground(Color.blue);
                             win.add(resultd,BorderLayout.SOUTH);
                         };
                     }
                  });
  win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
  public static void main(String[] args)
 {
new GuessWindow();
 }

}

  • 写回答

3条回答 默认 最新

  • 麦高芬� 2018-12-04 15:26
    关注

    就是要实现一个猜数小游戏,通过点击第一个按钮获得随机数,然后输入一个数字后点击确定,用输入的数字和产生的随机数进行比较

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧