一个小程序,想用线程实现在jpanel上搞出一行button,但是我的程序做出来后button不会动,程序改了三小时了,未果,请高指点江山:代码如下
package awt.test;import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.*;public class AddButtonDemo { /**
* @param args
*/
public static void main(String[] args) {
textFrame text =new textFrame();
text.show(); }}
class textFrame extends JFrame{
textFrame(){
this.setTitle("测试用");
this.setSize(300, 400);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
JButton sb1 =new JButton("测试用button");
textPanel panel =new textPanel(sb1);
//panel.setLayout(new FlowLayout());
Container c = getContentPane();
c.setLayout(new GridLayout(2,2));
demoThread t1 = new demoThread(panel);
Thread t2 =new Thread(t1);
t2.start();
c.add(panel);
c.setVisible(true);
}
}class textPanel extends JPanel{
JButton jb1 = null;
textPanel(JButton jb){
jb1=jb;
this.add(jb1);
this.setLayout(new FlowLayout());
}
public void addButton(){
this.add(jb1);
jb1.updateUI();
}
private JButton getbutton(){
return jb1;
}
}class demoThread implements Runnable{
int i;
textPanel jb2=null;
textPanel jb3=null;
demoThread(textPanel jb){
jb2=jb;
}
public void run(){
while (i<=300){
jb2.addButton();
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}
package awt.test;import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.*;public class AddButtonDemo { /**
* @param args
*/
public static void main(String[] args) {
textFrame text =new textFrame();
text.show(); }}
class textFrame extends JFrame{
textFrame(){
this.setTitle("测试用");
this.setSize(300, 400);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
JButton sb1 =new JButton("测试用button");
textPanel panel =new textPanel(sb1);
//panel.setLayout(new FlowLayout());
Container c = getContentPane();
c.setLayout(new GridLayout(2,2));
demoThread t1 = new demoThread(panel);
Thread t2 =new Thread(t1);
t2.start();
c.add(panel);
c.setVisible(true);
}
}class textPanel extends JPanel{
JButton jb1 = null;
textPanel(JButton jb){
jb1=jb;
this.add(jb1);
this.setLayout(new FlowLayout());
}
public void addButton(){
this.add(jb1);
jb1.updateUI();
}
private JButton getbutton(){
return jb1;
}
}class demoThread implements Runnable{
int i;
textPanel jb2=null;
textPanel jb3=null;
demoThread(textPanel jb){
jb2=jb;
}
public void run(){
while (i<=300){
jb2.addButton();
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
}
楼主【wineer200】截止到2008-06-25 21:42:23的历史汇总数据(不包括此帖):
发帖数:5 发帖分:100
结贴数:0 结贴分:0
未结数:5 未结分:100
结贴率:0.00 % 结分率:0.00 %
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
JBuilder自动生成的代码太变态了,建议换Eclipse
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class AddButtonDemo
{
public static void main(String[] args)
{
TextFrame text = new TextFrame();
text.setVisible(true);
}
}class TextFrame extends JFrame
{
private static final long serialVersionUID = -6621100123461208936L; TextFrame()
{
this.setTitle("测试用");
this.setSize(300, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton sb1 = new JButton("测试用button");
TextPanel panel = new TextPanel(sb1);
// panel.setLayout(new FlowLayout());
Container c = getContentPane();
c.setLayout(new GridLayout(2, 2));
c.add(panel);
DemoThread t1 = new DemoThread(panel);
Thread t2 = new Thread(t1);
t2.start();
}
}class TextPanel extends JPanel
{
private static final long serialVersionUID = 7621320838424621994L;
JButton jb1 = null; TextPanel(JButton jb)
{
jb1 = jb;
this.add(jb1);
this.setLayout(new FlowLayout());
} public void addButton()
{
//这里的jb1永远都是你那个名称为“测试用button”的按钮,重复add如果不改变大小和位置的话,你是看不出有什么变化的
//this.add(jb1);
//改成这样子就行了,不过具体的还要看你的需求
this.add(new JButton("测试"));
jb1.updateUI();
}
}class DemoThread implements Runnable
{
int i; TextPanel jb2 = null; DemoThread(TextPanel jb)
{
jb2 = jb;
} public void run()
{
while(i <= 300)
{
jb2.addButton(); try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
i++;
}
}
}