m0_59759126 2021-07-04 01:02 采纳率: 100%
浏览 29
已采纳

需要个添加历史记录的代码

package guess;
import java.awt.;
import java.awt.event.
;
import javax.swing.*;

public class guess {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    DrawFrame frame=new DrawFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}

class DrawFrame extends JFrame
{
private static int count=0;//猜测次数
private int rightNumber;//随机产生的供猜测的数字
private JTextField num;//输入的数字
private JLabel tip;//猜测次数提示
private JPanel panel;//中心输入框部分面板
private JLabel result;//猜测结果提示
private JButton button0=new JButton("开始");
private JButton button1=new JButton("确认");
private JButton button2=new JButton("重新开始");
private JButton button3=new JButton("退出");
private JButton button4=new JButton("玩法");
private JButton button5=new JButton("提示");

public DrawFrame()
{
    setBounds(700,200,450,200);
    setTitle("一个猜数游戏");
    rightNumber=(int)(Math.random()*40+10);
    
    //添加顶部次数提示
    tip=new JLabel("你已经猜了"+count+"次",JLabel.LEFT);
    
    //添加中心输入框部分
    panel = new JPanel();
    JLabel input=new JLabel("输入猜测的数");
    panel.add(input);
    num=new JTextField(20);
    panel.add(num);
    result=new JLabel();//显示猜测结果
    panel.add(result);
    
    //添加按钮
    JPanel buttons= new JPanel();
    ActionListener listener1=new ComfirmListener();
    button1.addActionListener(listener1);
    ActionListener listener2=new OtherListener2();
    button2.addActionListener(listener2);
    button3.addActionListener(listener2);
    button4.addActionListener(listener2);
    button5.addActionListener(listener2);
    
    //将按钮添加到panel中
    buttons.add(button0);
    buttons.add(button1);
    buttons.add(button2);
    buttons.add(button3);
    buttons.add(button4);
    buttons.add(button5);
    
    add(tip,"North");
    add(panel,"Center");
    add(buttons,"South");
}

//确认按钮的监听器类
class ComfirmListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        //猜测正确
        if(Integer.parseInt(num.getText())==rightNumber)
        {
            //设置文本框不可编辑
            num.setEditable(false);
            
            //提示猜测次数+1
            tip.setText("你已经猜了"+(++count)+"次");
            
            //调整背景色为默认背景色
            Color defaultColor = getBackground();
            panel.setBackground(defaultColor);
            
            //显示猜测结果
            panel.setBackground(Color.green);
            result.setText("恭喜你猜对啦!!");
        }
        //小于
        else if(Integer.parseInt(num.getText())<rightNumber)
        {
            panel.setBackground(Color.red);
            tip.setText("你已经猜了"+(++count)+"次");
            result.setText("此次结果为:太小");
        }
        else
        {
            panel.setBackground(Color.red);
            tip.setText("你已经猜了"+(++count)+"次");
            result.setText("此次结果为:太大");
        }
    }
}


//重新开始和退出按钮以及帮助的监听器
class OtherListener2 implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        //重新开始
        if(event.getSource()==button2)
        {
            //清除文本框内容
            num.setText("");
            
            //重新抽数字
            rightNumber=(int)(Math.random()*40+10);
                    
            //调整背景色为默认背景色
            Color defaultColor = getBackground();
            panel.setBackground(defaultColor);
            
            //猜测次数增加
            count=0;
            tip.setText("你已经猜了"+count+"次");
            
            //清除原猜测结果
            result.setText("");
            
        }
        else if(event.getSource()==button4) {
            JOptionPane.showMessageDialog(null, "玩法:请从10~50中用最少的次数猜一个随机的数字。祝你好运!");
            
        }
        else if(event.getSource()==button5) {
            JOptionPane.showMessageDialog(null,"提示:其一种解法用到高中确定区间零点的常用方法。");
            JOptionPane.showMessageDialog(null,"提示:其最优解是华罗庚发表的0.618优选法。");
        }
        
        //退出则关闭窗口
        else
        {
            setVisible(false);
        }
        
    }
    

}

}

  • 写回答

2条回答 默认 最新

  • 404警告 2021-07-04 01:24
    关注
    package test;
    
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    
    public class guess {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            DrawFrame frame = new DrawFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setBounds(100, 100, 784, 438);
            frame.setVisible(true);
        }
    }
    
    class DrawFrame extends JFrame {
        private static int count = 0;// 猜测次数
        private int rightNumber;// 随机产生的供猜测的数字
        private JTextField num;// 输入的数字
        private JLabel tip;// 猜测次数提示
        private JPanel panel;// 中心输入框部分面板
        private JLabel result;// 猜测结果提示
        private JButton button0 = new JButton("开始");
        private JButton button1 = new JButton("确认");
        private JButton button2 = new JButton("重新开始");
        private JButton button3 = new JButton("退出");
        private JButton button4 = new JButton("玩法");
        private JButton button5 = new JButton("提示");
        private JButton button6 = new JButton("历史记录");
        private List<String> countNum = new ArrayList<String>(); // 存储历史记录
    
        public DrawFrame() {
            setBounds(700, 200, 450, 200);
            setTitle("一个猜数游戏");
            rightNumber = (int) (Math.random() * 40 + 10);
    
            // 添加顶部次数提示
            tip = new JLabel("你已经猜了" + count + "次", JLabel.LEFT);
    
            // 添加中心输入框部分
            panel = new JPanel();
            JLabel input = new JLabel("输入猜测的数");
            panel.add(input);
            num = new JTextField(20);
            panel.add(num);
            result = new JLabel();// 显示猜测结果
            panel.add(result);
    
            // 添加按钮
            JPanel buttons = new JPanel();
            ActionListener listener1 = new ComfirmListener();
            button1.addActionListener(listener1);
            ActionListener listener2 = new OtherListener2();
            button2.addActionListener(listener2);
            button3.addActionListener(listener2);
            button4.addActionListener(listener2);
            button5.addActionListener(listener2);
    
            // 将按钮添加到panel中
            buttons.add(button0);
            buttons.add(button1);
            buttons.add(button2);
            buttons.add(button3);
            buttons.add(button4);
            buttons.add(button5);
            buttons.add(button6);
    
            add(tip, "North");
            add(panel, "Center");
            add(buttons, "South");
            button6.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    new popupWindows();
                }
            });
        }
    
        class popupWindows {
            JFrame mainWindow;
    
            public popupWindows() {
                mainWindow = new JFrame("历史记录");
    
                mainWindow.setVisible(true);
                mainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainWindow.setBounds(100, 100, 784, 438);
                mainWindow.getContentPane().setLayout(null);
                // 窗口显示在页面中间
                mainWindow.setLocationRelativeTo(null);
                // 窗口不允许调整大小
                mainWindow.setResizable(false);
    
                JScrollPane scrollPane = new JScrollPane();
                scrollPane.setBounds(14, 96, 738, 282);
                mainWindow.getContentPane().add(scrollPane);
    
                JTable table = new JTable();
                String[] str = new String[] { "序号", "猜测数" }; // 创建列数组
                // 构建二维数组存放信息
                Object[][] objects = new Object[countNum.size()][2];
    
                // 添加数据
                for (int i = 0; i < countNum.size(); i++) {
    
                    objects[i][0] = "第" + (i + 1) + "次";
                    objects[i][1] = "猜测数是: " + countNum.get(i);
                }
                table.setModel(new DefaultTableModel(objects, str));
                scrollPane.setViewportView(table);
                JLabel label = new JLabel("猜测正确数:" + (countNum.size() == 0 ? 0 : countNum.get(0)) + "次");
                label.setForeground(Color.DARK_GRAY);
                label.setBounds(333, 20, 136, 18);
                mainWindow.getContentPane().add(label);
    
            }
        }
    
        // 确认按钮的监听器类
        class ComfirmListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                if (countNum.size() == 0) {
                    countNum.add("0");
                }
                // 猜测正确
                if (Integer.parseInt(num.getText()) == rightNumber) {
                    int yesNum = Integer.parseInt(countNum.get(0));
                    countNum.set(0, ++yesNum + "");
                    // 设置文本框不可编辑
                    num.setEditable(false);
    
                    // 提示猜测次数+1
                    tip.setText("你已经猜了" + (++count) + "次");
    
                    // 调整背景色为默认背景色
                    Color defaultColor = getBackground();
                    panel.setBackground(defaultColor);
    
                    // 显示猜测结果
                    panel.setBackground(Color.green);
                    result.setText("恭喜你猜对啦!!");
                }
                // 小于
                else if (Integer.parseInt(num.getText()) < rightNumber) {
                    panel.setBackground(Color.red);
                    tip.setText("你已经猜了" + (++count) + "次");
                    result.setText("此次结果为:太小");
                } else {
                    panel.setBackground(Color.red);
                    tip.setText("你已经猜了" + (++count) + "次");
                    result.setText("此次结果为:太大");
                }
                countNum.add(num.getText());
            }
        }
    
        // 重新开始和退出按钮以及帮助的监听器
        class OtherListener2 implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                // 重新开始
                if (event.getSource() == button2) {
                    // 清除文本框内容
                    num.setText("");
    
                    // 重新抽数字
                    rightNumber = (int) (Math.random() * 40 + 10);
    
                    // 调整背景色为默认背景色
                    Color defaultColor = getBackground();
                    panel.setBackground(defaultColor);
    
                    // 猜测次数增加
                    count = 0;
                    tip.setText("你已经猜了" + count + "次");
    
                    // 清除原猜测结果
                    result.setText("");
                    // 清空
                    countNum.clear();
    
                } else if (event.getSource() == button4) {
                    JOptionPane.showMessageDialog(null, "玩法:请从10~50中用最少的次数猜一个随机的数字。祝你好运!");
    
                } else if (event.getSource() == button5) {
                    JOptionPane.showMessageDialog(null, "提示:其一种解法用到高中确定区间零点的常用方法。");
                    JOptionPane.showMessageDialog(null, "提示:其最优解是华罗庚发表的0.618优选法。");
                }
    
                // 退出则关闭窗口
                else {
                    setVisible(false);
                }
    
            }
    
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA