java如何打开另外一个程序的窗口?

这个是主程序的JButton按钮,想通过点击按钮打开并运行另一个程序

img

另一个程序:

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) {
        }

dispose();
new 打字游戏();
给你参考一下吧:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class StartupWindow extends JFrame implements ActionListener
{
    private JButton btn;
    public StartupWindow()
    {
        super("Simple GUI");
        setSize(500,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        btn = new JButton("Open the other JFrame!");
        btn.addActionListener(this);
        btn.setActionCommand("Open");
        add(btn);
        pack();
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String cmd = e.getActionCommand();
        if(cmd.equals("Open"))
        {
            dispose();
            new AnotherJFrame();
        }
    }
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run()
            {
                new StartupWindow().setVisible(true);
            }
        });
    }
}
import javax.swing.JFrame;
import javax.swing.JLabel;
public class AnotherJFrame extends JFrame
{
    public AnotherJFrame()
    {
        super("Another GUI");
        setSize(500,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new JLabel("Empty JFrame"));
        pack();
        setVisible(true);
    }
}