如何在 Java Swing JList 中显示行数
使用 JList getVisibleRowCount() 方法在 JList 中显示行数
String sports[]= {"Tennis", "Archery","Football","Fencing","Cricket","Squash","Hockey","Rugby"}; Jlist list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount);
以下是 JList 中显示行数的示例
示例
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); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount); 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); } }
输出
控制台中的输出将显示总行数
Row Count = 8
广告