1 package com.lovo.hilo;
2
3 import java.awt.Container;
4 import java.awt.GridLayout;
5 import java.awt.Toolkit;
6
7 import javax.swing.JFrame;
8
9 public class HiLoFrame extends JFrame {
10
11 private Container contentP;
12
13 private UpPanel upP;
14
15 private MidPanel mdP;
16
17 private DownPanel dnP;
18
19 private int randomNum;
20
21 public HiLoFrame() {
22 this.randomNum = (int)(Math.random() * 50 + 50);
23 System.out.println(this.randomNum);
24 // 工具箱類--ToolKit
25 Toolkit tk = Toolkit.getDefaultToolkit();
26 // 設置窗體大小
27 this.setSize(320, 290);
28 // 設置窗體位置
29 int screenW = (int) tk.getScreenSize().getWidth();// 得到屏幕寬
30 int screenH = (int) tk.getScreenSize().getHeight();// 得到屏幕高
31 this.setLocation((screenW - 320) / 2, (screenH - 290) / 2);
32 // 設置窗體大小不可更改
33 this.setResizable(true);
34 // 設置窗體圖標
35 this.setIconImage(tk.createImage("img/hp.JPG"));
36 // 設置窗體的標題
37 this.setTitle("HiLo游戲");
38 // 設置窗體關閉即為關閉程序
39 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40 // 設置窗體內容面板上所有的東西
41 this.addContent();
42 // 設置窗體可見
43 this.setVisible(true);
44 }
45
46
47
48 private void addContent() {
49 // TODO Auto-generated method stub
50 this.contentP = this.getContentPane();
51 this.contentP.setLayout(new GridLayout(3, 1));
52
53 this.upP = new UpPanel();
54 this.mdP = new MidPanel();
55 this.dnP = new DownPanel(this);
56
57 this.contentP.add(this.upP);
58 this.contentP.add(this.mdP);
59 this.contentP.add(this.dnP);
60 }
61
62 public UpPanel getUpP() {
63 return upP;
64 }
65
66 public MidPanel getMdP() {
67 return mdP;
68 }
69
70 public DownPanel getDnP() {
71 return dnP;
72 }
73
74 public int getRandomNum() {
75 return randomNum;
76 }
77
78 public void setRandomNum(int randomNum) {
79 this.randomNum = randomNum;
80 }
81
82 }
內部的功能
1 package com.lovo.hilo;
2
3 import java.awt.Color;
4
5 import javax.swing.BorderFactory;
6 import javax.swing.JPanel;
7 import javax.swing.JTextField;
8
9 //JPanel的默認布局管理是流布局
10 public class UpPanel extends JPanel{
11
12 private JTextField inputTxt;
13
14 public UpPanel(){
15 this.setBackground(Color.WHITE);
16 this.setBorder(BorderFactory.createTitledBorder("Your Guess"));
17
18 this.setLayout(null);
19 this.inputTxt = new JTextField();
20 this.inputTxt.setBounds(90, 33, 140,25);
21 this.add(this.inputTxt);
22 }
23
24 public JTextField getInputTxt() {
25 return inputTxt;
26 }
27
28 public void setInputTxt(JTextField inputTxt) {
29 this.inputTxt = inputTxt;
30 }
31 }
1 package com.lovo.hilo;
2
3 import java.awt.Color;
4 import java.awt.FlowLayout;
5 import java.awt.Font;
6
7 import javax.swing.BorderFactory;
8 import javax.swing.JLabel;
9 import javax.swing.JPanel;
10
11 public class MidPanel extends JPanel {
12
13 private JLabel msgLab;
14
15 public MidPanel(){
16 this.setBackground(Color.WHITE);
17 this.setLayout(new FlowLayout());
18 this.setBorder(BorderFactory.createTitledBorder("Hint"));
19
20 this.msgLab = new JLabel("Let's Play HiLo");
21 this.msgLab.setFont(new Font("微軟雅黑",Font.PLAIN,15));
22 this.add(this.msgLab);
23 }
24
25 public JLabel getMsgLab() {
26 return msgLab;
27 }
28
29 public void setMsgLab(JLabel msgLab) {
30 this.msgLab = msgLab;
31 }
32
33 }
1 package com.lovo.hilo;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5
6 import javax.swing.JPanel;
7
8 public class DownPanel extends JPanel {
9
10 private ButtonPanel btnP;
11
12 public DownPanel(HiLoFrame frame){
13 this.setBackground(Color.WHITE);
14 this.setLayout(new BorderLayout());
15
16 this.btnP = new ButtonPanel(frame);
17 this.add(this.btnP,BorderLayout.SOUTH);
18 }
19
20 public ButtonPanel getBtnP() {
21 return btnP;
22 }
23
24 public void setBtnP(ButtonPanel btnP) {
25 this.btnP = btnP;
26 }
27
28 }
1 package com.lovo.hilo;
2
3 import java.awt.Button;
4 import java.awt.Color;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.JButton;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12
13 public class ButtonPanel extends JPanel {
14
15 private JButton enterBtn;
16
17 private JButton replayBtn;
18
19 private int i;
20
21 private HiLoFrame frame;
22
23 //匿名內部類中不能訪問外部類的局部變量,只能訪問全局變量或常量
24 //這里的形參是一個欺騙編譯器的動作
25 public ButtonPanel( HiLoFrame frame){
26 this.frame=frame;
27 this.setBackground(Color.WHITE);
28 this.setLayout(new FlowLayout());
29
30 this.enterBtn = new JButton("確定");
31 this.replayBtn = new JButton("重玩");
32 this.replayBtn.setEnabled(false);
33 this.enterBtn.setEnabled(true);
34 this.enterBtn.addActionListener(new ActionListener() {
35 @Override
36 public void actionPerformed(ActionEvent e) {
37 // TODO Auto-generated method stub
38 //1、取得輸入的整數
39 String inputStr = ButtonPanel.this.frame.getUpP().getInputTxt().getText();
40 JLabel msgLab =ButtonPanel.this. frame.getMdP().getMsgLab();
41 //2、取得隨機數
42 int randomNum =ButtonPanel.this. frame.getRandomNum();
43 String msg = "";
44 if(inputStr.matches("[5-9][0-9]")){
45 int inputNum = Integer.parseInt(inputStr);
46 //3、比較
47 if(inputNum > randomNum){
48 msg = "不好意思,你猜大了!";
49 msgLab.setText(msg+"你還有"+(6-i)+"次機會");
50 }else if(inputNum < randomNum){
51 msg = "不好意思,你猜小了!";
52 msgLab.setText(msg+"你還有"+(6-i)+"次機會");
53 }else{
54 msg = "恭喜你猜對了!";
55 msgLab.setText(msg);
56 ButtonPanel.this.replayBtn.setEnabled(true);
57 ButtonPanel.this.enterBtn.setEnabled(false);
58 i=0;
59 }
60
61 }else{
62 msg = "請輸入50到99的有效數字!";
63 msgLab.setText(msg+"你還有"+(7-i)+"次機會");
64 }
65 i++;
66
67 if(i==7){
68 ButtonPanel.this.replayBtn.setEnabled(true);
69 ButtonPanel.this.enterBtn.setEnabled(false);
70 i=0;
71 }
72 }
73 });
74
75 this.replayBtn.addActionListener(new ActionListener(){
76
77 @Override
78 public void actionPerformed(ActionEvent e) {
79 // TODO Auto-generated method stub
80 ButtonPanel.this.frame.setRandomNum((int)(Math.random() * 50 + 50));
81 ButtonPanel.this.replayBtn.setEnabled(false);
82 ButtonPanel.this.enterBtn.setEnabled(true);
83 i=0;
84 System.out.println(ButtonPanel.this.frame.getRandomNum());
85 }
86
87 });
88 this.add(this.enterBtn);
89 this.add(this.replayBtn);
90
91 }
92
93 }
1 package com.lovo.test;
2
3
4 import com.lovo.cardlayout.CardFrame;
5 import com.lovo.event.ColorFrame;
6 import com.lovo.frame.BorderFrame;
7 import com.lovo.frame.FlowFrame;
8 import com.lovo.frame.GridFrame;
9 import com.lovo.frame.MyFrame;
10 import com.lovo.hilo.HiLoFrame;
11 import com.lovo.qq.QQFrame;
12
13 public class TestMain {
14
15 public static void main(String[] args) {
16 // TODO Auto-generated method stub
17
18 new HiLoFrame();
19
20 }
21
22 }