我可能出现的有你这种情况,原因是组件的放置方式,你在面扳属性中将layout的植改成xylayout

解决方案 »

  1.   

    我的代码主要是:
     public class HT extends Applet {
      JButton b1=new JButton(),
                  b2=new JButton();
      private boolean isStandalone = false;
      //Get a parameter value
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
          (getParameter(key) != null ? getParameter(key) : def);
      }  //Construct the applet
      public HT() {
      }
      //Initialize the applet
      public void init() {
        b1.setBounds(new Rectangle(100,100,250,250));
        b1.setOpaque(false);
      //  b1.setOpaque();
          b2.setBounds(new Rectangle(0,60,25,50));
          this.add(b1);
          this.add(b2);    try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public void Grap()
      {
      Graphics g=b1.getGraphics();  g.drawLine(100,100,150,150);
      }
      //Component initialization
      private void jbInit() throws Exception {
        this.setLayout(null);
      }
      //Get Applet information
      public String getAppletInfo() {
        return "Applet Information";
      }
      //Get parameter info
      public String[][] getParameterInfo() {
        return null;
      }
      //Main method
      public static void main(String[] args) {
        HT applet = new HT();
        applet.isStandalone = true;
        Frame frame;
        frame = new Frame();
        frame.setTitle("Applet Frame");
        frame.add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(400,320);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
        frame.setVisible(true);
        applet.Grap();
      }
    }