📄 example.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class Threa extends Thread //计时线程
{ int slp=0;
JTextField l;
Font f=new Font("System",Font.BOLD,23);
public Threa(JTextField t)
{ l=t;
}
public void run()
{
try{
while(slp>=0)
{
slp=slp+1;
if(slp>=0&&slp<=9) l.setText(" 00"+slp);
if(slp>=10&&slp<=99) l.setText(" 0"+slp);
if(slp>=100&&slp<=999) l.setText(" "+slp);
l.setFont(f);
l.setForeground(Color.RED);
l.validate();sleep(1000);
}
}
catch(InterruptedException e)
{}
}
}
class MyDialog extends JDialog implements ActionListener, ChangeListener //雷区自由设定对话框
{ static final int YES=1,NO=0,CLOSE=-1;
int message=10;
Button yes,no;
Label l1,l2,l3;
JSlider js1,js2,js3;
JTextField t1,t2,t3,t;
MyDialog(JFrame f,String s,boolean b)
{
super(f,s,b);
Container con=getContentPane();
con.setLayout(null);
yes=new Button("确定");
no=new Button("取消");
l1=new Label("牧场的宽度");
l2=new Label("牧场的长度");
l3=new Label("逃跑的猪数目");
js1=new JSlider(9,100,9);
js2=new JSlider(9,100,9);
js3=new JSlider(0,400,10);
js1.setMajorTickSpacing(10);
js2.setMajorTickSpacing(10);
js3.setMajorTickSpacing(40);
js1.setPaintTicks(true);
js1.setPaintLabels(true);
js2.setPaintTicks(true);
js2.setPaintLabels(true);
js3.setPaintTicks(true);
js3.setPaintLabels(true);
t=new JTextField();
t1=new JTextField(""+js1.getValue());
t2=new JTextField(""+js2.getValue());
t3=new JTextField(""+js3.getValue());
con.add(l1);
con.add(js1);
con.add(t1);
con.add(l2);
con.add(js2);
con.add(t2);
con.add(l3);
con.add(js3);
con.add(t3);
con.add(t);
con.add(yes);
con.add(no);
js1.addChangeListener(this);
js2.addChangeListener(this);
js3.addChangeListener(this);
yes.addActionListener(this);
no.addActionListener(this);
setBounds(100,100,400,200);
l1.setBounds(10,10,80,40);
l2.setBounds(10,50,80,40);
l3.setBounds(10,90,80,40);
js1.setBounds(90,10,250,40);
js2.setBounds(90,50,250,40);
js3.setBounds(90,90,250,40);
t1.setBounds(340,10,40,30);
t2.setBounds(340,50,40,30);
t3.setBounds(340,90,40,30);
yes.setBounds(270,130,50,30);
no.setBounds(330,130,50,30);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{ message=CLOSE;
setVisible(false); }
}
);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{
message=YES;
setVisible(false);
}
if(e.getSource()==no)
{
message=NO;
setVisible(false);
}
}
public void stateChanged(ChangeEvent e) //滑块监视器
{
if(e.getSource()==js1)
{ t1.setText(""+js1.getValue());
}
else if(e.getSource()==js2)
{ t2.setText(""+js2.getValue());
}
else if(e.getSource()==js3)
{ t3.setText(""+js3.getValue());
}
}
public int getMessage()
{
return message;
}
}
class MyDia1 extends JDialog implements ActionListener // 剧情对话框
{
int message=10;
JButton yes;
Icon icon;
MyDia1(JFrame f,String s,boolean b)
{
super(f,s,b);
Container con=getContentPane();
con.setLayout(null);
icon=new ImageIcon("gg.jpg");
yes=new JButton("确定",icon);
con.add(yes);
yes.addActionListener(this);
setBounds(100,100,280,290);
yes.setBounds(0,0,279,289);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{ setVisible(false);
}
}
public int getMessage()
{
return message;
}
}
class MyDia2 extends JDialog implements ActionListener // 挑战成功对话框
{
static final int YES=1;
int message=10;
JButton yes;
Icon icon;
JTextField xingming;
JLabel 姓名,时间,shijian;
File file;
int shi[]=new int[3];int Dengji;
MyDia2(JFrame f,String s,boolean b,int dengji)
{
super(f,s,b);
Container con=getContentPane();
con.setLayout(null);
icon=new ImageIcon("g.jpg");
yes=new JButton(icon);
con.add(yes);
yes.addActionListener(this);
yes.setBounds(0,0,331,304);
setBounds(100,100,331,304);
xingming=new JTextField("匿名");
shijian=new JLabel();
姓名=new JLabel(" 姓名");
时间=new JLabel(" 时间");
Dengji=dengji;
if(Dengji>0) //挑战成功。英雄榜写入
{
con.add(xingming);
con.add(shijian);
con.add(姓名);
con.add(时间);
xingming.setBounds(70,305,50,20);
shijian.setBounds(170,305,50,20);
姓名.setBounds(20,305,50,20);
时间.setBounds(120,305,50,20);
setBounds(100,100,331,355);
}
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{ message=YES;
try{
file=new File("Bang.txt");
if(file.exists()==false) { // 文件建立
file.createNewFile();
FileWriter out=new FileWriter(file);
out.write("999;匿名;999;匿名;999;匿名");
out.close();
}
if(file.exists()==true)
{ FileReader in=new FileReader(file);
BufferedReader buff=new BufferedReader(in);
String buf=buff.readLine();
String[] rest=buf.split(";",-2);
in.close();
double shi=Double.parseDouble(shijian.getText()); // 挑战者时间
int tiao=(int)shi;
int sj=Integer.parseInt(rest[2*(Dengji-1)]); //记录时间
if(tiao<sj)
{
rest[2*(Dengji-1)]=new String(""+tiao);rest[2*Dengji-1]=new String(xingming.getText()); }
file.delete();
file.createNewFile();
FileWriter out1=new FileWriter(file);
for(int i=0;i<6;i++)
{ out1.write(rest[i]);out1.write(";");}
out1.close();
}
}catch(IOException ioe){System.out.println(ioe); }
setVisible(false);
}
}
public int getMessage()
{
return message;
}
}
class MyDia extends JDialog implements ActionListener //挑战失败对话框
{ static final int YES=1,NO=0,CLOSE=-1;
int message=10;
Button yes,no;
Icon icon;
JLabel l;
MyDia(JFrame f,String s,boolean b)
{
super(f,s,b);
Container con=getContentPane();
con.setLayout(null);
icon=new ImageIcon("zhu.jpg");
l=new JLabel(icon);
yes=new Button("重新开始");
no=new Button("离开");
con.add(yes);
con.add(no);
con.add(l);
yes.addActionListener(this);
no.addActionListener(this);
setBounds(60,60,263,326);
l.setBounds(0,0,263,326);
yes.setBounds(180,230,80,30);
no.setBounds(180,265,80,30);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{ message=CLOSE;
setVisible(false); }
}
);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{
message=YES;
setVisible(false);
}
if(e.getSource()==no)
{
message=NO;
setVisible(false);
}
}
public int getMessage()
{
return message;
}
}
class Bang extends JDialog implements ActionListener //搜猪英雄榜对话框
{
Button yes,no;
JLabel label[];
int i=6;
File file;
Bang(JFrame f,String s,boolean b)
{
super(f,s,b);
Container con=getContentPane();
con.setLayout(new GridLayout(5,3));
setBounds(100,100,300,150);
yes=new Button("英雄重生");
no=new Button("再去挑战");
label=new JLabel[13];
label[0]=new JLabel(" 等级");
label[1]=new JLabel("时间");
label[2]=new JLabel("传世英雄");
label[3]=new JLabel(" 初级");
label[4]=new JLabel(" 中级");
label[5]=new JLabel(" 高级");
label[6]=new JLabel("");
label[7]=new JLabel("");
label[8]=new JLabel("");
label[9]=new JLabel("");
label[10]=new JLabel("");
label[11]=new JLabel("");
label[12]=new JLabel("");
con.add(label[0]);
con.add(label[1]);
con.add(label[2]);
con.add(label[3]);
con.add(label[6]);
con.add(label[7]);
con.add(label[4]);
con.add(label[8]);
con.add(label[9]);
con.add(label[5]);
con.add(label[10]);
con.add(label[11]);
con.add(label[12]);
con.add(yes);
con.add(no);
yes.addActionListener(this);
no.addActionListener(this);
file=new File("Bang.txt"); //英雄榜信息读入
if(file.exists()==true)
{
try{ FileReader in=new FileReader(file);
BufferedReader buff=new BufferedReader(in);
String buf=buff.readLine();
StringTokenizer fenxi=new StringTokenizer(buf,";");
while(fenxi.hasMoreTokens())
{ if(i<12)
label[i].setText(fenxi.nextToken());
i++;
}
}catch(IOException e){System.out.println(e); }
}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
setVisible(false); }
}
);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{
file.delete();
label[6].setText("999");
label[7].setText("匿名");
label[8].setText("999");
label[9].setText("匿名");
label[10].setText("999");
label[11].setText("匿名");
setVisible(false);
}
if(e.getSource()==no)
{
setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -