【问题标题】:JTable is not showing [closed]JTable没有显示[关闭]
【发布时间】:2014-02-01 15:53:27
【问题描述】:

谁能告诉我这段代码有什么问题?除了菜单,它没有显示任何内容,我似乎无法弄清楚出了什么问题。

 public class ProfesorSelect extends JFrame {
    public ProfesorSelect(Profesor profesor){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem newItem = new JMenuItem("Logout");
        JMenuItem open = new JMenuItem("Exit");
        setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.CENTER;
        fileMenu.add(newItem);
        fileMenu.add(open);
        setName(profesor.toString() + " /Profesor");
        menuBar.add(fileMenu);
        JScrollPane panel = new JScrollPane();
        JScrollPane scrollPanel = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        for(Clasa clasa: Centralizator.getInstance().getClase()){
            JPanel panelClasa = new JPanel();
            panelClasa.setLayout(new BoxLayout(panelClasa,BoxLayout.Y_AXIS));
            JLabel numeClasa = new JLabel(clasa.getIdClasa());
            ArrayList<String> elevi = new ArrayList<String>();
            for (Elev elev : clasa.getElevi()){
                elevi.add(elev.toString());
            }
            JScrollPane jScrollPane = new JScrollPane(new JList(elevi.toArray()));
            panelClasa.add(numeClasa);
            panelClasa.add(jScrollPane);
            scrollPanel.add(panelClasa);
        }
        add(scrollPanel);

        setJMenuBar(menuBar);
        pack();
        setVisible(true);
    }
}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发布MCVE。代码需要 main(String[]) 并导入为 MCVE。
  • 哎呀,你一定是在开玩笑吧,...你的代码甚至没有 JTable。严重地?请改进这个问题很多或删除它。
  • Centralizator 当然,您需要包含该类,或者将其完全分解以显示 MCVE..

标签: java swing jtable jscrollpane


【解决方案1】:

不要使用scrollPane.add 这不是滚动窗格的使用方式。

看看How to use scroll panes

【讨论】:

    猜你喜欢
    • 2012-10-28
    • 2020-11-17
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2014-08-01
    • 2012-01-16
    相关资源
    最近更新 更多