Anson__2002 2021-12-18 11:14 采纳率: 100%
浏览 41
已结题

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

  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2021-12-18 11:18
    关注

    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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月20日
  • 已采纳回答 12月18日
  • 创建了问题 12月18日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan