Applet 和 Swing 未在 Eclipse 中显示组件

     2023-02-23     119

关键词:

【中文标题】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。

Swing JLayeredPane 未在 paintAll 调用上绘制所有元素

】SwingJLayeredPane未在paintAll调用上绘制所有元素【英文标题】:SwingJLayeredPanenotpaintingallelementsonpaintAllcall【发布时间】:2016-03-2206:35:17【问题描述】:我有JLayeredPane,它在0级包含Canvas(在Paint方法中填充自己的黄色)和JPanel在1级... 查看详情

java中的applet是啥?

Applet是一种特殊的Java程序。它本身不能单独运行,需要嵌入在一个HTML文件中,借助浏览器或Appletviewer来解释执行,Applet可能通过网络传输,由浏览器自动装载并执行。要浏览Applet需要将它嵌入在网页中,只需在HTML文件中插入<... 查看详情

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

】JavaSwing1.6更新23个使用Applet的JVM锁【英文标题】:JavaSwing1.6update23JVMLocksusingApplet【发布时间】:2011-09-0510:48:04【问题描述】:我目前有一个小程序,但最近才发现它存在问题,有时整个JVM会锁定并变得无响应,包括Java控制台。... 查看详情

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

参考技术AApplet有这功能,说不清楚的。这是介绍:Applet(小应用程序)采用Java创建的基于HTML的程序。浏览器将其暂时下载到用户的硬盘上,并在Web页打开时在本地运行。一般的Applet只能通过appletviewer或者浏览器来运行,一般的... 查看详情

在 Eclipse 上按“运行”时无法(同时)启动游戏和音乐

】在Eclipse上按“运行”时无法(同时)启动游戏和音乐【英文标题】:Troublelaunchinggameandmusic(atthesametime)whenpressing\'run\'onEclipse【发布时间】:2019-03-2919:07:54【问题描述】:我有这个播放音乐的代码(在网上找到):importjava.applet.... 查看详情

将简单的appletgui从swing转换为javafx

我正在尝试创建一个applet,它将用一个漂亮的JavaFXGUI取代令人困惑的CLI。我没有丰富的GUI设计经验,我们在课堂环境中学到的大部分都是Swing/AWT。玩了一点JavaFX后,我觉得它已经表现得好多了,但是没有很多关于创建打算作为app... 查看详情

Eclipse 找不到 javax.swing

】Eclipse找不到javax.swing【英文标题】:Eclipsedoesn\'tfindjavax.swing【发布时间】:2012-12-2007:23:52【问题描述】:Eclipse找不到importjavax.swing.JOptionPane它在哪里,我如何包含它?importjavax.swing.JOptionPane;【问题讨论】:它包含在JDK中。发布... 查看详情

关于java中的,java.awt和java.awt

...好,现在我们就来看一下。以前你设计小应用程序会扩展Applet(java.applet.Applet);现在为了使用Swing包,你要扩展JApplet(javax.swing.JApplet)让我们先来看一个扩展JApplet小应用程序的实例,然后再分析二者的区别。程序源代吗如下... 查看详情

组件的 Java Applet GUI 问题

】组件的JavaAppletGUI问题【英文标题】:JavaAppletGUIproblemwithcomponents【发布时间】:2011-03-2516:44:35【问题描述】:我在将GUI组件放入小程序时遇到了问题。我正在寻找一种使用绝对坐标和大小来放置它的方法。这是我所做的:publicc... 查看详情

如何在 Eclipse Swing/maven 和 Androidstudio/gradle 项目之间共享代码?

】如何在EclipseSwing/maven和Androidstudio/gradle项目之间共享代码?【英文标题】:HowtosharecodebetweenEclipseSwing/mavenandAndroidstudio/gradleproject?【发布时间】:2017-10-1907:55:28【问题描述】:我是一位经验丰富的Java开发人员,对Eclipse和Maven有... 查看详情

主要功能未在 Pydev Eclipse 中执行

】主要功能未在PydevEclipse中执行【英文标题】:MainfunctionnotexecutinginPydevEclipse【发布时间】:2021-10-2009:33:28【问题描述】:当我在Pydev中运行代码时,主函数没有执行。我没有看到任何错误。我正在使用Pyton-3.9、Eclipse4.2和PyDev8.3... 查看详情

让 java.policy.applet 在 Eclipse 之外工作

】让java.policy.applet在Eclipse之外工作【英文标题】:Gettingjava.policy.appletworkingoutsideofeclipse【发布时间】:2012-01-1321:43:21【问题描述】:这应该是一个相对简单的修复,但我找不到如何让它工作。我创建了一个java小程序,它在eclipse... 查看详情

将applet应用程序迁移到java11

我有一个基于JavaApplets,Swing和AWT的应用程序。我想将源代码迁移到Java11。不支持Java11Applet,甚至删除了JavaWebStart。所以我有以下问题1)我应该如何迁移我的应用程序?2)带有applet代码的现有应用程序是否可以在Java11中运行?示... 查看详情

将桌面应用程序移植到 Applet 沙盒

】将桌面应用程序移植到Applet沙盒【英文标题】:PortingDesktopApptoAppletSandbox【发布时间】:2012-08-0707:58:25【问题描述】:我有一个旧的Swing桌面应用程序,我想将其转换为小程序,并将所有文件IO替换为Web服务调用。我正在努力寻... 查看详情

Qt 设计器插件未在 Eclipse CDT 中加载

】Qt设计器插件未在EclipseCDT中加载【英文标题】:QtdesignerplugindoesnotloadinEclipseCDT【发布时间】:2009-12-2220:26:42【问题描述】:我已经安装了Qwt小部件,它们在QtCreator和QT4Designer中运行良好;但是,当我通过EclipseCDT运行QTDesigner时... 查看详情

java中swt是否已经过时了,没有swing好用哦?

...kit)说实话,swt和swing都不流行,主要两个代表性的软件,eclipse和netbeans,swt除了开发eclipse插件外,其他情况很少单独用到(至少国内是这样的),swing做为jdk扩展库的一部分,学习资料很多,会的人相对而言多一些,而应用到具... 查看详情

java程序设计图形用户界面

...1.4使用Graphics2D类绘图2.Swing基础2.1JFC与Swing2.2Swing与AWT2.3在Applet和Application中应用Swing1.绘图1.1图形环境和图形对象坐标GUI组件的左上角坐标默认为(0,0) 查看详情

Sonarlink 插件未在 Eclipse 中安装

】Sonarlink插件未在Eclipse中安装【英文标题】:Sonarlinkpluginnotgettinginstalledineclipse【发布时间】:2021-12-0415:57:35【问题描述】:我正在尝试从Eclipse市场安装sonarlint插件。安装后,它显示为已安装在eclipse市场中。但是,重新启动eclip... 查看详情