1

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

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

我知道我可以在我的 main 方法中创建某种无限循环来等到布尔值设置为 true,但是我认为必须有一些更便宜的方法来让它工作。所以在这里我向你们提出这个问题。

这是第二次尝试的代码;

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);
    }
}
4

2 回答 2

2

每个应用程序只允许使用一种主要方法。老实说,当您在其他课程上调用 main 时,我不确定您正在尝试做什么或认为应该发生什么。当您在其他类上调用 main 时,您所做的只是调用一个恰好被称为 main 的方法并将 args 传递给它。您的冻结可能是因为您没有正确使用 Swing:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

于 2013-10-24T20:37:47.640 回答
1

您遇到的问题是 Java Swing 是单线程的。当你运行另一个类的主函数时,不管你怎么做,GUI 将无法继续运行,直到它返回。尝试生成一个调用第二个 main 方法的新线程。

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

编辑:根据@Radiodef 的建议进行了更新。当您说第二类必须在 GUI 上显示内容时,在顶部错过了。那么肯定要使用 invokeLater 。

于 2013-10-24T20:42:00.477 回答