Number Kingdom 笔记【Java】
1.引入的常用包
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
2.主程序部分
public class N
{
public static void main(String[] args)
{
Work();
}
}
3.时间/倒计时
java.util.Timer timer=new java.util.Timer();
MyTime timetask=new MyTime();
public class MyTime extends TimerTask
{
public void run()
{
sec--;
Work();
if(sec==0)Over();
}
}
timer.schedule(timetask,1000,1000);
//timer.schedule(timetask,首次延时,间隔时间);
4.JFrame声明
JFrame table=new JFrame("标题");//JFrame参数可为空
void Set_Table()
{
table.setSize(1000,618);//table.setSize(宽度,高度);
table.setVisible(true);//组件可见
table.setLayout(null);//自定义布局
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//正常关闭退出
}
//或table.setLayout(new FlowLayout());即流动布局(默认)
5.JLabel声明
JLabel say=new JLabel("文本内容");//参数可为空
say.setBounds(300,10,200,20);//say.setBounds(横向位置,纵向位置,宽度,高度);
say.setForeground(Color.green);//设置字体颜色
say.setFont(new Font(null,1,15));//设置字体格式 Font(字体,是否加粗,字号)
table.add(say);//加入组件
say.setText("新内容");//重设文本内容
//位置以左上角为原点,以像素为单位
JLabel内嵌HTML语法
//JLabel say21=new JLabel("
//<html>
// 规则:游戏共五关,通关有惊喜。<br>
//    每关需在1~200中选择符合题意的数字输入。<br>
//    输入数字的正误将会显示,每关正确数达到3个时过关。<br>
//    数字错误一次将损失一点生命值,生命值为0时游戏失败。<br>
// 提示:游戏中单击可获得提示。<br>
// 获得提示次数:单击后输入兑换码可获得提示次数。<br>
//");
6.JButton声明
JButton button=new JButton("内容");
button.setBounds(430,10,150,20);
table.add(button);
7.JTextArea声明
JTextArea answer=new JTextArea(1,20);//JTextArea(行数,长度)
table.add(answer);
ans=answer.getText();//获取文本框中文本
8.数组声明
int line[]=new int[205];
int snum[]={0,1,2,3};
9.按钮事件
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Work();
}
});
10.版权声明模板
void About()
{
JFrame table14=new JFrame("关于作者");
JLabel say19=new JLabel("联系作者:");
JLabel say18=new JLabel("<html>QQ:997577114<br>E-mail:wfzhangzeyu@163.com");
JLabel say20=new JLabel("All Rights Reserved By MC");
table14.setSize(400,200);
table14.setVisible(true);
table14.setLayout(null);
say19.setFont(new Font(null,1,12));
say19.setBounds(70,15,100,40);
say18.setFont(new Font(null,0,12));
say18.setBounds(155,20,200,40);
say20.setBounds(100,70,200,40);
say20.setFont(new Font(null,1,14));
table14.add(say18);
table14.add(say19);
table14.add(say20);
}