我现在的问题是一个按钮,一个JList,我要在点按钮后从数据库取数据显示在JList上,
怎么样做个假的JList,然后用按钮监听里的JList去覆盖他?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;public class ListDemo extends JPanel implements ActionListener {
private JList list;
private JButton btn;
private final String[] s = {\"one\",\"two\",\"three\",\"four\",\"five\"}; //把这个换成数据库里的数据便可以了
public ListDemo() {
String[] temp = {\"Click Button\"};
list = new JList(temp);
btn = new JButton(\"Click\");
btn.addActionListener(this);
add(list);
add(btn);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame(\"Demo\");
f.getContentPane().add(new ListDemo());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,300);
f.setVisible(true);
}public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
list.setListData(s);
}}