-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartFrame.java
More file actions
145 lines (121 loc) · 5.65 KB
/
Copy pathStartFrame.java
File metadata and controls
145 lines (121 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartFrame
{ public static int score = 0;
public static void main(String[] agrs)
{
//美化界面
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch(Exception e) {
System.out.println(e);
}
//
//加载图片
ImageIcon icon=new ImageIcon("04.png");
//Image im=new Image(icon);
//将图片放入label中
JLabel label=new JLabel(icon);
//设置label的大小
label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
JFrame frame=new JFrame("开始界面"); //创建Frame窗口
JPanel p1=new JPanel(); //面板1
JPanel p2=new JPanel(); //面板2
//获取窗口的第二层,将label放入
frame.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
//获取frame的顶层容器,并设置为透明
JPanel j=(JPanel)frame.getContentPane();
j.setOpaque(false);
JLabel label1=new JLabel("人机or人人:");
label1.setFont(new Font("楷体",Font.BOLD,20)); p2.add(label1);
JRadioButton rb1=new JRadioButton("人机"); JRadioButton rb2=new JRadioButton("人人");
ButtonGroup group1=new ButtonGroup(); group1.add(rb1); group1.add(rb2); p2.add(rb1); p2.add(rb2);
JLabel label2=new JLabel("选择棋盘大小:");
label2.setFont(new Font("楷体",Font.BOLD,20)); p2.add(label2);
JRadioButton rb3=new JRadioButton("13*13"); JRadioButton rb4=new JRadioButton("15*15");
ButtonGroup group2=new ButtonGroup(); group2.add(rb3); group2.add(rb4); p2.add(rb3); p2.add(rb4);
p1.setOpaque(false); p2.setOpaque(false); frame.setSize(icon.getIconWidth(),icon.getIconHeight());
JPanel cards=new JPanel(new CardLayout());//卡片式布局的面板
JButton a1 = new JButton("开始"); JButton a2 = new JButton("退出"); JButton a3 = new JButton("开始游戏");
p1.add(a1); p1.add(a2); p2.add(a3);
cards.add(p1,"card1"); //向卡片式布局面板中添加面板1
cards.add(p2,"card2"); //向卡片式布局面板中添加面板2
p1.setLayout(null); a1.setBounds(95,130,150,50); a2.setBounds(305,130,150,50);//设置按钮大小与位置
p2.setLayout(null); label1.setBounds(80,50,150,50); rb1.setBounds(250,50,50,50); rb2.setBounds(370,50,50,50);//rb3.setBounds(250,100,50,50); rb4.setBounds(350,100,50,50);
label2.setBounds(80,100,150,50); rb3.setBounds(250,100,80,50); rb4.setBounds(370,100,80,50);
a3.setBounds(185,200,180,50);
CardLayout cl=(CardLayout)(cards.getLayout());
cl.show(cards,"card1"); //调用show()方法显示面板2
frame.add(cards);
frame.setBounds(600,400,550,400);
frame.setVisible(true); cards.setOpaque(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(cards,"card2");//enter card2
}
});
a2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(1);//exit the game
}
});
rb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
score = score + 1;
}
});
rb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
score = score + 2;
}
});
rb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
score = score + 3;
}
});
rb4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
score = score + 5;
}
});
a3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (score){
case 4://人机13
System.out.println("人机13"); frame.setVisible(false);
break;
case 6://人机15
System.out.println("人机15"); frame.setVisible(false);
break;
case 5://人人13
System.out.println("人人13"); frame.setVisible(false);
break;
case 7://人人15
new Thread(new Runnable() {
@Override
public void run() {
new aGame(15).play();
}
}).start();
frame.setVisible(false);
}
}
});
}
}