由于系统需要,在按钮里设了监听,想要点一次按钮就可以弹出JList
但不知道为什么,需要点击两次才能够弹出.请求大神指点..
import javax.swing.*;
import javax.swing.JFrame;import java.awt.*;
import java.awt.event.*;
public class TextList extends JFrame{
String[] s={"tu.jpg","123.txt","abc.docx"};
JButton jb1=new JButton();
JList list=new JList();
JList list1=new JList();
public static void main(String[] args) {
// TODO 自动生成的方法存根 TextList tl=new TextList();
}
public TextList()
{
final Container cp=this.getContentPane();
this.setLayout(null);
jb1.setText("测试");
jb1.setBounds(300, 200, 100, 30);
cp.add(jb1);
jb1.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
list.setListData(s);
list.setBounds(10, 10, 100, 200);
cp.add(list);
}
});
this.setSize(618, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("按钮测试");
this.setLocation(50,50);
//显示窗体
this.setVisible(true);
}
}
监听 按钮 JListJava监听按钮JList
但不知道为什么,需要点击两次才能够弹出.请求大神指点..
import javax.swing.*;
import javax.swing.JFrame;import java.awt.*;
import java.awt.event.*;
public class TextList extends JFrame{
String[] s={"tu.jpg","123.txt","abc.docx"};
JButton jb1=new JButton();
JList list=new JList();
JList list1=new JList();
public static void main(String[] args) {
// TODO 自动生成的方法存根 TextList tl=new TextList();
}
public TextList()
{
final Container cp=this.getContentPane();
this.setLayout(null);
jb1.setText("测试");
jb1.setBounds(300, 200, 100, 30);
cp.add(jb1);
jb1.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
list.setListData(s);
list.setBounds(10, 10, 100, 200);
cp.add(list);
}
});
this.setSize(618, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("按钮测试");
this.setLocation(50,50);
//显示窗体
this.setVisible(true);
}
}
监听 按钮 JListJava监听按钮JList
在
cp.add(list);
后面跟上
repaint();
就可以刷新界面了。