這篇文章主要講解了java實現(xiàn)簡單猜數(shù)字的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
為浉河等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及浉河網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、成都做網(wǎng)站、浉河網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
代碼不多說,圖片自加,實現(xiàn)功能有數(shù)字比大小,菜單開始,幫助,退出,關(guān)于等,運行結(jié)果如圖:
代碼:
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("關(guān)于(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("王氏猜數(shù)字游戲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位數(shù)"); 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() { @Override public 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); } }); // 開始監(jiān)聽1 btnStart.addActionListener(new ActionListener() { @Override public 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); } }); // 退出監(jiān)聽 out.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(false); } }); // 幫助監(jiān)聽 help.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "請按開始進(jìn)行游戲!就是猜數(shù)字而已,就不多說了!", "幫助", JOptionPane.PLAIN_MESSAGE); } }); // 關(guān)于監(jiān)聽 about.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "謝謝嘗臉試玩!", "3Q", JOptionPane.WARNING_MESSAGE); } }); // 開始監(jiān)聽2 caiButton.addActionListener(new ActionListener() { @Override public 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的數(shù)字!"); } }else if(num == y){ mes.setText(" 恭喜你猜對了哦!"); } else{ mes.setText(" 你輸入的" + num + "小了哦!"); if (num < 1) { mes.setText(" 請輸入大于0的數(shù)字!"); } } } catch (Exception e2) { mes.setText(" 請輸入0-99的數(shù)字!"); } } }); // 重置監(jiān)聽 congButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { text.setText(""); } }); } @Override public void actionPerformed(ActionEvent e) { } }
看完上述內(nèi)容,是不是對java實現(xiàn)簡單猜數(shù)字的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁題目:java實現(xiàn)簡單猜數(shù)字的方法
分享URL:http://m.newbst.com/article36/jhsjpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、商城網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站改版、靜態(tài)網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)