自己写了一个类mybutton继承jbutton,加了几个方法
然后在带main的类里加了2组100个mybutton
结果运行的时候每组只出现一个button
其他的只有当鼠标划过的时候才会显现出来
我用myeclipse编的
怎么解决?
大谢~

解决方案 »

  1.   

    简单的问题,你的setVisible(true)没放到最后吧,把他放到最后就好了!
      

  2.   

    import java.awt.*;
    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个同样的方法!
    不知道这是不是楼主说的意思!
      

  3.   

    遇到过 基本应该是layout的问题
      

  4.   

    这问题我怎么记得我回答过?LZ你看看你的MyButton里面是不是把getX()和getY()这2个方法给覆盖掉了,如果是的话在你的MyButton里面把getX()和getY()这2个方法改下名字我始终觉得这问题我回答过
      

  5.   

    没有绘制成功。当鼠标经过时系统会向按钮发送repaint消息,这时才绘制按钮
      

  6.   

    这回可以显示组件了import java.awt.BorderLayout;
    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); }
    }