import javax.swing.*;
public class test extends JApplet
{
JButton button=new JButton("测试");
public void init()
{
button.setSize(60,60);
getContentPane().add(button);
}
}

解决方案 »

  1.   

    This is because your layout.
    You can try button.setBorder(x, y, width, height)
      

  2.   

    setBorder(x,y,width,height)好像没有这个方法吧!
    即使有和我的要求也不一样!我不想自己去设置位置,只想自己设置大小!!!!
    应该怎么办!
      

  3.   

    先setLayOut(null);
    然后再设置组件大小应该就可以了
      

  4.   

    不行啊!没有布局管理器.
    你加个JScrollPane都没有用了!
    而且还有很多东东,写了也不起作用...
    我就是想用布局管理器还布局,然后自己可能修改它的大小!!!
    可不可以!
      

  5.   

    把内容面板的布局管理器改为NULL试试,
    然后 button 好象有个 setBound(x, y, width, height)
      

  6.   

    button.setPreferredSize(new Dimension(80, 30));
      

  7.   

    /**
    *这个是我的程序.要的就是要这样的功能:
    *用布局管理器布局,然后自己设置大小!因为如果不设置大小不一的话,当用户比较少的时候,按钮
    *就会变很大!所以想设置按钮的大小!
    *谢谢!
    */
    package mychat.client.windows;import mychat.client.main.ClientMain;
    import mychat.data.*;import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    //import java.util.Date;public class WinUser extends JFrame implements ActionListener,ComponentListener
    {
     ClientMain main;
     JButton user_b;
     int usernum=0;
     JScrollPane sp;
     JPanel userPanel;
     GridLayout layout;
     int width;
     public WinUser()
     {
      super("MyChat - v 1.0");
      layout=new GridLayout(1,1);
      userPanel=new JPanel(layout);
      sp=new JScrollPane(userPanel);
     }
     public WinUser(ClientMain main)
     {
      super("MyChat - v 1.0");
      layout=new GridLayout();
      userPanel=new JPanel(layout);
      sp=new JScrollPane(userPanel);
      this.main=main;
     }
     public void init()
     {
      width=getWidth();
      setSize(200,550);
      addWindowListener(new closewindow());
      addComponentListener(this);
      getContentPane().add(sp);
      //setResizable(false);
      setVisible(true);
     }
     /**
     *添加用户
     *@param u 为系统发来的ChatData数据包
     */
     public void addUser(ChatData u)
     {
      System.out.println("添加用户");
      layout.setRows(++usernum);
      userPanel.setLayout(layout);
      user_b=new JButton(u.getSender().getName()+" "+u.getText());
      user_b.setSize(getWidth()-12,30);
      user_b.addActionListener(this);
      user_b.setPreferredSize(new Dimension(80, 30));
      userPanel.add(user_b);
      repaint();
      System.out.println("添加用户完毕");
     }
     /**
     *删除用户
     *@param u 为系统发来的ChatData数据包
     */
     public void delUser(ChatData u)
     {}
     /**
     *添加聊天信息
     *@param u 为系统发来的ChatData数据包
     */
     public void addChatText(ChatData u)
     {}
     //main()
     public static void main(String args[])
     {
      new WinUser().init();
     }
     /**
     *重载paint方法
     */
     public void paint(Graphics g)
     {
      layout.setRows(usernum);
      Component[] cs=userPanel.getComponents();
      for(int i=0;i<cs.length;i++)
      cs[i].setSize(getWidth()-12,30);
      userPanel.setLayout(layout);
      sp.setViewportView(userPanel);
     /*
     //或者是
     sp=new JScrollPane(userPanel);
     */
     super.paint(g);
     }
     //listener
     public void actionPerformed(ActionEvent e)
     {
      System.out.println(e.getActionCommand());
     }
     class closewindow extends WindowAdapter
     {
      public void windowClosing (WindowEvent e)
      {
      System.exit(0);
      }
     }
     public void componentResized(ComponentEvent e)
     {
      width=getWidth();
      System.out.println(width);
      repaint();
     }
     public void componentShown(ComponentEvent e)
     {repaint();}
     public void componentMoved(ComponentEvent e)
     {repaint();}
     public void componentHidden(ComponentEvent e)
     {repaint();}
    }