import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;
import javax.swing.*;
public class youxi extends JFrame implements ActionListener {
JFrame frame = new JFrame("Hangman Game");
JLabel userLabel = new JLabel("HANGMAN");
JButton loginButton1 = new JButton("play game");
JButton loginButton2 = new JButton("view instructions");
JButton loginButton3 = new JButton("exit");
public youxi(){
frame.setSize(400, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.setVisible(true);
panel.setLayout(null);
userLabel.setBounds(110,5,180,35);
userLabel.setFont(new Font("",0,30));
panel.add(userLabel);
loginButton1.setBounds(110,50,150,30);
panel.add(loginButton1);
loginButton2.setBounds(110,90,150,30);
panel.add(loginButton2);
loginButton3.setBounds(110,130,150,30);
panel.add(loginButton3);
loginButton1.addActionListener(this);
loginButton2.addActionListener(this);
loginButton3.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==loginButton1){
chuangkou a=new chuangkou();
}
if(e.getSource()==loginButton2)
{
JOptionPane.showMessageDialog(this,
"游戏说明:有这些国家可选择\nJAPAN\nQATAR\nSYRIA\nMONGOLIA\nBAHARAININDIA\nPAKISTAN\nAUSTRALIA\nCHINA");}
{
}
if(e.getSource()==loginButton3){
System.exit(0);
}
}
public static void main(String[] args){
youxi a=new youxi();
}
}
package HangmanGame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;
import javax.swing.*;
public class chuangkou extends JFrame implements ActionListener{
JButton a[]=new JButton[27];
JFrame frame = new JFrame("Game");
JPanel panel = new JPanel();
JLabel userLabel = new JLabel("GUESS A WORD",JLabel.CENTER);
JLabel userLabel2 = new JLabel("WAIT FOR PALY",JLabel.CENTER);
int time=0;
int time2=0;
String b[]={"Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M","play"};
String b1="QWERTYUIOPASDFGHJKLZXCVBNM";
char[] b2=b1.toCharArray();
String word1[]= {"JAPAN","QATAR","SYRIA","MONGOLIA","BAHARAIN","INDIA","PAKISTAN","AUSTRALIA","CHINA"};
int t;
int x;
Random r=new Random();
int n1=r.nextInt(9);
int n2=word1[n1].length();
JLabel c[]=new JLabel[n2];
JLabel d[]=new JLabel[n2];
char[] word2=word1[n1].toCharArray();
public chuangkou(){
for(int i=0;i<n2;i++){
c[i]=new JLabel("__ ");
c[i].setText("__ ");
}
frame.setSize(800, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.setVisible(true);
panel.setLayout(null);
userLabel.setBounds(0,0,784,35);
userLabel.setFont(new Font("",0,20));
// userLabel.setBackground(Color.gray);
userLabel.setOpaque(true);
userLabel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(userLabel);
userLabel2.setBackground(Color.white);
userLabel2.setOpaque(true);
userLabel2.setBounds(0,35,784,350);
userLabel2.setFont(new Font("",0,35));
panel.add(userLabel2);
panel.setBackground(Color.black);
for(int i=0;i<=26;i++){
a[i]=new JButton(b[i]);
a[i].setFont(new Font("",0,20));
}
for(int i=0;i<=9;i++){
a[i].setBounds(40+70*i, 400, 60, 30);
panel.add(a[i]);
}
for(int i=10;i<=18;i++){
a[i].setBounds(70+70*(i-10), 480, 60, 30);
panel.add(a[i]);
}
for(int i=19;i<=26;i++){
a[i].setBounds(100+70*(i-19), 560, 60, 30);
panel.add(a[i]);
}
a[26].setBounds(100+70*7, 560, 80, 30);
for(int i=0;i<=26;i++) {a[i].addActionListener(this);}
}
public void actionPerformed(ActionEvent e) {
if(a[26]==e.getSource())
for(int i=0;i<n2;i++){
userLabel2.setText("");
c[i].setBackground(Color.white);
c[i].setOpaque(true);
c[i].setText("__ ");
c[i].setBounds(200+60*i,200,50,50);
c[i].setFont(new Font("",0,35));
panel.add(c[i]);
}
for(int i=0;i<26;i++){
if(a[i]==e.getSource()){
a[i].setBackground(Color.red);
x=time;
for(int j=0;j<n2;j++){
if(b2[i]==word2[j]){
a[i].setBackground(Color.green);
c[j].setText(b2[i]+" ");
time++;
}
}
if(x==time){
time2++;
}
if(time==n2){JOptionPane.showMessageDialog(this,"你赢了!");dispose();}
if(time2==5){JOptionPane.showMessageDialog(this,"你失败了!");dispose();}
}
}
}
}