62,612
社区成员
发帖
与我相关
我的任务
分享
package dianm;
public class DianM {
public static void main(String[] args) {
show s=new show(27);
// Mlistener listener=new Mlistener();
// listener.gets(s);
s.Jisuan();
s.Paint();
s.run(new YanShi().XuanZ("零次函数"));
// System.out.println("XXXXXXXXXXXXXXXXX");
// s.label[s.locArray[0][0]][s.locArray[0][1]].addMouseListener(listener);
}
}
主类package dianm;
import java.awt.Color;
import java.awt.GridLayout;
import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import static javax.swing.SwingConstants.CENTER;
public class show extends JFrame{
int Pnum,l,w;
int[][] locArray;
JLabel label[][]=new JLabel[10][10];
show(){}
show(int n){
if(n%2==0)
Pnum=n;
else
Pnum=n+1;
locArray=new int[Pnum][2];}
void Jisuan(){
l=10;
w=10;
int x=1,y=1;
int temp=0;
for(;temp<Pnum;){
if (x==1 && y>0 && y<8){locArray[temp][0]=x; locArray[temp][1]=y; y++; temp++;}
else if(y==8 && x>0 && x<8){locArray[temp][0]=x; locArray[temp][1]=y; x++; temp++;}
else if(x==8 && y<9 && y>1){locArray[temp][0]=x; locArray[temp][1]=y; y--; temp++;}
else if(y==1 && x<9 && x>1){locArray[temp][0]=x; locArray[temp][1]=y; x--; temp++;}
else{System.out.println("Wrong!");}
}
}
void Paint(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(10,10,700,700);
setLayout(new GridLayout(10,10,5,5));
int i,j;
for(i=0;i<10;i++){
for(j=0;j<10;j++){
label[i][j]=new JLabel();
label[i][j].setOpaque(true);
add(label[i][j]);
}
}
for(int n=0;n<Pnum;n++){
label[locArray[n][0]][locArray[n][1]].setText(String.valueOf(n));
label[locArray[n][0]][locArray[n][1]].setBackground(Color.black);
label[locArray[n][0]][locArray[n][1]].setForeground(Color.white);
label[locArray[n][0]][locArray[n][1]].setHorizontalAlignment(CENTER);
}
setVisible(true);
}
void run(int[] a){
for(int i=0;;i++){
try {
if(i<a.length){
sleep(a[i]);
}
else{
sleep(a[a.length-1]);
}
} catch (InterruptedException ex) {
Logger.getLogger(show.class.getName()).log(Level.SEVERE, null, ex);
}
label[locArray[i%Pnum][0]][locArray[i%Pnum][1]].setBackground(Color.red);
if(i>0)
label[locArray[(i-1)%Pnum][0]][locArray[(i-1)%Pnum][1]].setBackground(Color.black);
}
}
}
界面,监视器想调用其中的run方法package dianm;
public class YanShi {
int[] XuanZ(String a){
if(null != a)
switch (a) {
case "零次函数":
return YS0();
case "一次函数":
return YS1();
case "二次函数":
return YS2();
default:
System.out.println("Wrong!");
}
return null;
}
private int[] YS0(){
int[] a=new int[10];
int i=0;
while(i<10){a[i]=20;i++;}
return a;
}
private int[] YS1(){
int[] a={300,280,260,240,220,200,180,160,140,120,100,80,60,40,20};
return a;
}
private int[] YS2(){
int[] a=new int[10];
return a;
}
}
这个类是用来控制跑马灯延时的,以int[]返回JLabel lable = new JLabel("0");
lable.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
//鼠标单击事件
super.mouseClicked(e);
}
});