※ 此程式碼並非優化版本,僅供個人學習用途,若有任何問題請告知。
※ 準備3個jpg圖檔,分別是:scissors.jpg, stone.jpg, paper.jpg 程式碼如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.FlowLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.ImageIcon; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
/* paper, scissors, stone */ | |
class result { | |
int computer() { | |
int ran = (int)(Math.random()*10000+0); | |
if(ran > 6000) {return 1;} | |
else if(ran <= 6000 && ran >= 1500) {return 2;} | |
else {return 3;} | |
} | |
} | |
public class paper_scissors_stone extends JFrame { | |
public static void main(String[] args) { | |
JFrame jf = new JFrame("猜拳猜猜猜"); | |
JPanel content = new JPanel(); | |
JButton btn1, btn2, btn3, btn4; //1_scissors 2_stone 3_paper | |
result r = new result(); | |
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); | |
jf.setBounds(200, 200, 900, 300); | |
jf.setContentPane(content); | |
content.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50)); | |
ImageIcon img1 = new ImageIcon("src//A//1.jpg"); | |
btn1 = new JButton(img1); //scissors | |
btn1.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
if(r.computer() == 1) {JOptionPane.showMessageDialog(null, "恭喜你,你贏了!\n你出:剪刀\n電腦出:布", "結果", JOptionPane.WARNING_MESSAGE);} | |
else if(r.computer() == 2) {JOptionPane.showMessageDialog(null, "真可惜,你輸了!\n你出:剪刀\n電腦出:石頭", "結果", JOptionPane.WARNING_MESSAGE);} | |
else {JOptionPane.showMessageDialog(null, "平手!\n你出:剪刀\n電腦出:剪刀", "結果", JOptionPane.WARNING_MESSAGE);} | |
} | |
}); | |
content.add(btn1); | |
ImageIcon img2 = new ImageIcon("src//A//2.jpg"); | |
btn2 = new JButton(img2); //stone | |
btn2.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
if(r.computer() == 1) {JOptionPane.showMessageDialog(null, "恭喜你,你贏了!\n你出:石頭\n電腦出:剪刀", "結果", JOptionPane.WARNING_MESSAGE);} | |
else if(r.computer() == 2) {JOptionPane.showMessageDialog(null, "真可惜,你輸了!\n你出:石頭\n電腦出:布", "結果", JOptionPane.WARNING_MESSAGE);} | |
else {JOptionPane.showMessageDialog(null, "平手!\n你出:石頭\n電腦出:石頭", "結果", JOptionPane.WARNING_MESSAGE);} | |
} | |
}); | |
content.add(btn2); | |
ImageIcon img3 = new ImageIcon("src//A//3.jpg"); | |
btn3 = new JButton(img3); //paper | |
btn3.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
if(r.computer() == 1) {JOptionPane.showMessageDialog(null, "恭喜你,你贏了!\n你出:布\n電腦出:石頭", "結果", JOptionPane.WARNING_MESSAGE);} | |
else if(r.computer() == 2) {JOptionPane.showMessageDialog(null, "真可惜,你輸了!\n你出:布\n電腦出:剪刀", "結果", JOptionPane.WARNING_MESSAGE);} | |
else {JOptionPane.showMessageDialog(null, "平手!\n你出:布\n電腦出:布", "結果", JOptionPane.WARNING_MESSAGE);} | |
} | |
}); | |
content.add(btn3); | |
btn4 = new JButton("test button"); | |
btn4.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
JFrame jf2 = new JFrame("test"); | |
//jf2.setDefaultCloseOperation(jf2.EXIT_ON_CLOSE); | |
jf2.setBounds(600, 300, 200, 150); | |
jf2.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50)); | |
jf2.setVisible(true); | |
} | |
}); | |
content.add(btn4); | |
jf.setVisible(true); | |
} | |
} |
結果如下:
沒有留言:
張貼留言