当按下JButton时,Java运行其他类的main方法 [英] Java running main method of other class, when JButton is pressed

查看:274
本文介绍了当按下JButton时,Java运行其他类的main方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个JFrame,它有两个按钮,可以让我调用其他类的main方法。第一次尝试是将它直接放入每个按钮的actionPerformed中,这将导致另一个类的JFrame打开但只显示它的标题而不显示JPanel的任何内容另外冻结程序(甚至不能按关闭按钮,必须进入任务管理器或eclipse杀死它)。第二次尝试是在actionPerformed中添加一个方法调用,并且这次添加方法将调用其他类的main方法但结果相同(冻结程序)。

I am trying to develop a JFrame which has two buttons that would let me to call the main method of other classes. The first try was to put it directly into the actionPerformed of each button, this will cause the JFrame of the other class to open but showing only the title of it and not showing any contents of the JPanel additionally freezing the program (can't even press close button, have to go into task manager or eclipse to kill it). The second try was adding a method call in actionPerformed, and adding the method will this time call the main method of other class however the same result (freeze of program).

出于测试目的,我已经调用了其他类的main方法,直接在这个类main方法中向我证明了其他类的框架已成功出现,包括它的所有JPanel内容,功能等。

For testing purposes I have placed the call to main method of other class, straight in this class main method which has proven to me that the frame of other class has successfully appeared, including all its JPanel contents, functionality etc.

我知道我可以在我的main方法中做一些无限循环,等到布尔值设置为true,但是那么我虽然必须有一些更便宜的方式让它运作。所以我在这里向你们提出这个问题。

I know I could make some kind of infinite loop in my main method to wait until a boolean is set to true, but then I though there must be some less-expensive way to get it working. So here I am asking this question to you guys.

这是第二次尝试的代码;

Here is the code of the 2nd try;

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;


    public class Chat {
    public static void main (String[] args) {


        JFrame window = new JFrame("Chat Selection");

        //Set the default operation when user closes the window (frame)
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set the size of the window
        window.setSize(600, 400);
        //Do not allow resizing of the window
        window.setResizable(false);
        //Set the position of the window to be in middle of the screen when program is started
        window.setLocationRelativeTo(null);

        //Call the setUpWindow method for setting up all the components needed in the window
        window = setUpWindow(window);

        //Set the window to be visible
        window.setVisible(true);

    }

    private static JFrame setUpWindow(JFrame window) {
        //Create an instance of the JPanel object
        JPanel panel = new JPanel();
        //Set the panel's layout manager to null
        panel.setLayout(null);

        //Set the bounds of the window
        panel.setBounds(0, 0, 600, 400);

        JButton client = new JButton("Run Client");
        JButton server = new JButton("Run Server");
        JLabel author = new JLabel("By xxx");

        client.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //run client main
                runClient();
            }
        });

        server.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //run server main
            }
        });

        panel.add(client);
        client.setBounds(10,20,250,200);
        panel.add(server);
        server.setBounds(270,20,250,200);
        panel.add(author);
        author.setBounds(230, 350, 200, 25);

        window.add(panel);

        return window;
    }

    private static void runClient() {

        String[] args1={"10"};
        ClientMain.main(args1);
    }
}


推荐答案

你遇到的问题是Java Swing是单线程的。当您运行其他类的main函数时,无论如何操作,GUI都将无法继续运行,直到它返回为止。尝试生成一个调用第二个主方法的新线程。

The problem you're having is that Java Swing is single threaded. When you're running the main function of the other class, however you do it, the GUI won't be able to keep running until it returns. Try spawning off a new thread that calls the second main method.

private static void runClient() {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            String[] args1={"10"};
            ClientMain.main(args1);
        }
    });
}

编辑:根据@ Radiodef的建议更新。当你说第二堂课必须在GUI上显示东西时,错过了顶部。当然绝对想要使用invokeLater。

Updated, as per @Radiodef's suggestion. Missed at the top when you said this second class had to display things on the GUI. Definitely want to go with the invokeLater then.

这篇关于当按下JButton时,Java运行其他类的main方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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