㈠ java課程設計源代碼(急!!!!)
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class game21 extends JFrame {
private JLabel label_2;
private int number;
private int sum;
final JLabel label = new JLabel();
final JLabel label_1 = new JLabel();
public static void main(String[] args) {
new game21();
}
public game21() {
super("21點?!");
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
onClick();
}
});
button.setText("出牌");
button.setBounds(170, 350, 106, 28);
getContentPane().add(button);
label.setBorder(new LineBorder(Color.black, 1, false));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("", Font.BOLD, 26));
label.setText("背面");
label.setBounds(158, 81, 137, 153);
getContentPane().add(label);
label_1.setText("你已經擁有的牌:");
label_1.setBounds(109, 22, 270, 45);
getContentPane().add(label_1);
this.setBounds(200, 300, 501, 528);
this.setVisible(true);
getContentPane().add(getLabel_2());
}
public int randNumber() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return (int) (Math.random() * 10 + 1);
}
public void onClick() {
number = this.randNumber();
this.sum += number;
label.setText("" + number);
String strTemp = this.label_1.getText();
strTemp += "" + number + " ";
label_1.setText(strTemp);
String temp = "合計:" + sum;
label_2.setText(temp);
isWin();
}
public void isWin() {
if (sum > 21) {
JOptionPane.showMessageDialog(this, "你輸了");
clear();
return;
} else if (sum == 21) {
JOptionPane.showMessageDialog(this, "你贏了");
clear();
return;
} else {
int i = JOptionPane.showOptionDialog(this, "是否繼續?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}
private void clear() {
label_2.setText("合計:");
sum = 0;
number = 0;
label_1.setText("你已經擁有的牌:");
}
/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合計:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}
}
真好無聊中。。
㈡ 需要用Java編一個模擬21點紙牌游戲程序
rock [英] [rɒk] [美] [rɑk] n.岩石; 岩; 巨石; 礫石; 卵石; 強壯的人; 堅如磐石的事物; 可靠的人; 冷酷無情的人; 強硬的事物; 錢財; 搖滾樂; 輕輕搖動; 輕輕搖擺; 輕輕搖晃 v.輕輕搖動; 使輕輕擺動; 使輕輕晃動; 跳搖滾舞; 奏搖滾樂; 穿; 炫耀地表現出; 很好; 劇烈震動 [例句] Many types of rock are ejected from volcanoes as solid, fragmentary material. 多種岩石以固體碎屑形式從火山中噴發出來。 [變形] 過去分詞:rocked 現在分詞:rocking 過去式:rocked 第三人稱單數:rocks
㈢ 用Java做21點游戲
就這么點分、還匿名、見得人的東西!~