刚学java不久,我想写一个带滚动条的list,编译的时候一直有:使用了未经检查或不安全的操作,请使用-Xlint:unchecked重新编译
查不出原因.望指点.
- Java code
import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;public class textarea2 implements ListSelectionListener{ String[] listEntries = {"alpha","bata","gama","delta","epsilon","zeta","eta","theta"}; JList llist = new JList(listEntries); public static void main(String[] args) { textarea2 gui = new textarea2(); gui.go(); }public void go() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); llist.setVisibleRowCount(4); llist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); llist.addListSelectionListener(this); JScrollPane scroller = new JScrollPane(llist); scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ); scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER ); frame.getContentPane().add(BorderLayout.CENTER,scroller); frame.setSize(350,300); frame.setVisible(true); } public void valueChanged(ListSelectionEvent e) { }}
------解决方案--------------------------------------------------------
你使用的是jdk7吧!
Java 7里Swing开始支持范型,JList 变成 JList<String>. 常量应该使用类不是对象实例引入。
frame.EXIT_ON_CLOSE 改成 JFrame.EXIT_ON_CLOSE.
- Java code
import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;public class TextArea2 implements ListSelectionListener{ String[] listEntries = {"alpha","bata","gama","delta","epsilon","zeta","eta","theta"}; JList<String> llist = new JList<>(listEntries); public static void main(String[] args) { TextArea2 gui = new TextArea2(); gui.go(); } public void go() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); llist.setVisibleRowCount(4); llist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); llist.addListSelectionListener(this); JScrollPane scroller = new JScrollPane(llist); scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ); scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER ); frame.getContentPane().add(BorderLayout.CENTER,scroller); frame.setSize(350,300); frame.setVisible(true); } public void valueChanged(ListSelectionEvent e) { }}