import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ProgramB extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
static DefaultListModel<String> dlm1 = new DefaultListModel<String>();
static JButton btnGroup1 = new JButton("");
static boolean boo = false;
public static void main(String args[]){
JFrame jf = new JFrame("测试窗口");
jf.setSize(800, 600);
jf.setLocationRelativeTo (null);
jf.setLayout(null);
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
JPanel jpa = new JPanel();
jpa.setBounds(40, 40, 200, 200);
jpa.setBackground(Color.WHITE);
jpa.setLayout(null);
jf.add(jpa);
final JPanel jpa2 = new JPanel();
jpa2.setBounds(100, 300, 600, 200);
jpa2.setBackground(Color.WHITE);
jpa2.setLayout(null);
jf.add(jpa2);
final JButton jbtn = new JButton("开始");
jbtn.setBounds(300, 40, 160, 80);
final JButton jbtn2 = new JButton("停止");
jbtn2.setBounds(300, 40, 160, 80);
jf.add(jbtn);
jf.add(jbtn2);
JList<String> jl = new JList<String>(dlm1);
JScrollPane jsp = new JScrollPane(jl);
jsp.setBounds(20, 20, jpa.getWidth()-40, jpa.getHeight()-40);
jl.setFont(new Font("宋体", Font.CENTER_BASELINE, 32));
jsp.setBackground(Color.WHITE);
jpa.add(jsp);
dlm1.addElement("11");
dlm1.addElement("22");
dlm1.addElement("33");
dlm1.addElement("44");
dlm1.addElement("55");
jl.setModel(dlm1);
jbtn.addActionListener(new ActionListener() {
//开始按钮
@Override
public void actionPerformed(ActionEvent e) {
jpa2.removeAll();
jpa2.updateUI();
jbtn.setVisible(false);
jbtn2.setVisible(true);
ProgramB t = new ProgramB();
new Thread(t).start();
for(int i=0;i<dlm1.size();i++){
boo = false;
btnGroup1 = new JButton();
btnGroup1.setBounds(jpa2.getWidth()/dlm1.size()*i+20, 20, jpa2.getWidth()/dlm1.size()-30, jpa2.getHeight()-40);
btnGroup1.setFont(new Font("宋体", Font.BOLD, 24));
btnGroup1.setBackground(new Color(102,205,170));
jpa2.repaint();
jpa2.validate();
jpa2.add(btnGroup1);
}
}
});
jbtn2.addActionListener(new ActionListener() {
//stop
@Override
public void actionPerformed(ActionEvent e) {
boo = true;
jbtn2.setVisible(false);
jbtn.setVisible(true);
}
});
}
@Override
public void run() {
while(!boo){
String[] str2 = new String[dlm1.size()];
for(int k=0;k<dlm1.size();k++){
str2[k] = dlm1.getElementAt((int)(0+Math.random()*str2.length));
//System.out.println(Integer.valueOf(str2[k]));
btnGroup1.setText(str2[k]);
//System.out.println(dlm1.getElementAt(Integer.valueOf(str2[k])));
}
}
}
}