Событие мыши - Java
Формулировка задачи:
Добрый день всем.
Ребята ответе на вопрос.
Создаю динамически Labelы как сделать чтобы при наведении на них мишью они меняли цвет.
Заранее спасибо.
Вот мой код
Листинг программы
- private void ShowPrice(){
- sp=new Pane();
- sp.setLayoutX(8);
- sp.setLayoutY(60);
- sp.setPrefSize(550,700);
- sp.setStyle("-fx-border-width:2pt;-fx-border-color:olive;-fx-background-color:lightgrey;");
- anhorPanePrice.getChildren().add(sp);
- for(int i=0;i<6;i++){
- for(int j=0;j<pr.getParsingFirm().size();j++){
- Label mylabel=new Label();
- mylabel.setLayoutY(40+j*30);
- mylabel.setPrefSize(200,30);
- mylabel.getOnMouseClicked();
- switch(i){
- case 0:
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#000066;");
- mylabel.setLayoutX(10);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("Название фирм");
- }else{
- mylabel.setText(pr.getParsingFirm().get(j).toString());
- }
- break;
- case 1:
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#a4aa16;");
- mylabel.setLayoutX(210);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("A-92");
- }else{
- mylabel.setText(pr.getParsingA92().get(j).toString());
- }
- break;
- case 2:
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:GREEN;");
- mylabel.setLayoutX(270);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("A-95");
- }else{
- mylabel.setText(pr.getParsingA95().get(j).toString());
- }
- break;
- case 3: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:RED;");
- mylabel.setLayoutX(330);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("A-95+");
- }else{
- mylabel.setText(pr.getParsingA95P().get(j).toString());
- }
- break;
- case 4: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:BLACK;");
- mylabel.setLayoutX(390);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("ДТ");
- }else{
- mylabel.setText(pr.getParsingDT().get(j).toString());
- }
- break;
- case 5: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#7d125a;");
- mylabel.setLayoutX(450);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("Газ");
- }else{
- mylabel.setText(pr.getParsingGas().get(j).toString());
- }
- break;
- }
- sp.getChildren().add(mylabel);
- }
- }
- }
Решение задачи: «Событие мыши»
textual
Листинг программы
- package price1;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javafx.fxml.FXML;
- import javafx.scene.control.*;
- import javafx.scene.layout.AnchorPane;
- import javafx.scene.layout.Pane;
- import javafx.scene.web.WebView;
- import javax.script.*;
- /**
- *
- * @author Миха
- */
- public class FXMLDocumentController implements MouseListener {
- @FXML
- private Label label;
- @FXML
- private AnchorPane anhorPanePrice;
- @FXML
- private ProgressBar prog;
- @FXML
- private ComboBox comboboxFirm;
- @FXML
- private ComboBox comboboxCountry;
- @FXML
- private AnchorPane anchorPane;
- private Parsing_price pr;
- private Pane sp;
- private WebView wv;
- private Label mylabel;
- @FXML
- private void handleComboboxTextChanget(){
- String country=comboboxCountry.getValue().toString();
- pr=new Parsing_price();
- pr.Parsing_Country(country);
- pr.Parsing_Firm();
- pr.Parsing_Oil();
- pr.Parsing_City();
- ShowPrice();
- AddComboboxFirm();
- try {
- WebMap();
- } catch (ScriptException | NoSuchMethodException ex) {
- }
- Price_DataBase db=new Price_DataBase();
- }
- private void AddComboboxFirm(){
- comboboxFirm.getItems().clear();
- for(int i=1;i<pr.getParsingFirm().size();i++){
- comboboxFirm.getItems().addAll(pr.getParsingFirm().get(i));
- }
- }
- @FXML
- private void handleComboboxValueFirm(){
- }
- private void ShowPrice(){
- sp=new Pane();
- sp.setLayoutX(8);
- sp.setLayoutY(60);
- sp.setPrefSize(550,700);
- sp.setStyle("-fx-border-width:2pt;-fx-border-color:olive;-fx-background-color:lightgrey;");
- anhorPanePrice.getChildren().add(sp);
- for(int i=0;i<6;i++){
- for(int j=0;j<pr.getParsingFirm().size();j++){
- mylabel=new Label();
- mylabel.setLayoutY(40+j*30);
- mylabel.setPrefSize(200,30);
- // mylabel.addMouseListener(this);
- switch(i){
- case 0:
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#000066;");
- mylabel.setLayoutX(10);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("Название фирм");
- }else{
- mylabel.setText(pr.getParsingFirm().get(j).toString());
- }
- break;
- case 1:
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#a4aa16;");
- mylabel.setLayoutX(210);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("A-92");
- }else{
- mylabel.setText(pr.getParsingA92().get(j).toString());
- }
- break;
- case 2:
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:GREEN;");
- mylabel.setLayoutX(270);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("A-95");
- }else{
- mylabel.setText(pr.getParsingA95().get(j).toString());
- }
- break;
- case 3: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:RED;");
- mylabel.setLayoutX(330);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("A-95+");
- }else{
- mylabel.setText(pr.getParsingA95P().get(j).toString());
- }
- break;
- case 4: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:BLACK;");
- mylabel.setLayoutX(390);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("ДТ");
- }else{
- mylabel.setText(pr.getParsingDT().get(j).toString());
- }
- break;
- case 5: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#7d125a;");
- mylabel.setLayoutX(450);
- if(j==0){
- mylabel.setLayoutY(10);
- mylabel.setText("Газ");
- }else{
- mylabel.setText(pr.getParsingGas().get(j).toString());
- }
- break;
- }
- sp.getChildren().add(mylabel);
- }
- }
- }
- private void WebMap() throws ScriptException, NoSuchMethodException{
- System.out.println("Hello");
- wv=new WebView();
- wv.setLayoutX(580);
- wv.setLayoutY(60);
- wv.setPrefSize(700,700);
- wv.setStyle("-fx-border-width:2pt;-fx-border-color:olive;-fx-background-color:lightgrey;");
- wv.getEngine().load("file:///E:/Java/%D0%9F%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D1%8B/Price_oil/MyMap.html");
- anhorPanePrice.getChildren().add(wv);
- ScriptEngineManager manager = new ScriptEngineManager();
- ScriptEngine engine = manager.getEngineByName("JavaScript");
- // JavaScript code in a String
- String script = "function hello(name) { print('Hello, ' + name); }";
- // evaluate script
- engine.eval(script);
- // javax.script.Invocable is an optional interface.
- // Check whether your script engine implements or not!
- // Note that the JavaScript engine implements Invocable interface.
- Invocable inv = (Invocable) engine;
- // invoke the global function named "hello"
- inv.invokeFunction("hello", "Scripting!!" );
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- if (e.getSource().equals(mylabel)) {
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:RED;");
- }
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // for(int i=0;i<pr.getParsingFirm().size();i++)
- if (e.getSource().equals(mylabel)) {
- mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:BLUE;");
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д