我面临的问题是,当我从GUImain类启动程序时,它将毫无问题地工作,但是当我尝试从主类中调用它时,则无法打开。

我从控制台收到的唯一消息是:

<terminated> Main [Java Application] /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java (5 Dec 2014 14:39:29)


这是我的主要课程代码:

public class Main
{
    public static void main(String[] args)
    {
        int i = 0;
        int t = 0;
        int st = 0;
        int h = 0;

        Texts textObject = new Texts();
        textObject.TextList();

        Commands commandObject = new Commands();
        commandObject.commands();

        GUImain guiObject = new GUImain();
    }
}


这是我的GUImain课程

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JTextField;

public class GUImain
{
    private JFrame frame;
    private JTextField textField;


    //Launch the application.
    public static void main(String[] args)
    {
        GUImain window = new GUImain();
        window.frame.setVisible(true);
    }

    //Create the application.
    public GUImain()
    {
        frame = new JFrame();
        frame.setBounds(100, 100, 611, 471);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnInventory = new JButton("Inventory");
        btnInventory.setBounds(514, 6, 91, 29);
        frame.getContentPane().add(btnInventory);

        JButton btnLoad = new JButton("Load");
        btnLoad.setBounds(453, 6, 68, 29);
        frame.getContentPane().add(btnLoad);

        JButton btnSave = new JButton("Save");
        btnSave.setBounds(404, 6, 54, 29);
        frame.getContentPane().add(btnSave);

        JButton btnOptions = new JButton("Options");
        btnOptions.setBounds(335, 6, 76, 29);
        frame.getContentPane().add(btnOptions);

        JTextArea History = new JTextArea();
        History.setText("lol");
        History.setBounds(6, 6, 329, 343);
        frame.getContentPane().add(History);

        JButton btnEnter = new JButton("Enter");
        btnEnter.setBounds(518, 404, 85, 39);
        frame.getContentPane().add(btnEnter);

        JScrollBar scrollBar = new JScrollBar();
        scrollBar.setBounds(320, 6, 15, 338);
        frame.getContentPane().add(scrollBar);

        textField = new JTextField();
        textField.setBounds(5, 410, 508, 28);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(6, 357, 600, 42);
        frame.getContentPane().add(textArea);

        JLabel lblMapGoesHere = new JLabel("Map goes here");
        lblMapGoesHere.setBounds(342, 37, 263, 312);
        frame.getContentPane().add(lblMapGoesHere);
    }
}

最佳答案

您只是在Main类中创建GUImain的实例。
您应该使其可见或运行GUImain的main方法。
尝试添加行,

frame.setVisible(true);


到GUImain的构造函数的末尾。您可以删除

window.frame.setVisible(true);


从主要的方法。

这样可以解决问题。但这不是这样做的好方法。

09-20 10:39