Applet 和 Swing 未在 Eclipse 中显示组件

Posted

技术标签:

【中文标题】Applet 和 Swing 未在 Eclipse 中显示组件【英文标题】:Applet and Swing not showing components in eclipse 【发布时间】:2016-03-17 18:27:49 【问题描述】:

我在 Eclipse 中将以下代码作为应用程序运行,它运行良好,但是当我尝试将其作为小程序运行时,我得到一个显示“按钮到文本”的窗口,但没有任何按钮出现。此外,还会弹出一个空的“小程序查看器”窗口。这是代码。

/**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】:

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

【讨论】:

感谢您的帮助。现在一切正常! 如果它有助于解决问题,请accept the answer。

以上是关于Applet 和 Swing 未在 Eclipse 中显示组件的主要内容,如果未能解决你的问题,请参考以下文章

java awt;java applet;java swing分别是啥?他们之间有啥联系和区别?

将简单的Applet GUI从Swing转换为JavaFX

跪求运用JAVA eclipse,求四则运算

Java Swing 1.6 更新 23 个使用 Applet 的 JVM 锁

求利用gdal 技术,java语言,对tif图片的放大,缩小,裁切代码,swing jsp,applet不限

java中怎样让swing倒入jsp网页中