Applet和Swing没有在eclipse中显示组件

时间:2015-12-11 18:40:10

标签: java eclipse swing applet components

我在eclipse中运行以下代码作为一个应用程序并且它完美地工作,但是当我尝试将其作为applet运行时,我得到了一个窗口,上面写着" Button to text"但没有按钮出现。另外,一个空的小程序查看器"窗户弹出。这是代码。

/**This program completes the requirements described
 * in Option 3 which is to create a frame that contains 3 buttons.
 * These buttons then will update the text shown on the frame.
 * The program uses AWT and Swing to accomplish this task.
 * For more information on the particular methods of this program, look below.
 * Press a button to change the text on the bottom to the text displayed on the button
 */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingOption3 extends Applet {

    private JFrame mainFrame;
    private JLabel header;
    private JLabel status;
    private JPanel control;
    //initialize the variables by creating them
    public SwingOption3(){
        prepareGUI();
        //this constructor runs the prepareGUI method
    }
public static void main(String[] args){
    SwingOption3 swingOption3 = new SwingOption3();
    swingOption3.showEvent();
    //creates an object from the main class and runs the showEvent method
}
/** The following method performs these actions
 * 1. Adds a title to the frame ("Button to Text")
 * 2. Sets the size to 400 by 400
 * 3. Uses the GridLayout to proportion the frame
 * 4. Initializes the header and status variables
 * 5. Adds a listener to the main frame
 * 6. Adds the header, status, and control to the main frame
 * 7. Sets the main frame to visible
 * 8. Sets the control layout to flow
 */
private void prepareGUI(){
    mainFrame = new JFrame("Button to Text");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3, 1));

    header = new JLabel("",JLabel.CENTER );
    status = new JLabel("", JLabel.CENTER);

    status.setSize(350, 100);
    mainFrame.addWindowListener(new WindowAdapter() {
        //this method allows the program window to be closed
        public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
        }
    });
    control = new JPanel();
    control.setLayout(new FlowLayout());

    mainFrame.add(header);
    mainFrame.add(control);
    mainFrame.add(status);
    mainFrame.setVisible(true);

}
/**The following method completes these tasks:
 * 1. Sets the text of the header to inform the user what they should do
 * 2. Adds the buttons to the JPanel "control"
 * 3. Creates the listener methods and describes the ActionEvent 
 * that will be sent for each button press
 **/
private void showEvent(){
    header.setText("Press a button to change the text at the bottom");
    //informs the user to press a button in order to change the text
    JButton appleButton = new JButton("Apple");
    JButton pearButton = new JButton("Pear");
    JButton bananaButton = new JButton("Banana");
    //creates the buttons and titles them
    appleButton.setActionCommand("Apple");
    pearButton.setActionCommand("Pear");
    bananaButton.setActionCommand("Banana");
    //sets the command that will be received and interpreted by the program
    appleButton.addActionListener(new ButtonClickListener());
    pearButton.addActionListener(new ButtonClickListener());
    bananaButton.addActionListener(new ButtonClickListener());
    //creates listeners for the button clicks
    control.add(appleButton);
    control.add(pearButton);
    control.add(bananaButton);
    //adds the buttons to the JPanel "control"
    mainFrame.setVisible(true);
    //makes the frame visible
}
private class ButtonClickListener implements ActionListener {
    /**
     * This method performs the following actions
     * 1.Receives the command/ActionEvent from the previous method
     * 2. Displays the appropriate text based on the ActionEvent
     */
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(command.equals("Apple")) {
            status.setText("Display: Apple");
        } //sets the display to "Display: Apple"
        else if( command.equals("Pear")) {
            status.setText("Display: Pear");
        } //sets the display to "Display: Pear"
        else {
            status.setText("Display: Banana");
        } //sets the display to "Display: Banana"
    }
}
}
`

我不太确定我哪里出错了所以我发布了整件事。感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

您不在applet中创建新的JFrame,而是使用applet本身作为控件的容器。并且你的构造函数不会调用showEvent() - 其他控件永远不会被实例化。