给我编写一个程序,创建对象和类,利用javafx实现电视播放功能,至少有5个频道,电视没有打开的时候是黑屏显示。
时间: 2024-06-10 20:05:58 浏览: 22
好的,根据您的要求,我可以编写以下的JavaFX程序来实现电视播放功能,并且可以添加至少5个频道:
```
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
public class TVPlayer extends Application {
private Rectangle screen;
private Text channelName;
public class Channel {
private String name;
private String url;
public Channel(String name, String url) {
this.name = name;
this.url = url;
}
public String getName() {
return name;
}
public String getURL() {
return url;
}
}
public class TV {
private Channel channel;
private boolean isOn;
public TV() {
channel = null;
isOn = false;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
public Channel getChannel() {
return channel;
}
public void turnOn() {
isOn = true;
updateScreen();
}
public void turnOff() {
isOn = false;
screen.setFill(Color.BLACK);
channelName.setText("");
}
public boolean isOn() {
return isOn;
}
private void updateScreen() {
if (isOn && channel != null) {
screen.setFill(Color.WHITE);
channelName.setText(channel.getName());
} else {
screen.setFill(Color.BLACK);
channelName.setText("");
}
}
}
@Override
public void start(Stage stage) {
TV tv = new TV();
// Create some channels
Channel channel1 = new Channel("CNN", "http://stream.com/cnn");
Channel channel2 = new Channel("NBC", "http://stream.com/nbc");
Channel channel3 = new Channel("ABC", "http://stream.com/abc");
Channel channel4 = new Channel("Fox News", "http://stream.com/foxnews");
Channel channel5 = new Channel("CBS", "http://stream.com/cbs");
// Create some buttons to change channels
Button button1 = new Button("CNN");
Button button2 = new Button("NBC");
Button button3 = new Button("ABC");
Button button4 = new Button("Fox News");
Button button5 = new Button("CBS");
// Set up the TV screen and channel display text
screen = new Rectangle(0, 0, 640, 480);
screen.setFill(Color.BLACK);
channelName = new Text(10, 20, "");
// Add the screen and channel display to the pane
Pane pane = new Pane();
pane.getChildren().addAll(screen, channelName);
// Add the buttons and attach event handlers to change channels
pane.getChildren().addAll(button1, button2, button3, button4, button5);
button1.setOnAction(e -> tv.setChannel(channel1));
button2.setOnAction(e -> tv.setChannel(channel2));
button3.setOnAction(e -> tv.setChannel(channel3));
button4.setOnAction(e -> tv.setChannel(channel4));
button5.setOnAction(e -> tv.setChannel(channel5));
// Turn on the TV by default
tv.turnOn();
// Set up the stage with the pane and show it
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序实现了一个`TVPlayer`类,其中包含`Channel`和`TV`类。`Channel`类表示一个电视频道,有一个名字和一个URL表示该频道的流地址。`TV`类表示一个电视机,在电视机上显示一个频道,并且可以开和关,也可以切换频道。程序创建了5个默认频道,并且添加了5个按钮来切换频道。当电视机开启时,会在黑屏上显示白色屏幕,并且显示当前频道的名字。当电视机关闭时,会重新变为黑屏,不显示任何内容。