public TestSleep(){ Container con = getContentPane(); con.setLayout(new BorderLayout()); Runnable r = new Change(this,con); Thread th = new Thread(r); th.start(); setVisible(true);
} public void InitUi(Container con){ JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER)); p.add(bcn); p.add(cancel); con.add(p,BorderLayout.SOUTH);
addListener();
setVisible(true); }
public void addListener(){ bcn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println(" 确认 "); } });
cancel.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println(" 退出 "); } }); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TestSleep(); }} class Change implements Runnable{ private TestSleep sl = null; private Container con = null; private int width = 0; private int height = 0;
import javax.swing.*;public class MyButton extends JButton
{
private String str;
public MyButton()
{
}
public MyButton(String str)
{
this.str=str;
}
public static void main(String args[])
{
Frame frame=new Frame();
frame.setLayout(new GridLayout(10,10));
MyButton[] mb=new MyButton[100];
for(int i=0;i<mb.length;i++)
{
mb[i]=new MyButton();
}
for(int i=0;i<mb.length;i++)
{
frame.add(mb[i]);
}
frame.setSize(400,300);
frame.setVisible(true);
}
}
我只加了100个!另100个同样的方法!
不知道这是不是楼主说的意思!
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class TestSleep extends JFrame{
private JButton bcn = new JButton("Confirm");
private JButton cancel = new JButton("Cancel");
public TestSleep(){
Container con = getContentPane();
con.setLayout(new BorderLayout()); Runnable r = new Change(this,con);
Thread th = new Thread(r);
th.start();
setVisible(true);
}
public void InitUi(Container con){
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER));
p.add(bcn);
p.add(cancel);
con.add(p,BorderLayout.SOUTH);
addListener();
setVisible(true);
}
public void addListener(){
bcn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(" 确认 ");
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(" 退出 ");
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestSleep(); }}
class Change implements Runnable{
private TestSleep sl = null;
private Container con = null;
private int width = 0;
private int height = 0;
public Change(TestSleep sl,Container con){
this.sl = sl;
this.con = con;
}
public void run(){
while(true){
width = width + 20;
height = height + 10;
try{
Thread.currentThread().sleep(10);
}catch(InterruptedException e){
}
sl.setSize(width, height);
if(width >500){
break;
}
}
sl.InitUi(con); }
}