实现电视音量的加减

实现电视音量的加减

代码如下:

注:一个程序分为两个文件

第一部分
package Day05;

public class TV {
int channel = 1;//1<=channel<=120
int volumeLevel = 50;// 0<=volumeLevel<=100
boolean on = false;
/**
*
*/
public TV() {
}
/**
* @param channel
* @param volumeLevel
* @param on
*/
public TV(int channel, int volumeLevel, boolean on) {
this.channel = channel;
this.volumeLevel = volumeLevel;
this.on = on;
} public void turnOn() {
this.on = true;
} public void turnOff() {
this.on = false;
} public int getChannel() {
return channel;
}
public void setChannel(int channel) {
if (on && channel >= 1 && channel <= 120) {
this.channel = channel;
}
}
public int getVolumeLevel() {
return volumeLevel;
}
public void setVolumeLevel(int volumeLevel) {
if (on && volumeLevel >= 0 && volumeLevel <= 100) {
this.volumeLevel = volumeLevel;
}
} public void channelUp() {
if (channel < 120 && this.on) {
this.channel++;
}
} public void channelDown() {
if (channel > 1 && this.on) {
this.channel--;
}
} public void volumeUp() {
if (volumeLevel > 0 && this.on) {
this.volumeLevel++;
}
} public void volumeDown() {
if (volumeLevel > 0 && this.on) {
this.volumeLevel--;
}
}
}
第二部分
package Day05; public class testTV { public static void main(String[] args) {
TV tv1 = new TV();
tv1.turnOn();
tv1.setChannel(30);
tv1.setVolumeLevel(10);

System.out.println("tv1 channel: " + tv1.channel);
System.out.println("tv1 volume: " + tv1.volumeLevel);
System.out.println("tv1 on: " + tv1.on);

TV tv2 = new TV(10, 30, true);
System.out.println("tv2 channel: " + tv2.channel);
System.out.println("tv2 volume: " + tv2.volumeLevel);
System.out.println("tv2 on: " + tv2.on);
} }

转载于:https://www.cnblogs.com/F001li/p/7055771.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
调节功能 HTML部分: ```html <audio src="music.mp3" controls></audio> <div class="volume-container"> <div class="volume-slider"></div> <div class="volume-icon"></div> </div> ``` jQuery部分: ```javascript $(document).ready(function() { var audio = $("audio")[0]; var volumeSlider = $(".volume-slider"); var volumeIcon = $(".volume-icon"); // 初始化音量为50% audio.volume = 0.5; volumeSlider.css("width", audio.volume * 100 + "%"); // 点击音量图标切换静音 volumeIcon.click(function() { if (audio.volume > 0) { audio.volume = 0; volumeIcon.addClass("mute"); } else { audio.volume = 0.5; volumeIcon.removeClass("mute"); } volumeSlider.css("width", audio.volume * 100 + "%"); }); // 拖动音量条调整音量 volumeSlider.slider({ range: "min", min: 0, max: 1, value: audio.volume, step: 0.01, slide: function(event, ui) { audio.volume = ui.value; if (audio.volume > 0) { volumeIcon.removeClass("mute"); } else { volumeIcon.addClass("mute"); } } }); }); ``` CSS部分: ```css .volume-container { position: relative; width: 100px; height: 10px; margin: 10px auto; background-color: #ccc; border-radius: 5px; } .volume-slider { position: absolute; top: 0; left: 0; bottom: 0; width: 50%; background-color: #333; border-radius: 5px; } .ui-slider-horizontal { height: 10px; background-color: transparent; border: none; } .ui-slider-handle { width: 16px; height: 16px; margin-top: -3px; background-color: #333; border: none; border-radius: 50%; box-shadow: 0 0 3px 1px rgba(0, 0, 0, 0.3); } .volume-icon { position: absolute; top: 50%; right: -8px; margin-top: -8px; width: 16px; height: 16px; background-image: url(volume.png); background-repeat: no-repeat; } .volume-icon.mute { background-image: url(mute.png); } ``` 说明: 1. 在HTML中使用`<audio>`标签来播放音频,并添加`controls`属性来显示控制面板。 2. 使用`<div>`标签来创建音量调节器的容器。 3. 使用`.volume-slider`来表示音量调节器的滑块,使用`.volume-icon`来表示音量图标。 4. 在jQuery中获取`<audio>`标签对象和音量调节器的滑块、图标对象。 5. 初始化音量为50%。 6. 点击音量图标可以切换静音和恢复音量,同时更新音量调节器的滑块。 7. 使用jQuery UI的`slider`方法来创建音量调节器的滑块,设置最小值为0,最大值为1,初始值为当前音量,步长为0.01。 8. 在拖动滑块时更新音量,同时判断是否为静音状态并更新音量图标。 9. 根据需要自定义CSS样式,其中`.ui-slider-horizontal`、`.ui-slider-handle`为jQuery UI自动生成的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值