我正在尝试将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();
}
}
请知道为什么以及该做什么?