小程序组件不可行

时间:2012-06-12 13:11:18

标签: java applet awt null-layout-manager

登录后我正在调用Applet类。表格是可见的,但组件是不可见的,问题是什么?这是我的班级:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stanacle;


import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Calendar;
import javax.swing.JOptionPane;
/**
*
* @author Stano
*/
public class MenuOption extends JApplet{
Label header, menu, error, user;
TextField option;
Button go;

/**
 * Initialization method that will be called after the applet is loaded
 * into the browser.
 */
public void init() {
    // TODO start asynchronous download of heavy 
    setSize(1024, 768);
    setLayout(null);
    setVisible(true);
    header = new Label();
        menu = new Label();
        error = new Label();
        user = new Label();
        option = new TextField();
        go = new Button("GO");
        header.setBounds(370, 10, 200, 20);
        menu.setBounds(10, 60, 200, 20);
        option.setBounds(150, 60, 200, 20);
        go.setBounds(750, 10, 150, 30);
        error.setBounds(370, 310, 500, 20);
        user.setBounds(10, 10, 200, 20);
        header.setText("MENU OPTION PAGE");
        menu.setText("Enter Menu Option");


        add(header);
        add(menu);
        add(option);
        add(go);
        add(error);
        add(user);
}
// TODO overwrite start(), stop() and destroy() methods
}

表单正在显示,但包含TextField,Labeld和Button的组件未显示。我做错了什么?

1 个答案:

答案 0 :(得分:1)

1)代码行

setSize(1024, 768);
setLayout(null);
setVisible(true);

必须是public void init() {的最后一行代码,因为您显示容器然后添加J/Components

2)如果没有真正的原因(OpenGL,CAD / CAM)则使用

3)不要使用NullLayout