如何使用for语句java打开另一个程序?

时间:2016-10-21 15:37:28

标签: java image swing user-interface button

我是java的新手我一直在做基本的事情,而对于我的最终项目,我们想创建一个gui rpg。我们现在的问题是我们无法通过点击gui按钮来弄清楚如何打开另一个程序。我的朋友告诉我你们使用eclipse所以我不必显示进口。请记住我在高中时所以不要过于苛刻:D这是我们的代码:

public class Narnia {

    private static final String BACKHGROUND_IMAGE_URL = "http://randomwallpapers.net/fantasy-castle-1920x1080-wallpaper328374.jpg";

    protected void initUI() throws MalformedURLException {
        JFrame frame = new JFrame(Narnia.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final ImageIcon backgroundImage = new ImageIcon(new URL(BACKHGROUND_IMAGE_URL));

        JLabel mainPanel = new JLabel(backgroundImage) {
            @Override
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                Dimension lmPrefSize = getLayout().preferredLayoutSize(this);
                size.width = Math.max(size.width, lmPrefSize.width);
                size.height = Math.max(size.height, lmPrefSize.height);
                return size;
            }
        };

        mainPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        for (int i = 0; i < 1; i++) {
            mainPanel.add(new JButton("Play" + ("")), gbc);
        }
        for (int i = 0; i < 1; i++) {
            mainPanel.add(new JButton("Credits " + ("")), gbc);
        }
        for (int i = 0; i < 1; i++) {
            mainPanel.add(new JButton("Exit " + ("")), gbc);
        }

        // Let's put a filler bottom component that will push the rest to the top
        gbc.weighty = 1.0;
        mainPanel.add(Box.createGlue(), gbc);
        frame.add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new Narnia().initUI();
                    } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

以下是我们要打开的课程:

public class chooseaclass {

    private static final String BACKHGROUND_IMAGE_URL = "http://randomwallpapers.net/fantasy-castle-1920x1080-wallpaper328374.jpg";

    protected void initUI() throws MalformedURLException {
        JFrame frame = new JFrame(chooseaclass.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final ImageIcon backgroundImage = new ImageIcon(new URL(BACKHGROUND_IMAGE_URL));

        JLabel mainPanel = new JLabel(backgroundImage) {
            @Override
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                Dimension lmPrefSize = getLayout().preferredLayoutSize(this);
                size.width = Math.max(size.width, lmPrefSize.width);
                size.height = Math.max(size.height, lmPrefSize.height);
                return size;
            }
        };

        mainPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(40, 40, 40, 40);
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        for (int i = 0; i < 1; i++) {
            mainPanel.add(new JButton("Archer" + ("")), gbc);
        }
        for (int i = 0; i < 1; i++) {
            mainPanel.add(new JButton("Mage " + ("")), gbc);
        }
        for (int i = 0; i < 1; i++) {
            mainPanel.add(new JButton("Knight " + ("")), gbc);
        }

        // Let's put a filler bottom component that will push the rest to the top
        gbc.weighty = 1.0;
        mainPanel.add(Box.createGlue(), gbc);
        frame.add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new chooseaclass().initUI();
                    } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您需要为所需的任何按钮添加侦听器。在这种情况下,我们将使用ActionListener

让我们使用您已有的现有专栏:mainPanel.add(new JButton("Play" + ("")), gbc);

首先,为了简化,我们将JButton放在一个变量中: JButton playButton = new JButton("Play" + (""));

要添加侦听器,我们需要使用方法addActionListener()

现在添加一个ActionListener作为匿名类,以便我们可以实现一个系统可以在幕后调用的方法:

JButton playButton = new JButton("Play" + (""));
    playButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            new chooseaclass.initUI() //insantiate a new chooseaclass instance

        }

    });
    mainPanel.add(playButton, gbc);

actionPerformed()方法中,我实例化了chooseaclass。你可以从那里做任何你想做的事。

我在没有编辑器的情况下从袖口写下这段代码,因此它可能包含语法错误。