applet程序在浏览器中无法正常显示

这个问题困扰我很久,虽然它只是个小问题,现在记录一下。

 

系统win7,jdk1.8.0_131,浏览器QQ浏览器,火狐等等能想到的都是了一遍(都不行,郁闷)。

下面的这张图片是运行applet程序是需要用到的html文件和java源代码,以及在appletviewer中正常执行结果。

图1

 

而在浏览器中,applet片段无法显示,见下图。

图2

 

图3                

  推测结果:

(1)       我的JDK配置不正确

(2)       浏览器已经抛弃了applet,笑死。。。怎么可以这样。郁闷

求解答。


好的,以下是一个实现秒表功能的Applet程序的示例代码: ```java import java.applet.*; import java.awt.*; import java.awt.event.*; public class Stopwatch extends Applet implements Runnable, ActionListener { // 定义按钮和标签 Button startButton, stopButton, resetButton; Label timeLabel; // 定义线程和计时器变量 Thread timerThread; int timeCounter; // 初始化Applet界面 public void init() { // 设置界面布局 setLayout(new BorderLayout()); // 创建按钮和标签 startButton = new Button("Start"); stopButton = new Button("Stop"); resetButton = new Button("Reset"); timeLabel = new Label("00:00:00"); // 添加按钮和标签 add(timeLabel, BorderLayout.CENTER); Panel buttonPanel = new Panel(); buttonPanel.add(startButton); buttonPanel.add(stopButton); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.SOUTH); // 添加按钮事件监听器 startButton.addActionListener(this); stopButton.addActionListener(this); resetButton.addActionListener(this); } // 实现按钮事件处理 public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { // 启动线程 if (timerThread == null) { timerThread = new Thread(this); timerThread.start(); } } else if (e.getSource() == stopButton) { // 停止线程 if (timerThread != null) { timerThread.interrupt(); timerThread = null; } } else if (e.getSource() == resetButton) { // 重置计时器 timeCounter = 0; updateTimeLabel(); } } // 实现线程运行 public void run() { try { while (true) { Thread.sleep(1000); timeCounter++; updateTimeLabel(); } } catch (InterruptedException e) { // 线程被断,忽略异常 } } // 更新时间标签 private void updateTimeLabel() { int hours = timeCounter / 3600; int minutes = (timeCounter % 3600) / 60; int seconds = timeCounter % 60; timeLabel.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds)); } } ``` 该程序使用了Applet界面,包含了三个按钮和一个标签。通过点击“Start”按钮启动计时器线程,通过点击“Stop”按钮停止计时器线程,通过点击“Reset”按钮重置计时器。计时器的时间以“小时:分钟:秒”格式显示在标签。 注意:由于Java Applet已经被弃用,该示例代码可能无法在最新的浏览器正常运行。建议使用JavaFX或Swing等替代方案。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值