界面图:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main {
public static void main(String[] args) {
madoFrame();
}
private static void madoFrame(){
// Frame是带有标题和边框的顶级窗口
Frame f = new Frame("猜拳游戏");
Label p1 = new Label("石头");
Label p2 = new Label("石头");
p1.setLocation(100,50);
p2.setLocation(150,50);
Label str = new Label();
str.setLocation(100,25);
Button b = new Button("开始");
b.setSize(10,30);
b.setLocation(100,0);
//将组件添加到Frame中
f.add(p1);
f.add(p2);
f.add(str);
f.add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int count = 0;count < 10;count++){
Fist fist = new Fist();
String n1 = fist.getP1();
String n2 = fist.getP2();
p1.setText(n1);
p2.setText(n2);
if (n1.equals(n2)){
str.setText("不相上下");
}else {
if (("剪刀").equals(n1) && ("石头").equals(n2)){
str.setText("阿廖赢了");
}else if(("剪刀").equals(n1) && ("布").equals(n2)){
str.setText("猫苘赢了");
}else if(("石头").equals(n1) && ("布").equals(n2)){
str.setText("阿廖赢了");
}else if(("石头").equals(n1) && ("剪刀").equals(n2)){
str.setText("猫苘赢了");
}else if(("布").equals(n1) && ("剪刀").equals(n2)){
str.setText("阿廖赢了");
}else if(("布").equals(n1) && ("石头").equals(n2)){
str.setText("猫苘赢了");
}
}
}
}
});
f.setLayout(new java.awt.GridLayout());
//设置框架大小
f.setSize(500, 100);
center(f);
//显示窗体
f.setVisible(true);
f.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private static void center(Component c) {
Toolkit kit = Toolkit.getDefaultToolkit();
int x = (kit.getScreenSize().width - c.getWidth()) / 2;
int y = (kit.getScreenSize().height - c.getHeight()) / 2;
c.setLocation(x, y);
}
}
但是关闭不了,只能参考他人的三个方法java中关闭Frame窗体的方法
改成这样子:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main extends Frame{
public void display(){
this.setTitle("猜拳游戏");
Label p1 = new Label("石头");
Label p2 = new Label("石头");
p1.setLocation(100,50);
p2.setLocation(150,50);
Label str = new Label();
str.setLocation(100,25);
Button b = new Button("开始");
b.setSize(10,30);
b.setLocation(100,0);
//将组件添加到Frame中
this.add(p1);
this.add(p2);
this.add(str);
this.add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int count = 0;count < 10;count++){
Fist fist = new Fist();
String n1 = fist.getP1();
String n2 = fist.getP2();
p1.setText(n1);
p2.setText(n2);
if (n1.equals(n2)){
str.setText("不相上下");
}else {
if (("剪刀").equals(n1) && ("石头").equals(n2)){
str.setText("阿廖赢了");
}else if(("剪刀").equals(n1) && ("布").equals(n2)){
str.setText("猫苘赢了");
}else if(("石头").equals(n1) && ("布").equals(n2)){
str.setText("阿廖赢了");
}else if(("石头").equals(n1) && ("剪刀").equals(n2)){
str.setText("猫苘赢了");
}else if(("布").equals(n1) && ("剪刀").equals(n2)){
str.setText("阿廖赢了");
}else if(("布").equals(n1) && ("石头").equals(n2)){
str.setText("猫苘赢了");
}
}
}
}
});
this.setLayout(new java.awt.GridLayout());
center(this);
//设置框架大小
this.setSize(500, 100);
//显示窗体
this.setVisible(true);
}
public static void main (String args[]){
Main f = new Main();
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.display();
}
private static void center(Component c) {
Toolkit kit = Toolkit.getDefaultToolkit();
int x = (kit.getScreenSize().width - c.getWidth()) / 2;
int y = (kit.getScreenSize().height - c.getHeight()) / 2;
c.setLocation(x, y);
}
}