java 猜數字遊戲

來源:互聯網
上載者:User

標籤:

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

           

                 

 

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() {
@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);
}
});
// 開始監聽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);
}
});
// 退出監聽
out.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);

}
});
// 協助監聽
help.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "請按開始進行遊戲!就是猜數字而已,就不多說了!",
"協助", JOptionPane.PLAIN_MESSAGE);
}
});
// 關於監聽
about.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "謝謝嘗臉試玩!", "3Q",
JOptionPane.WARNING_MESSAGE);
}
});
// 開始監聽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的數字!");
}
}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() {

@Override
public void actionPerformed(ActionEvent e) {
text.setText("");
}
});

}

@Override
public void actionPerformed(ActionEvent e) {
}
}

java 猜數字遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.