JScrollPane组件不会出现

时间:2015-11-15 18:01:14

标签: java swing jpanel jscrollpane jlist

我正在尝试将JList添加到我的JPanel中,因为它由以下源代码声明,但它不起作用:

    public class Game {

    public static void main(String[] args) throws Exception {

    UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

    DefaultListModel<String> defListPlayers = new DefaultListModel<String>();
    defListPlayers.addElement("Player 1");
    defListPlayers.addElement("Player 2");

    JList<String> listPlayers = new JList<String>(defListPlayers);
    listPlayers.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    listPlayers.setLayoutOrientation(JList.VERTICAL);
    listPlayers.setVisibleRowCount(10);


    JScrollPane scroller = new JScrollPane(listPlayers);
    scroller.setViewportBorder(new LineBorder(Color.RED));
    scroller.revalidate();

    JPanel panel = new JPanel();
    panel.add(scroller);

    JFrame frame = new JFrame;
    frame.setContentPane(panel);
    frame.pack();
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().repaint();

    }
   }

请知道为什么以及该做什么?

0 个答案:

没有答案