【问题标题】:java splitpane help code terminates when i run itjava splitpane帮助代码在我运行时终止
【发布时间】:2018-04-17 08:09:41
【问题描述】:

当我尝试运行此代码时,我在运行控制台中收到一条消息,指出操作已终止,但代码中没有错误,一切似乎都很好

public class Component1 extends JFrame 
{
    public Component1 () {

        setTitle("GUI");
        setSize(150, 150);

        //The code below creates the two panels of the UI And they are both labelled with names.
        JPanel jsp1 = new JPanel();
        JPanel jsp2 = new JPanel();
        JLabel j1 = new JLabel("Left Side");
        JLabel j2 = new JLabel("Right Side");

        //jsp1 and jsp2 work together with the code above to create the two panels of the interface by adding "J1" and "J2" this allows 
        //both sides to be labelled and appear on the UI        

        jsp1.add(j1);
        jsp2.add(j2);   

        JSplitPane splitPane = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
            true, jsp1, jsp2);

        splitPane.setOneTouchExpandable(true);
        getContentPane().add(splitPane);
    }

    public static void main(String[] args) 
    {
        Component1 sp = new Component1 ();
        sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sp.setVisible(true);
    }
    Component1panel panel = new Component1panel ();
}

【问题讨论】:

    标签: java terminate splitpane


    【解决方案1】:

    当您的main() 结束时,您将退出您的应用程序。你应该启动一个线程来保存你的框架。

    尝试编辑你的 main (如果需要,修复导入):

    public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
    
                // build and show your GUI               
    
                Component1 sp = new Component1 ();
                sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                sp.setVisible(true);
            }
        });
    }
    

    另外,JFrame hello-world 的链接可能会有所帮助:link

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多