Jframe突然不再可见了

时间:2016-11-02 17:59:25

标签: java jframe visibility jlist

我正在建立一个音乐播放器。一切都很完美。但是当我尝试向我的第二个JList添加一个JPanel时,它都失败了..文件编译并在命令提示符下运行程序..我想......但是程序没有显示出来。

即使我没有触及window.setVisible(true);可见线。

有人能看到错误吗?

这是代码:

/**
 * Created by Natalie89dk on 02-11-2016.
 */
import javax.swing.*;
import java.awt.*;

public class Frame {

    public Frame(){
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Player");
        window.setBounds(200, 10, 500, 300);
        window.setLayout(new BorderLayout());
        window.setVisible(true);

        JPanel playlistList = new JPanel(new GridBagLayout());
        playlistList.setBackground(Color.GRAY);
        window.add(playlistList, BorderLayout.WEST);

        GridBagConstraints contain1 = new GridBagConstraints();
        JLabel label11 = new JLabel("Playlists");
        contain1.gridx = 0;
        contain1.gridy = 0;
        contain1.insets = new Insets(10, 10, 10, 10);
        playlistList.add(label11, contain1);

        JButton createPlaylist = new JButton();
        createPlaylist.setText("Create playlist");
        contain1.gridx = 0;
        contain1.gridy = 1;
        contain1.insets = new Insets(10, 10, 10, 10);
        playlistList.add(createPlaylist, contain1);

        DefaultListModel<String> listModel = new DefaultListModel<String>();
        JList<String> list = new JList<String>(listModel);
        contain1.gridx = 0;
        contain1.gridy = 2;
        contain1.insets = new Insets(10, 10, 10, 10);
        playlistList.add(list, contain1);

        String playlist1 = "Playlist1";
        String playlist2 = "Playlist2";
        String playlist3 = "Playlist3";
        String playlist4 = "Playlist4";
        String playlist5 = "Playlist5";
        String playlist6 = "Playlist6";
        String playlist7 = "Playlist7";
        String playlist8 = "Playlist8";
        String playlist9 = "Playlist9";
        String playlist10 = "Playlist10";
        String playlist11 = "Playlist11";
        String playlist12 = "Playlist12";

        listModel.addElement(playlist1);
        listModel.addElement(playlist2);
        listModel.addElement(playlist3);
        listModel.addElement(playlist4);
        listModel.addElement(playlist5);
        listModel.addElement(playlist6);
        listModel.addElement(playlist7);
        listModel.addElement(playlist8);
        listModel.addElement(playlist9);
        listModel.addElement(playlist10);
        listModel.addElement(playlist11);
        listModel.addElement(playlist12);

        JScrollPane scrollPane = new JScrollPane(list);
        //scrollPane.setViewportView(list);
        contain1.gridx = 0;
        contain1.gridy = 3;
        contain1.insets = new Insets(10, 10, 10, 10);
        playlistList.add(scrollPane, contain1);

        JPanel currentPlaylist = new JPanel(new GridBagLayout());
        currentPlaylist.setBackground(Color.GRAY);
        window.add(currentPlaylist, BorderLayout.EAST);

        GridBagConstraints contain = new GridBagConstraints();

        JLabel label12 = new JLabel("Playing playlist");
        contain.gridx = 0;
        contain.gridy = 0;
        contain.insets = new Insets(10, 10, 10, 10);
        currentPlaylist.add(label12, contain);

        JButton addTrack = new JButton();
        //addTrack.setSize(new Dimension(100, 30));
        addTrack.setText("Add Track");
        // window.add(addTrack, BorderLayout.EAST);
        contain.gridx = 0;
        contain.gridy = 1;
        contain.insets = new Insets(10, 4, 10, 10);
        currentPlaylist.add(addTrack, contain);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Player();
            }
        });
    }
}

3 个答案:

答案 0 :(得分:1)

这很困难的原因是抛出异常并且你没有看到它。尝试将您的调用包装到“异常”的try / catch中的新Player()并打印出结果。

虽然这并没有直接回答你的问题,但它会给你答案,也会在将来帮助你:)

这是一个常见的事情,你需要记住,只要你有一个异步操作的run()类型的方法来检查。

Java不会强制您进入任何特定的日志记录工具,但“main”默认将异常记录到标准错误(System.err)。这只是main方法的行为而与Java无关!从“Frame”抛出的异常(例如它不是JFrame的事实)直到invokeLater返回之后才会发生 - 所以main物理上无法处理异常,因为它在异常发生之前已经退出( invokeLater在代码运行之前立即返回。

当你运行一个线程异常,逃避你的run()方法只是停止线程执行,并且静默消耗堆栈跟踪。有一个未被捕获的异常处理程序的概念可以很容易地完成这项工作,但默认情况下它什么都不做。

您通常必须自己处理异步异常,否则它们将完全隐身,隐藏异常是Java中最大的开发人员时间浪费之一。

答案 1 :(得分:0)

我认为这可能是因为您没有在类框架上使用扩展JFrame。

示例:

 public class Frame extends JFrame{}

答案 2 :(得分:0)

您的课程称为Frame,而不是Player

替换它:

                    @Override
                    public void run() {
                        new Player();
                    }

                    @Override
                    public void run() {
                        new Frame();
                    }