Java Swing – JTable没有显示

我在使用Java Swing时遇到了一些麻烦.
我正在尝试使用顶部的控制面板制作一个框架,其中包含一些按钮.
以下我希望JTable显示

我一直在尝试,但桌子没有显示.
如果我删除顶部的controlPanel,它有时显示,有时不显示.
我在我的JTable的构造函数中使用的代码在同一个应用程序中提供,
所以它没有网络错误

public ServerMainFrame(GuiController gc){
    this.gc = gc;
    initGUI();
}

private void initGUI() {
    System.out.println("initiating GUI");
    createFrame();
    addContentPanel();
    addControls();
    //openPopUpServerSettings();
    addSongTable();
}

private void createFrame()
{
    this.setTitle("AudioBuddy 0.1");
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(800, 600);
    this.setResizable(false);
    this.setLocationRelativeTo(null);
}

private void addContentPanel()
{
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout());
    p.setSize(new Dimension(800, 600));
    this.setContentPane(p);
}

private void addControls()
{
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());
    controlPanel.setBorder(BorderFactory.createLineBorder(Color.black));
    controlPanel.setSize(700,100);

    // Buttons
    JButton play = new JButton("Play");
    JButton pause = new JButton("Pause");
    JButton stop = new JButton ("Stop");
    JButton next = new JButton("Next");
    JButton prev = new JButton("Previous");     
    controlPanel.add(play);
    controlPanel.add(pause);
    controlPanel.add(stop);
    controlPanel.add(next);
    controlPanel.add(prev);

    // Currently playing
    JLabel playing = new JLabel("Currently playing:");
    controlPanel.add(playing);

    JLabel current = new JLabel("Johnny Cash - Mean as Hell");
    controlPanel.add(current);

    this.getContentPane().add(controlPanel);
}

private void addSongTable()
{
    JTable songTable = new JTable(Server.getSongTableModel());
    songTable.setVisible(true);
    JPanel tablePanel = new JPanel();
    tablePanel.setVisible(true);
    tablePanel.add(songTable);
    songTable.repaint();
    this.getContentPane().add(tablePanel);

    JButton btnMulticastList = new JButton("send list to clients");
    btnMulticastList.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            Server.MulticastPlaylist();
        }
    });

    getContentPane().add(btnMulticastList);
}

解决方法:

if I remove the controlPanel at the top, it sometimes shows and
sometimes not.

>一切都隐藏在Server.getSongTableModel()中,没有人知道没有发布带有硬编码值的SSCCE
> GUI与Concurency in Swing有问题
> XxxModel持续加载数据与构建GUi,然后异常导致描述的问题

The code that I use inside my constructor of my JTable is provided in
the same application, so it’s no network error

>不知道你在说什么
>必须创建一个空GUI,见InitialTread
>显示GUI,然后开始将数据加载到JTable
>然后从SwingWorker或(描述网络问题)启动工作线程(Backgroung任务)更好Runnable #Thread(用于捕获异常和处理单独线程的confortable)
>从Runnable到Swing GUI的输出必须包装到invokeLater()中

上一篇:Java实训小结


下一篇:最大中位数问题