在 Java 中创建 JList 并且始终显示滚动条?
要始终在 JList 中显示滚动条,请使用属性 HORIZONTAL_SCROLLBAR_ALWAYS 和 VERTICAL_SCROLLBAR_ALWAYS
JList list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
以下是创建 JList 并显示滚动条的示例
示例
package my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String sports[]= {"Tennis", "Archery","Football","Fencing","Cricket","Squash","Hockey","Rugby"}; list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); frame.add(scrollPane); frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); frame.setSize(400,100); frame.setVisible(true); } }
输出
广告