Javafx Label 是不能用鼠标选中,复制的。有没有办法使Label 能,或有别的Object可以顶替Label呢

Javafx Label 是不能用鼠标选中,复制的。有没有办法使Label 能,或有别的Object可以顶替Label呢,第1张

效果如图

TextField文本框组件模拟Label

javaFX想实现文字的可选,可复制, 那么最简单的就是使用TextField组件,

步骤1: 通过css修去掉文本框组件的背景色, 来模仿Label ;

css代码:     -fx-background-color: #00000000;

步骤2: 文本框组件设置不可编辑,

      java代码      textFieldsetEditable(false);

使用javafx的Media,MediaPlayer,MediaView。代码如下:

import javaioFile;

import javafxapplicationApplication;

import javafxbeansbindingBindings;

import javafxbeanspropertyReadOnlyProperty;

import javafxbeansvalueObservableValue;

import javafxeventActionEvent;

import javafxgeometryPos;

import javafxsceneScene;

import javafxscenecontrolButton;

import javafxscenecontrolMenu;

import javafxscenecontrolMenuBar;

import javafxscenecontrolMenuItem;

import javafxscenecontrolSlider;

import javafxsceneimageImage;

import javafxsceneimageImageView;

import javafxscenelayoutBorderPane;

import javafxscenelayoutHBox;

import javafxscenelayoutPane;

import javafxscenelayoutVBox;

import javafxscenemediaMedia;

import javafxscenemediaMediaPlayer;

import javafxscenemediaMediaPlayerStatus;

import javafxscenemediaMediaView;

import javafxstageFileChooser;

import javafxstageFileChooserExtensionFilter;

import javafxstageStage;

import javafxutilDuration;

public class App extends Application {

private MediaView mediaView;

private Slider processSlider;

private static final Object AUTO = new Object(), MANUAL = new Object();

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage primaryStage) throws Exception {

primaryStagesetTitle("javafx视频播放器");

        

        BorderPane root = new BorderPane();

        // 菜单栏

        initMenu(root);

        // 视频播放控件

        mediaView = initMediaView(root);

        // 视频功能控件

        initMediaControllView(root);

        

        primaryStagesetScene(new Scene(root, 600, 400));

        primaryStagesetOnCloseRequest(event -> Systemexit(0));

        primaryStageshow();

}

private MenuBar initMenu(final BorderPane parent) {

        Menu menu = new Menu("菜单");

        MenuItem item = new MenuItem("打开");

        itemsetOnAction(event -> {

         FileChooser chooser = new FileChooser();

         choosersetTitle("选择视频文件");

         choosergetExtensionFilters()add(new ExtensionFilter("视频文件", "mp4", "flv"));

         File file = choosershowOpenDialog(parentgetScene()getWindow());

         if (file != null) {

         MediaPlayer player = mediaViewgetMediaPlayer();

         // 关闭当前的player

         if (player != null) {

         playerstop();

         playerdispose();

         }

         processSlidersetValue(0);

         // 创建一个新的player并自动播放

         player = new MediaPlayer(new Media(filetoURI()toString()));

         playercurrentTimeProperty()addListener(this::mediaProcessChanged);

         playersetAutoPlay(true);

         mediaViewsetMediaPlayer(player);

         }

        });

        menugetItems()add(item);

        

        item = new MenuItem("退出");

        itemsetOnAction(event -> parentgetScene()getWindow()hide());

        menugetItems()add(item);

        

        MenuBar menuBar = new MenuBar(menu);

        parentsetTop(menuBar);

        return menuBar;

}

private MediaView initMediaView(final BorderPane parent) {

        MediaView view = new MediaView();

        Pane pane = new Pane(view);

        parentsetCenter(pane);

        // 视频宽高可随着窗口变化而自动缩放

        viewfitWidthProperty()bind(BindingsselectDouble(viewparentProperty(), "width"));

        viewfitHeightProperty()bind(BindingsselectDouble(viewparentProperty(), "height"));

        viewsetPreserveRatio(true);

        

return view;

}

private void initMediaControllView(final BorderPane parent) {

VBox bottom = new VBox(10);

bottomsetAlignment(PosCENTER);

parentsetBottom(bottom);

Slider slider = new Slider(0, 100, 0);

slidervalueProperty()addListener(this::processSliderChanged);

// slidersetOnMouseClicked(this::processSliderClicked);

bottomgetChildren()add(slider);

processSlider = slider;

HBox hbox = new HBox(10);

bottomgetChildren()add(hbox);

hboxsetAlignment(PosCENTER);

Button btn = new Button(null, new ImageView(loadImage("pausepng")));

btnsetOnAction(this::pauseOrPlay);

hboxgetChildren()add(btn);

btn = new Button(null, new ImageView(loadImage("stoppng")));

btnsetOnAction(this::stopPlay);

hboxgetChildren()add(btn);

}

private static Image loadImage(String resPath) {

return new Image(ThreadcurrentThread()getContextClassLoader()getResourceAsStream(resPath));

}

private void pauseOrPlay(ActionEvent event) {

MediaPlayer player = mediaViewgetMediaPlayer();

if (player == null) {

return;

}

Status status = playergetStatus();

if (status == StatusREADY || status == StatusPAUSED) {

playerplay();

((Button) eventgetTarget())setGraphic(new ImageView(loadImage("pausepng")));

} else if (status == StatusPLAYING) {

playerpause();

((Button) eventgetTarget())setGraphic(new ImageView(loadImage("playpng")));

}

}

private void stopPlay(ActionEvent event) {

MediaPlayer player = mediaViewgetMediaPlayer();

if (player == null) {

return;

}

playerstop();

}

// private void processSliderClicked(MouseEvent event) {

// Slider slider = (Slider) eventgetSource();

// if (mediaViewgetMediaPlayer() != null) {

// Duration d = mediaViewgetMediaPlayer()getTotalDuration()multiply(slidergetValue() / slidergetMax());

// mediaViewgetMediaPlayer()seek(d);

// }

// }

private void processSliderChanged(ObservableValue< extends Number> observable, Number oldValue, Number newValue) {

Slider slider = (Slider) ((ReadOnlyProperty< extends Number>) observable)getBean();

if (slidergetUserData() == AUTO) { // 进度条是自动改变的,因此不用设置播放器

slidersetUserData(null);

return;

}

if (mediaViewgetMediaPlayer() != null) {

slidersetUserData(MANUAL);

Duration d = mediaViewgetMediaPlayer()getTotalDuration()multiply(newValuedoubleValue() / slidergetMax());

mediaViewgetMediaPlayer()seek(d);

}

}

private void mediaProcessChanged(ObservableValue< extends Duration> observable, Duration oldValue, Duration newValue) {

if (processSlidergetUserData() == MANUAL) { // 手动点击进度条

processSlidersetUserData(null);

return;

}

MediaPlayer player = (MediaPlayer) ((ReadOnlyProperty< extends Duration>) observable)getBean();

processSlidersetUserData(AUTO);

processSlidersetValue(newValuetoMillis() / playergetTotalDuration()toMillis()  100);

}

}

试试GridPane的这个方法:

public static void setHgrow(Node child,

Priority value)

Sets the horizontal grow priority for the child when contained by a gridpane

If set, the gridpane will use the priority to allocate the child additional

horizontal space if the gridpane is resized larger than it's preferred width

Setting the value to null will remove the constraint

Parameters:

child - the child of a gridpane

value - the horizontal grow priority for the child

(1)准备工作

1) 安装JDK 6或者JDK 7

2) 安装scala 210x (注意版本)

2)下载Intellij IDEA最新版(本文以IntelliJ IDEA Community Edition 1311为例说明,不同版本,界面布局可能不同)

3)将下载的Intellij IDEA解压后,安装scala插件,流程如下:

依次选择“Configure”–> “Plugins”–> “Browse repositories”,输入scala,然后安装即可

(2)搭建Spark源码阅读环境(需要联网)

一种方法是直接依次选择“import project”–> 选择spark所在目录 –>

“SBT”,之后intellij会自动识别SBT文件,并下载依赖的外部jar包,整个流程用时非常长,取决于机器的网络环境(不建议在windows

下 *** 作,可能遇到各种问题),一般需花费几十分钟到几个小时。注意,下载过程会用到git,因此应该事先安装了git。

第二种方法是首先在linux *** 作系统上生成intellij项目文件,然后在intellij IDEA中直接通过“Open

Project”打开项目即可。在linux上生成intellij项目文件的方法(需要安装git,不需要安装scala,sbt会自动下载)是:在

spark源代码根目录下,输入sbt/sbt gen-idea

注:如果你在windows下阅读源代码,建议先在linux下生成项目文件,然后导入到windows中的intellij IDEA中。

(3)搭建Spark开发环境

在intellij IDEA中创建scala project,并依次选择“File”–> “project structure”

–> “Libraries”,选择“+”,将spark-hadoop

对应的包导入,比如导入spark-assembly_210-090-incubating-hadoop220jar(只需导入该jar

包,其他不需要),如果IDE没有识别scala 库,则需要以同样方式将scala库导入。之后开发scala程序即可:

编写完scala程序后,可以直接在intellij中,以local模式运行,方法如下:

点击“Run”–> “Run Configurations”,在d出的框中对应栏中填写“local”,表示将该参数传递给main函数,如下图所示,之后点击“Run”–> “Run”运行程序即可。

如果想把程序打成jar包,通过命令行的形式运行在spark 集群中,可以按照以下步骤 *** 作:

依次选择“File”–> “Project Structure” –> “Artifact”,选择“+”–>

“Jar” –> “From Modules with

dependencies”,选择main函数,并在d出框中选择输出jar位置,并选择“OK”。

最后依次选择“Build”–> “Build Artifact”编译生成jar包。

public void play(String Filename)

{

try{

// 用输入流打开一音频文件

InputStream in = new FileInputStream(Filename);//FIlename 是你加载的声音文件如(“gamewav”)

// 从输入流中创建一个AudioStream对象

AudioStream as = new AudioStream(in);

AudioPlayerplayerstart(as);//用静态成员playerstart播放音乐

//AudioPlayerplayerstop(as);//关闭音乐播放

//如果要实现循环播放,则用下面的三句取代上面的“AudioPlayerplayerstart(as);”这句

/AudioData data = asgetData();

ContinuousAudioDataStream gg= new ContinuousAudioDataStream (data);

AudioPlayerplayerstart(gg);// Play audio

/

//如果要用一个 URL 做为声音流的源(source),则用下面的代码所示替换输入流来创建声音流:

/AudioStream as = new AudioStream (urlopenStream());

/

} catch(FileNotFoundException e){

Systemoutprint("FileNotFoundException ");

} catch(IOException e){

Systemoutprint("有错误!");

}

}

javaFx程序运行,光标(焦点)默认显示在最上行的TextField上。

这样我就无法看到输入框的提示字符

解决方法:

//注册

Button btn = new Button("登录");

HBox hBox = new HBox(10);

hBoxsetAlignment(PosBOTTOM_RIGHT);

hBoxgetChildren()add(btn);

gridadd(hBox, 2, 5);

Scene scene = new Scene(grid, 300, 275);

btnrequestFocus();             //让登录按钮获取焦点

btnrequestFocus(); //一定要在new Scene(grid, 300, 275)之后调用(聪明的你已经猜到使Scene初始化的缘由啦!谈不上好与坏,默认有光标不挺好的,没有的话,不显得javaFx比较2不是哈哈哈)

btnrequestFocus(); 替换为hBoxrequestFocus(); 则看不到光标啦!

需要的才是最好的!(第一次在百度上回答问题百度半天无结果啊哈哈哈2019年5月3日23点23分)

TableView的数据填充,需要一个ObservableList。其中需要一个类来做数据填充。

下面看看我们数据填充的类:

复制代码代码如下:

import javafxbeanspropertySimpleDoubleProperty;

import javafxbeanspropertySimpleStringProperty;

/

@author wing

/

public final class DownloadData {

private final SimpleStringProperty fileName = new SimpleStringProperty();

private final SimpleStringProperty status = new SimpleStringProperty();

private final SimpleStringProperty dlSpeed = new SimpleStringProperty();

private final SimpleDoubleProperty progress = new SimpleDoubleProperty();

private final SimpleStringProperty downloadSize = new SimpleStringProperty();

private final SimpleStringProperty dlPercent = new SimpleStringProperty();

private String uuid;

public DownloadData(String filename, double progress) {

setFileName(filename);

setProgress(progress);

}

public DownloadData(String status, String filename, String dlSpeed, double progress) {

setStatus(status);

setFileName(filename);

setDlSpeed(dlSpeed);

setProgress(progress);

}

/

@return the fileName

/

public String getFileName() {

return fileNameget();

}

/

@param fileName the fileName to set

/

public void setFileName(String fileName) {

thisfileNameset(fileName);

}

public SimpleStringProperty fileNameProperty(){

return fileName;

}

/

@return the status

/

public String getStatus() {

return statusget();

}

/

@param status the statusto set

/

public void setStatus(String status) {

thisstatusset(status);

}

public SimpleStringProperty statusProperty(){

return status;

}

/

@return the String

/

public String getDlSpeed() {

return dlSpeedget();

}

/

@param dlSpeed the dlSpeed to set

/

public void setDlSpeed(String dlSpeed) {

thisdlSpeedset(dlSpeed);

}

public SimpleStringProperty dlSpeedProperty(){

return dlSpeed;

}

/

@return the progress

/

public double getProgress() {

return progressget();

}

/

@param progress the progress to set

/

public void setProgress(double progress) {

thisprogressset(progress);

}

public SimpleDoubleProperty progressProperty(){

return progress;

}

public String getDownloadSize() {

return downloadSizeget();

}

public void setDownloadSize(String downloadSize) {

thisdownloadSizeset(downloadSize);

}

public SimpleStringProperty downloadSizeProperty(){

return downloadSize;

}

public String getDlPercent() {

return dlPercentget();

}

public void setDlPercent(String dlPercent) {

thisdlPercentset(dlPercent);

}

public SimpleStringProperty dlPercentProperty(){

return dlPercent;

}

public String getUUID() {

return uuid;

}

public void setUUID(String uuid) {

thisuuid = uuid;

}

}

记住,用作数据填充的类,一定要用JavaFX的Property机制,可以进行数据绑定,这样在我们改变ObservableList的时候,TableView的数据才会实时刷新。

复制代码代码如下:

private final ObservableList<DownloadData> data

= FXCollectionsobservableArrayList();

ObservableList<TableColumn> observableList = mDownloadTablegetColumns();

observableListget(0)setCellValueFactory(new PropertyValueFactory("status"));

observableListget(1)setCellValueFactory(new PropertyValueFactory("fileName"));

observableListget(2)setCellValueFactory(new PropertyValueFactory("dlSpeed"));

observableListget(3)setCellValueFactory(new PropertyValueFactory("downloadSize"));

observableListget(4)setCellValueFactory(new PropertyValueFactory("progress"));

observableListget(4)setCellFactory(ProgressBarTableCellforTableColumn());

observableListget(5)setCellValueFactory(new PropertyValueFactory("dlPercent"));

mDownloadTablesetItems(data);

我们通过TableViewgetColumns来获取TableView的所有列。

CellValueFactory指的是TableView每一列里填充的数据。我们这里简单的使用PropertyValueFacotry。后面的要对应你DownloadData中的Property属性名。

CellFactory我们可以指定TableView中某一个Cell的视图类型。大家可以看到我用到了个ProgressBar。

另外CellFactory,JavaFX中自带部分的CellFactory,详细的大家可以在javafxscenecontrolcell包中找到。

接着我们通过创建DownloadData,设置数据,并添加到ObservableList中即可。

jTextArea1;好像就这两个函数setSelectionEnd(pos)jTextArea1setSelectionStart(pos)arequestFocus看看textArea的属性里有

getCaretPosition();一个继承自Class TextComponent的方法也可以getSelectionStart();

(1):如果是刚刚打开一个窗口就要让textArea获得焦点,可以在打开窗口的事件里添加a。requesFocus

setSelectionStart

AWT/Swing中TextArea/JTextArea使用setCaretPosition方法

public void setCaretPosition(int position) The caret position is constrained to be between 0 and the last character of the text, inclusive

JavaFX中:

TextArea ta = new TextArea();tasetText("1234567890");tapositionCaret(4);

解决方案1:

jTextArea1;

好像就这两个函数吧setSelectionEnd(pos)。jTextArea1setSelectionStart(pos)

解决方案2:

arequestFocus

看看textArea的属性里有

如果是刚刚打开一个窗口就要让textArea获得焦点,可以在打开窗口的事件里添加a。requesFocus

解决方案3:

setSelectionStart

(2):Java如何设置按钮上的文字…… <input type=button value=这里设置需要的文字即可/> 定义和用法 <inpu

以上就是关于Javafx Label 是不能用鼠标选中,复制的。有没有办法使Label 能,或有别的Object可以顶替Label呢全部的内容,包括:Javafx Label 是不能用鼠标选中,复制的。有没有办法使Label 能,或有别的Object可以顶替Label呢、初学了一段时间Java。想做一个应用程序,程序只需要播放一个本地视频,MP4格式,求教怎么做、javafx 如何让ComboBox铺满GridPane的整列等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9303294.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存