【问题标题】:Why won't my swing GUI launch from the main class为什么我的摆动 GUI 不能从主类启动
【发布时间】:2015-02-03 18:26:53
【问题描述】:

我面临的问题是,当我从 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”java.lang.Error 中的异常:未解决的编译问题:方法 setVisible(boolean) 未在 Main.main(Main.java:17) 处为 GUImain 类型定义并且我在 GUImain 下设置了可见的GUImain 窗口 = new GUImain();

标签: java eclipse swing user-interface awt


【解决方案1】:

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

frame.setVisible(true);

到 GUImain 的构造函数结束。你可以删除

window.frame.setVisible(true);

来自主方法。

这将解决问题。但这不是一个好方法。

【讨论】:

    【解决方案2】:

    Main 类中的main 方法只是创建一个新的GUImain 对象,其中包含一个名为JFrame 的实例frame,但它永远不会可见:

    public class Main {
        public static void main(String[] args) {
            ...
            GUImain guiObject = new GUImain();
        }
    }
    

    您可以向这个frame 类成员添加一个公共getter 以使其可见:

    public class GUImain {
       ...
       public JFrame getFrame() {
           return this.frame;
       }
    }
    
    public class Main {
        public static void main(String[] args) {
            ...
            GUImain guiObject = new GUImain();
            guiObject.getFrame().setVisible(true);
        }
    }
    

    或者只是向GUImain 添加一个新方法以显示框架:

    public class GUImain {
       ...
       public void displayGUI() {
           this.frame.setVisible(true);
       }
    }
    
    public class Main {
        public static void main(String[] args) {
            ...
            GUImain guiObject = new GUImain();
            guiObject.displayGUI();
        }
    }
    

    旁注

    Swing 组件应该在Evevent Dispatch Thread (EDT) 的上下文中创建和更新。见Initial Threads

    【讨论】:

    • 感谢您的出色工作我使用了第二种方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多