这个是主程序的JButton按钮,想通过点击按钮打开并运行另一个程序
另一个程序:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public static void main(String[] args) {
JFrame w=new JFrame();
w.setSize(500,400);
Mypanel6 mp=new Mypanel6();
mp.init();
w.add(mp);
w.addKeyListener(mp);
w.setVisible(true);
w.setVisible(true);
mp.go();
}
}
class Mypanel6 extends JPanel implements KeyListener{
int x[]=new int[10];
int y[]=new int[10];
char c[]=new char[10];
int score=1000;
public void init() {
for(int i=0;i<10;i++) {
x[i]=i*50;
y[i]=0;
c[i]=(char)(Math.random()*26+97);
}
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.BLACK);
for(int i=0;i<10;i++) {
g.drawString(new Character(c[i]).toString(),x[i],y[i]);
}
g.setColor(Color.RED);
g.drawString("你的成绩是:"+score,5,15);
}
public void go() {
while(true) {
for(int i=0;i<10;i++) {
y[i]++;
if(y[i]>400) {
y[i]=0;
x[i]=i*50;
c[i]=(char)(Math.random()*26+97);
score-=10;
}
}
try {
Thread.sleep(100);
}catch (Exception e) {}
repaint();
}
}
public void keyPressed(KeyEvent arg0) {
char keyc=arg0.getKeyChar();
int hitIndex =-1;
for(int i=0;i<10;i++){
if(keyc == c[i]) {
hitIndex=i;
}
}
if(hitIndex!=-1) {
y[hitIndex]=0;
x[hitIndex]=hitIndex*50;
c[hitIndex]=(char)(Math.random()*26+97);
score+=10;
}else {
score-=10;
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}