Событие мыши - Java

Узнай цену своей работы

Формулировка задачи:

Добрый день всем. Ребята ответе на вопрос. Создаю динамически Labelы как сделать чтобы при наведении на них мишью они меняли цвет. Заранее спасибо. Вот мой код
Листинг программы
  1. private void ShowPrice(){
  2. sp=new Pane();
  3. sp.setLayoutX(8);
  4. sp.setLayoutY(60);
  5. sp.setPrefSize(550,700);
  6. sp.setStyle("-fx-border-width:2pt;-fx-border-color:olive;-fx-background-color:lightgrey;");
  7. anhorPanePrice.getChildren().add(sp);
  8. for(int i=0;i<6;i++){
  9. for(int j=0;j<pr.getParsingFirm().size();j++){
  10. Label mylabel=new Label();
  11. mylabel.setLayoutY(40+j*30);
  12. mylabel.setPrefSize(200,30);
  13. mylabel.getOnMouseClicked();
  14. switch(i){
  15. case 0:
  16. mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#000066;");
  17. mylabel.setLayoutX(10);
  18. if(j==0){
  19. mylabel.setLayoutY(10);
  20. mylabel.setText("Название фирм");
  21. }else{
  22. mylabel.setText(pr.getParsingFirm().get(j).toString());
  23. }
  24. break;
  25. case 1:
  26. mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#a4aa16;");
  27. mylabel.setLayoutX(210);
  28. if(j==0){
  29. mylabel.setLayoutY(10);
  30. mylabel.setText("A-92");
  31. }else{
  32. mylabel.setText(pr.getParsingA92().get(j).toString());
  33. }
  34. break;
  35. case 2:
  36. mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:GREEN;");
  37. mylabel.setLayoutX(270);
  38. if(j==0){
  39. mylabel.setLayoutY(10);
  40. mylabel.setText("A-95");
  41. }else{
  42. mylabel.setText(pr.getParsingA95().get(j).toString());
  43. }
  44. break;
  45. case 3: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:RED;");
  46. mylabel.setLayoutX(330);
  47. if(j==0){
  48. mylabel.setLayoutY(10);
  49. mylabel.setText("A-95+");
  50. }else{
  51. mylabel.setText(pr.getParsingA95P().get(j).toString());
  52. }
  53. break;
  54. case 4: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:BLACK;");
  55. mylabel.setLayoutX(390);
  56. if(j==0){
  57. mylabel.setLayoutY(10);
  58. mylabel.setText("ДТ");
  59. }else{
  60. mylabel.setText(pr.getParsingDT().get(j).toString());
  61. }
  62. break;
  63. case 5: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#7d125a;");
  64. mylabel.setLayoutX(450);
  65. if(j==0){
  66. mylabel.setLayoutY(10);
  67. mylabel.setText("Газ");
  68. }else{
  69. mylabel.setText(pr.getParsingGas().get(j).toString());
  70. }
  71. break;
  72. }
  73. sp.getChildren().add(mylabel);
  74. }
  75. }
  76. }

Решение задачи: «Событие мыши»

textual
Листинг программы
  1. package price1;
  2.  
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseListener;
  5. import javafx.fxml.FXML;
  6. import javafx.scene.control.*;
  7. import javafx.scene.layout.AnchorPane;
  8. import javafx.scene.layout.Pane;
  9. import javafx.scene.web.WebView;
  10.  import javax.script.*;
  11. /**
  12.  *
  13.  * @author Миха
  14.  */
  15. public class FXMLDocumentController implements  MouseListener {
  16.    
  17.     @FXML
  18.     private Label label;
  19.    
  20.     @FXML
  21.     private AnchorPane anhorPanePrice;
  22.    
  23.    
  24.     @FXML
  25.     private ProgressBar prog;
  26.    
  27.     @FXML
  28.     private ComboBox comboboxFirm;
  29.    
  30.     @FXML
  31.     private ComboBox comboboxCountry;
  32.     @FXML
  33.     private AnchorPane anchorPane;
  34.     private Parsing_price pr;
  35.     private  Pane sp;
  36.     private WebView wv;
  37.     private Label mylabel;
  38.    
  39.    
  40.    
  41.    
  42.    
  43.    @FXML
  44.    private void handleComboboxTextChanget(){
  45.        String country=comboboxCountry.getValue().toString();
  46.          pr=new Parsing_price();
  47.         pr.Parsing_Country(country);
  48.         pr.Parsing_Firm();
  49.         pr.Parsing_Oil();
  50.         pr.Parsing_City();
  51.     ShowPrice();
  52.     AddComboboxFirm();
  53.         try {
  54.             WebMap();
  55.         } catch (ScriptException | NoSuchMethodException ex) {
  56.         }
  57.         Price_DataBase db=new Price_DataBase();
  58.        
  59.    }
  60.  
  61.     private void AddComboboxFirm(){
  62.         comboboxFirm.getItems().clear();
  63.         for(int i=1;i<pr.getParsingFirm().size();i++){
  64.        comboboxFirm.getItems().addAll(pr.getParsingFirm().get(i));
  65.     }
  66.     }
  67.    
  68.     @FXML
  69.     private void handleComboboxValueFirm(){
  70.        
  71.     }
  72.  
  73.     private void ShowPrice(){
  74.         sp=new Pane();
  75.         sp.setLayoutX(8);
  76.         sp.setLayoutY(60);
  77.         sp.setPrefSize(550,700);
  78.         sp.setStyle("-fx-border-width:2pt;-fx-border-color:olive;-fx-background-color:lightgrey;");
  79.         anhorPanePrice.getChildren().add(sp);        
  80.                
  81.         for(int i=0;i<6;i++){
  82.             for(int j=0;j<pr.getParsingFirm().size();j++){
  83.             mylabel=new Label();
  84.             mylabel.setLayoutY(40+j*30);
  85.             mylabel.setPrefSize(200,30);
  86. //           mylabel.addMouseListener(this);
  87.                 switch(i){
  88.                 case 0:
  89.                         mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#000066;");
  90.                         mylabel.setLayoutX(10);
  91.                      
  92.                     if(j==0){
  93.                         mylabel.setLayoutY(10);
  94.                         mylabel.setText("Название фирм");
  95.                     }else{
  96.                         mylabel.setText(pr.getParsingFirm().get(j).toString());
  97.                     }
  98.                         break;
  99.                 case 1:
  100.                         mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#a4aa16;");
  101.                         mylabel.setLayoutX(210);  
  102.                     if(j==0){
  103.                         mylabel.setLayoutY(10);
  104.                         mylabel.setText("A-92");
  105.                     }else{        
  106.                         mylabel.setText(pr.getParsingA92().get(j).toString());
  107.                     }
  108.                         break;  
  109.                 case 2:
  110.                         mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:GREEN;");
  111.                         mylabel.setLayoutX(270);
  112.                     if(j==0){
  113.                         mylabel.setLayoutY(10);
  114.                         mylabel.setText("A-95");
  115.                     }else{            
  116.                         mylabel.setText(pr.getParsingA95().get(j).toString());
  117.                     }
  118.                         break;  
  119.                 case 3: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:RED;");
  120.                         mylabel.setLayoutX(330);            
  121.                     if(j==0){
  122.                         mylabel.setLayoutY(10);
  123.                         mylabel.setText("A-95+");
  124.                     }else{
  125.                         mylabel.setText(pr.getParsingA95P().get(j).toString());
  126.                     }
  127.                         break;    
  128.                 case 4: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:BLACK;");
  129.                         mylabel.setLayoutX(390);            
  130.                    if(j==0){
  131.                         mylabel.setLayoutY(10);
  132.                         mylabel.setText("ДТ");
  133.                     }else{
  134.                         mylabel.setText(pr.getParsingDT().get(j).toString());
  135.                    }
  136.                         break;    
  137.                 case 5: mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:#7d125a;");
  138.                         mylabel.setLayoutX(450);            
  139.                     if(j==0){
  140.                         mylabel.setLayoutY(10);
  141.                         mylabel.setText("Газ");
  142.                     }else{
  143.                         mylabel.setText(pr.getParsingGas().get(j).toString());
  144.                     }
  145.                         break;
  146.             }
  147.             sp.getChildren().add(mylabel);
  148.         }
  149.         }
  150.     }
  151.      
  152.     private void WebMap() throws ScriptException, NoSuchMethodException{
  153.           System.out.println("Hello");
  154.         wv=new WebView();
  155.         wv.setLayoutX(580);
  156.         wv.setLayoutY(60);
  157.         wv.setPrefSize(700,700);
  158.         wv.setStyle("-fx-border-width:2pt;-fx-border-color:olive;-fx-background-color:lightgrey;");
  159.         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");
  160.        
  161.         anhorPanePrice.getChildren().add(wv);
  162.         ScriptEngineManager manager = new ScriptEngineManager();
  163.         ScriptEngine engine = manager.getEngineByName("JavaScript");
  164.  
  165.         // JavaScript code in a String
  166.         String script = "function hello(name) { print('Hello, ' + name); }";
  167.         // evaluate script
  168.         engine.eval(script);
  169.  
  170.         // javax.script.Invocable is an optional interface.
  171.         // Check whether your script engine implements or not!
  172.         // Note that the JavaScript engine implements Invocable interface.
  173.         Invocable inv = (Invocable) engine;
  174.  
  175.         // invoke the global function named "hello"
  176.         inv.invokeFunction("hello", "Scripting!!" );
  177.        
  178.        
  179.     }
  180.  
  181.     @Override
  182.     public void mouseEntered(MouseEvent e) {
  183.            if (e.getSource().equals(mylabel)) {
  184.             mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:RED;");
  185.         }
  186.     }
  187.  
  188.     @Override
  189.     public void mouseExited(MouseEvent e) {
  190. //        for(int i=0;i<pr.getParsingFirm().size();i++)
  191.             if (e.getSource().equals(mylabel)) {
  192.             mylabel.setStyle("-fx-font:bold italic 10pt Georgia;-fx-text-fill:BLUE;");
  193.             }
  194.     }
  195.  
  196.     @Override
  197.     public void mouseClicked(MouseEvent e) {
  198.     }
  199.  
  200.     @Override
  201.     public void mousePressed(MouseEvent e) {
  202.     }
  203.  
  204.     @Override
  205.     public void mouseReleased(MouseEvent e) {
  206.     }
  207.  
  208.  
  209. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 4.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут