繁体   English   中英

Applet和Swing在Eclipse中未显示组件

[英]Applet and Swing not showing components in eclipse

我已经在eclipse中将以下代码作为应用程序运行,并且可以完美运行,但是当我尝试将其作为applet运行时,我看到了一个显示“按钮到文本”的窗口,但是没有按钮出现。 此外,将弹出一个空的“小程序查看器”窗口。 这是代码。

/**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"
    }
}
}
`

我不太确定我要去哪里错,所以我把整个事情都贴了。 我感谢您的帮助。

您不会在applet中创建新的JFrame,而是将applet本身用作控件的容器。 而且您的构造函数不会调用showEvent()-从未实例化其他控件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM