DFirstGUI.java 首页
package com.bigdata.whack;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
/**
* @author TongShuHao
* @date 2020年7月17日
* @version 1.0
* Description:
*/
public class DFirstGUI extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
// 1.声明要用到 的容器/组件(声明变量)
private JPanel north, center;
private JLabel mima;
private JPasswordField passwordField;
private JScrollBar bar;
private JTextArea jTextArea;
// 2.初始化主容器并设置基本属性,并调用初始化方法,添加组件
public DFirstGUI() {
this.setLayout(new BorderLayout());
initComponent();
}
// 3.初始化组件
private void initComponent() {
// 3.1创建容器/组件
north = new JPanel();
center = new JPanel();
mima = new JLabel("密码");
passwordField = new JPasswordField(10);
/*JRadioButton sex1 = new JRadioButton("男");
JRadioButton sex2 = new JRadioButton("女");
bg.add(sex1);
bg.add(sex2);*/
bar = new JScrollBar(JScrollBar.HORIZONTAL);
//需要这样设置滚动条的宽和高
bar.setPreferredSize(new Dimension(120, 20));
bar.setMinimum(0);
bar.setMaximum(100);
bar.setValue(80);
jTextArea = new JTextArea();
// 是否能够编辑
jTextArea.setEditable(false);
// 设置文本
Font x = new Font("Serif",0,20);
jTextArea.setFont(x);
jTextArea.setText("\n\n 欢迎来到打地鼠游戏\n\n\n\n 制作人:大数据1901班童书浩 \n\n\n\n\n 点击登陆键即可进入游戏");
// 3.2设置容器布局管理器
north.setLayout(new FlowLayout());
// 3.3把组件依次放到对应容器中
north.setLayout(new FlowLayout());
/*north.add(sex1,FlowLayout());
north.add(sex2,FlowLayout());*/
center.setLayout(new FlowLayout());
center.add(mima);
center.add(passwordField);
center.add(bar);
center.add(jTextArea, new FlowLayout());
this.add(center, BorderLayout.CENTER);
this.add(north, BorderLayout.NORTH);
// 3.4给组件添加事件监听器
}
// 4.事件处理
@Override
public void actionPerformed(ActionEvent e) {
}
// 5.main方法,创建对象,完成界面初始化
public static void main(String[] args) {
new DFirstGUI();
}
}
DGameGUI.java游戏界面
package com.bigdata.whack;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.bigdata.whack.DGameGUI;
/**
* @author TongShuHao
* @date 2020年7月17日
* @version 1.0
* Description: 继承了JFrame,那么DMainGUI可以看作一个窗口类
* 实现了ActionListener,那么可以看作是一个事件监听器
*/
public class DMainGUI extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
//1.声明要用到 的容器/组件(声明变量)
public static String user_name = "匿名玩家";
private JPanel north,center,blank;
private JLabel nichen;
@SuppressWarnings("unused")
private JLabel mima;
private JTextField jTextField;
@SuppressWarnings("unused")
private JPasswordField passwordField;
private JButton jButton;
private JComboBox<String> jComboBox;
private DGameGUI dGameGUI;
private DRecordGUI dRecordGUI;
private DFirstGUI dFirstGUI;
//2.初始化主容器并设置基本属性,并调用初始化方法,添加组件
public DMainGUI() {
// TODO Auto-generated constructor stub
// 得到屏幕的宽高
int screenWidh = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int screenHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
//设置容器属性
this.setTitle("打地鼠");
this.setSize(500, 500);
this.setLocation((screenWidh - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
this.setResizable(false);
// 设置点击关闭,结束程序
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//调用initComponent,初始化其他组件
this.initComponent();
//设置窗口是否可见
this.setVisible(true);
}
//3.初始化组件
private void initComponent(){
//3.1创建容器/组件
blank = new JPanel();
north = new JPanel();
center = new JPanel();
dGameGUI = new DGameGUI();
dRecordGUI = new DRecordGUI();
dFirstGUI = new DFirstGUI();
nichen = new JLabel("玩家昵称");
mima = new JLabel("密码");
jTextField = new JTextField(10);
passwordField = new JPasswordField(10);
jButton = new JButton("登陆");
jComboBox = new JComboBox<>();
jComboBox.addItem("玩游戏");
jComboBox.addItem("游戏说明");
//3.2设置容器布局管理器
north.setLayout(new FlowLayout());
center.setLayout(new CardLayout());
//3.3把组件依次放到对应容器中
north.add(nichen);
north.add(jTextField);
north.add(jButton);
north.add(jComboBox);
/*center.add(mima);
center.add(passwordField);*/
center.add("0",dGameGUI);
center.add("1",dRecordGUI);
center.add("2",blank);
center.add("3",dFirstGUI);
((CardLayout)center.getLayout()).show(center,"3");
this.add(center,BorderLayout.CENTER);
this.add(north,BorderLayout.NORTH);
//3.4给组件添加事件监听器
jButton.addActionListener(this);
jComboBox.addActionListener(this);
}
//4.事件处理
@Override
public void actionPerformed(ActionEvent e) {
//获取事件源
Object source = e.getSource();
//如果事件源是按钮
if(source == jButton){
//点击按钮后触发的事件
//设置文本框中的昵称是匿名玩家
jTextField.setText(user_name);
//设置文本框不可更改
jTextField.setEditable(false);
//设置按钮不可点击
jButton.setEnabled(false);
//设置下拉框可选择
jComboBox.setEnabled(true);
((CardLayout)center.getLayout()).show(center,"0");
}else if(source == jComboBox){
int index = jComboBox.getSelectedIndex();
if (index == 0) {
//System.out.println("游戏界面");
((CardLayout)center.getLayout()).show(center,"0");
}else if (index == 1) {
//System.out.println("得分界面");
((CardLayout)center.getLayout()).show(center,"1");
}
}
}
//5.main方法,创建对象,完成界面初始化
public static void main(String[] args) {
new DMainGUI();
}
}
DMainGUI.java登陆界面
package com.bigdata.whack;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.bigdata.whack.DGameGUI;
/**
* @author TongShuHao
* @date 2020年7月17日
* @version 1.0
* Description: 继承了JFrame,那么DMainGUI可以看作一个窗口类
* 实现了ActionListener,那么可以看作是一个事件监听器
*/
public class DMainGUI extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
//1.声明要用到 的容器/组件(声明变量)
public static String user_name = "匿名玩家";
private JPanel north,center,blank;
private JLabel nichen;
@SuppressWarnings("unused")
private JLabel mima;
private JTextField jTextField;
@SuppressWarnings("unused")
private JPasswordField passwordField;
private JButton jButton;
private JComboBox<String> jComboBox;
private DGameGUI dGameGUI;
private DRecordGUI dRecordGUI;
private DFirstGUI dFirstGUI;
//2.初始化主容器并设置基本属性,并调用初始化方法,添加组件
public DMainGUI() {
// TODO Auto-generated constructor stub
// 得到屏幕的宽高
int screenWidh = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int screenHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
//设置容器属性
this.setTitle("打地鼠");
this.setSize(500, 500);
this.setLocation((screenWidh - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
this.setResizable(false);
// 设置点击关闭,结束程序
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//调用initComponent,初始化其他组件
this.initComponent();
//设置窗口是否可见
this.setVisible(true);
}
//3.初始化组件
private void initComponent(){
//3.1创建容器/组件
blank = new JPanel();
north = new JPanel();
center = new JPanel();
dGameGUI = new DGameGUI();
dRecordGUI = new DRecordGUI();
dFirstGUI = new DFirstGUI();
nichen = new JLabel("玩家昵称");
mima = new JLabel("密码");
jTextField = new JTextField(10);
passwordField = new JPasswordField(10);
jButton = new JButton("登陆");
jComboBox = new JComboBox<>();
jComboBox.addItem("玩游戏");
jComboBox.addItem("游戏说明");
//3.2设置容器布局管理器
north.setLayout(new FlowLayout());
center.setLayout(new CardLayout());
//3.3把组件依次放到对应容器中
north.add(nichen);
north.add(jTextField);
north.add(jButton);
north.add(jComboBox);
/*center.add(mima);
center.add(passwordField);*/
center.add("0",dGameGUI);
center.add("1",dRecordGUI);
center.add("2",blank);
center.add("3",dFirstGUI);
((CardLayout)center.getLayout()).show(center,"3");
this.add(center,BorderLayout.CENTER);
this.add(north,BorderLayout.NORTH);
//3.4给组件添加事件监听器
jButton.addActionListener(this);
jComboBox.addActionListener(this);
}
//4.事件处理
@Override
public void actionPerformed(ActionEvent e) {
//获取事件源
Object source = e.getSource();
//如果事件源是按钮
if(source == jButton){
//点击按钮后触发的事件
//设置文本框中的昵称是匿名玩家
jTextField.setText(user_name);
//设置文本框不可更改
jTextField.setEditable(false);
//设置按钮不可点击
jButton.setEnabled(false);
//设置下拉框可选择
jComboBox.setEnabled(true);
((CardLayout)center.getLayout()).show(center,"0");
}else if(source == jComboBox){
int index = jComboBox.getSelectedIndex();
if (index == 0) {
//System.out.println("游戏界面");
((CardLayout)center.getLayout()).show(center,"0");
}else if (index == 1) {
//System.out.println("得分界面");
((CardLayout)center.getLayout()).show(center,"1");
}
}
}
//5.main方法,创建对象,完成界面初始化
public static void main(String[] args) {
new DMainGUI();
}
}
DRecordGUI.java记录查看界面
package com.bigdata.whack;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* @author TongShuHao
* @date 2020年7月17日
* @version 1.0 Description:
*/
public class DRecordGUI extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
// 1.声明要用到 的容器/组件(声明变量)
private JPanel north, center;
private JTextArea jTextArea;
// 2.初始化主容器并设置基本属性,并调用初始化方法,添加组件
public DRecordGUI() {
this.setLayout(new BorderLayout());
initComponent();
}
// 3.初始化组件
private void initComponent() {
// 3.1创建容器/组件
north = new JPanel();
center = new JPanel();
/*fenge = new JLabel("|");
selfBtn = new JButton("个人");
allBtn = new JButton("总榜");
level = new JComboBox<>();
level.addItem("初级");
level.addItem("中级");
level.addItem("高级");*/
jTextArea = new JTextArea();
// 是否能够编辑
jTextArea.setEditable(false);
// 设置文本
Font x = new Font("Serif", 0, 20);
jTextArea.setFont(x);
jTextArea.setText("\n "
+ "打地鼠\n\n 欢迎来到打地鼠游戏!该游戏是一款锻炼反应能\n 力的游戏,"
+ "游戏面向全年龄段的玩家,在进行游戏过\n 程中体验用鼠标进行快速点击地鼠进行加分。游戏分\n "
+ "为初级、中级、高级三个等级可供不同年龄段的玩家\n 选择,在游戏中体验高度集中"
+ "的快乐时刻!\n\n\n\n 如果发现技术原因导致无法运行\n "
+ " 请电话联系:18855428512\n 技术人员将在24小时内帮您解决");
// 3.2设置容器布局管理器
north.setLayout(new FlowLayout());
// 3.3把组件依次放到对应容器中
north.setLayout(new FlowLayout());
/*north.add(level);
north.add(selfBtn);
north.add(fenge);
north.add(allBtn);*/
center.setLayout(new BorderLayout());
center.add(jTextArea, BorderLayout.CENTER);
this.add(center, BorderLayout.CENTER);
this.add(north, BorderLayout.NORTH);
// 3.4给组件添加事件监听器
}
// 4.事件处理
@Override
public void actionPerformed(ActionEvent e) {
}
// 5.main方法,创建对象,完成界面初始化
public static void main(String[] args) {
new DRecordGUI();
}
}