Java如何关闭程序并从GUI移到另一个程序 [英] Java how to close program and move to anther one from a GUI

查看:41
本文介绍了Java如何关闭程序并从GUI移到另一个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个带有图像的普通窗口.我想知道如何制作一个按钮,说单击此处开始",按下该按钮将关闭程序并启动另一个程序.

I have made a normal window with an image. I was wondering how to make a button saying "Click here to start" which when pressed will close the program and launch another program.

推荐答案

我会先看看 如何使用按钮 并查看如何使用 CardLayout

这将允许您拥有一个窗口并减少您需要的切换代码量

This will allow you to have a single window and reduce the amount of switching code you would need

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SimpleDemo {

    public static void main(String[] args) {
        new SimpleDemo();
    }

    public SimpleDemo() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final CardLayout cardLayout = new CardLayout();
                final JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(cardLayout);

                JPanel startPanel = new JPanel(new GridBagLayout());
                JButton startButton = new JButton("Start");
                startPanel.add(startButton);
                startButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cardLayout.show(frame.getContentPane(), "game");
                    }
                });

                JLabel game = new JLabel("Game On", JLabel.CENTER);

                frame.add(startPanel, "start");
                frame.add(game, "game");

                cardLayout.show(frame.getContentPane(), "start");

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

这篇关于Java如何关闭程序并从GUI移到另一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆