色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

java實現簡單猜數字

瀏覽:97日期:2022-09-01 15:22:00

本文實例為大家分享了java實現簡單猜數字的具體代碼,供大家參考,具體內容如下

代碼不多說,圖片自加,實現功能有數字比大小,菜單開始,幫助,退出,關于等,運行結果如圖:

java實現簡單猜數字

java實現簡單猜數字

java實現簡單猜數字

java實現簡單猜數字

java實現簡單猜數字

代碼:

import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Bingo extends JFrame implements ActionListener {JFrame frame;JMenuBar topBbr;JMenu menuQ;JMenu menuF;JMenuItem start = new JMenuItem('開始(n)');JMenuItem out = new JMenuItem('不玩了(e)');JMenuItem help = new JMenuItem('幫助(h)');JMenuItem about = new JMenuItem('關于(a)');JPanel panelMain = new JPanel(new BorderLayout());JPanel panelNext = new JPanel(new BorderLayout());JButton btnStart;JPanel jp1, jp2, jp3, jp4;public static void main(String[] args) {new Bingo();}public Bingo() {frame = new JFrame();topBbr = new JMenuBar();frame.setTitle('王氏猜數字游戲1.0版');frame.setSize(400, 320);frame.setLocation(450, 240);frame.setLayout(getLayout());// frame.setLayout(new FlowLayout());menuQ = new JMenu('游戲(q)');menuQ.add(start);menuQ.add(out);menuF = new JMenu('幫助(f)');ImageIcon icon = new ImageIcon('imgs/logo.jpg');btnStart = new JButton(icon);JButton caiButton = new JButton('猜一猜');JButton congButton = new JButton('重新來');JTextField text = new JTextField(6);JLabel mes = new JLabel(' 請你輸入一個2位數');menuF.add(help);menuF.add(about);topBbr.add(menuQ);topBbr.add(menuF);frame.setJMenuBar(topBbr);panelMain.add('Center', btnStart);frame.add(panelMain);frame.setResizable(false);frame.setVisible(true);// Image im = new ImageIcon('imgs/logo.jpg').getImage();// im = im.getScaledInstance(-1, 90, DO_NOTHING_ON_CLOSE);// setIconImage(im);start.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {panelMain.setVisible(false);start.setEnabled(false);frame.add(panelNext);jp1 = new JPanel();jp2 = new JPanel(new GridLayout(2, 1));jp3 = new JPanel();JLabel top = new JLabel('??');// top.setBackground(new Color(100, 100, 0, 100));// top.setOpaque(true);JLabel jb = new JLabel(' ');JLabel jb2 = new JLabel(' ');top.setFont(new Font('隸體', Font.BOLD, 100));top.setForeground(Color.yellow);jp1.add(top);jp2.add(mes);jp2.add(text);jp3.add(caiButton);jp3.add(congButton);panelNext.add('North', jp1);panelNext.add('Center', jp2);panelNext.add('West', jb);panelNext.add('East', jb2);panelNext.add('South', jp3);panelNext.setVisible(true);}});// 開始監聽1btnStart.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {start.setEnabled(false);panelMain.setVisible(false);frame.add(panelNext);jp1 = new JPanel();jp2 = new JPanel(new GridLayout(2, 1));jp3 = new JPanel();JLabel top = new JLabel('??');// top.setBackground(new Color(100, 100, 0, 100));// top.setOpaque(true);JLabel jb = new JLabel(' ');JLabel jb2 = new JLabel(' ');top.setFont(new Font('隸體', Font.BOLD, 100));top.setForeground(Color.yellow);jp1.add(top);jp2.add(mes);jp2.add(text);jp3.add(caiButton);jp3.add(congButton);panelNext.add('North', jp1);panelNext.add('Center', jp2);panelNext.add('West', jb);panelNext.add('East', jb2);panelNext.add('South', jp3);panelNext.setVisible(true);}});// 退出監聽out.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {frame.setVisible(false);}});// 幫助監聽help.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(null, '請按開始進行游戲!就是猜數字而已,就不多說了!','幫助', JOptionPane.PLAIN_MESSAGE);}});// 關于監聽about.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(frame, '謝謝嘗臉試玩!', '3Q',JOptionPane.WARNING_MESSAGE);}});// 開始監聽2caiButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Random x = new Random();int y = x.nextInt(100);try {int num = Integer.parseInt(text.getText());if (num > y) {mes.setText(' 你輸入的' + num + '大了哦!');if (num > 99) {mes.setText(' 請輸入小于99的數字!');}}else if(num == y){mes.setText(' 恭喜你猜對了哦!');} else{mes.setText(' 你輸入的' + num + '小了哦!');if (num < 1) {mes.setText(' 請輸入大于0的數字!');}} } catch (Exception e2) {mes.setText(' 請輸入0-99的數字!');}}});// 重置監聽congButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {text.setText('');}});}@Overridepublic void actionPerformed(ActionEvent e) {}}

更多有趣的經典小游戲實現專題,也分享給大家:

C++經典小游戲匯總

python經典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經典游戲 玩不停

java經典小游戲匯總

javascript經典小游戲匯總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲欧洲久久久精品 | 国产精品一区伦免视频播放 | 国产精品日韩专区 | 国产精品久久久久久久 | 日本在线观看免费看片 | 欧美在线观看一区二区三区 | 国产亚洲综合成人91精品 | 韩国一级淫片视频免费播放 | aaaaaa级特色特黄的毛片 | 久久久久视频精品网 | 性感美女视频黄.免费网站 性高湖久久久久久久久 | 亚洲在线天堂 | 免费人成在线 | 精品久久久久久乐 | 91久久青青草原线免费 | 一级做a爰性色毛片 | 亚洲欧美日本人成在线观看 | 免费观看欧美性一级 | 免费黄色一级网站 | 成人午夜视频一区二区国语 | 欧美成人26uuu欧美毛片 | 亚洲综合精品成人 | 免费观看三级毛片 | 一级做a爰片毛片 | 欧美大尺度aaa级毛片 | 中文字幕福利片 | 精品欧美一区二区三区四区 | 日本视频播放免费线上观看 | 亚洲国产影视 | 欧美日韩国产成人精品 | 中文乱码一二三四有限公司 | 国产成人免费在线 | 久久亚洲国产午夜精品理论片 | 亚洲精品xxxx | 国产成人综合日韩精品婷婷九月 | 久久依人| 国产17部性孕妇孕交在线 | 欧美私人网站 | 国产成年女一区二区三区 | 成人毛片视频免费网站观看 | 婷婷国产成人久久精品激情 |