创建带有居中标签的自定义面板,之后可以对其进行编辑

时间:2018-11-15 09:32:45

标签: java user-interface javafx javafx-8

我试图实现一个自定义面板,该面板的中心有一个具有自定义背景色的标签。标签具有自定义的contextMenu,该菜单具有用于更改字符串的选项,该选项可以“更改字符串”,“更改颜色”,“复制设置”和“粘贴设置”,以便更改标签中的文本。我目前停留在标签上。我希望能够随时编辑两个标签中的任何一个

import static java.lang.Math.random;
import java.util.Optional;
import java.util.Random;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Alert;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
*
* @author Gleyv
*/
public class CustomPanel extends Region{
public Label topLabel = new Label();
public Rectangle rect = new Rectangle();;
private StackPane rootPane = new StackPane(topLabel) ;
private Color mColor;
private static final double aspectRatio = 12.0 / 20.0;
public CustomPanel()
{

    rect.boundsInParentProperty();
    getChildren().addAll(rootPane);
    setContext();
    setStyle("-fx-background-color: #8fbc8f");
    //setStyle("/customStyle/label");
}


/**
 *
 */

public void setContext(){
    ContextMenu contextMenu = new ContextMenu();

    MenuItem cString = new MenuItem("Change String");
    cString.setOnAction(actionEvent -> onString());
    MenuItem cColor = new MenuItem("Change Color");
    cColor.setOnAction(actionEvent -> onColor());
    MenuItem cSettings = new MenuItem("Copy Settings");
    cSettings.setOnAction(actionEvent -> onCopySettings());
    MenuItem pSettings = new MenuItem("Paste Settings");  
    pSettings.setOnAction(actionEvent -> onPSettings());
    contextMenu.getItems().addAll(cString, cColor, cSettings, pSettings);

     topLabel.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() 
    {

        @Override
        public void handle(ContextMenuEvent event) {

            contextMenu.show(topLabel, event.getScreenX(), 
     event.getScreenY());
        }
    });

    }

    @Override
    protected void layoutChildren(){
      //topLabel= new Label("Im the label now");
      double width = getWidth();
      double height = getHeight();

      double safeWidth = height * aspectRatio;
      double safeHeight = width / aspectRatio;

      if (safeHeight > height) safeHeight = height;
      if (safeWidth > width) { safeWidth = width;
      }
       rootPane.setPrefWidth(safeWidth);
       rootPane.setPrefWidth(safeHeight);
       //setLabel("im the label");
       layoutInArea(rootPane, 0.0D, 0.0D, width, height, 


        getBaselineOffset(), HPos.LEFT, VPos.CENTER);

     }

   private void onString() {
    TextInputDialog dialog = new TextInputDialog();
    dialog.setTitle("Text input");
    dialog.setHeaderText("Enter some string");
    Optional<String> result = dialog.showAndWait();

    String entered = result.get();
    System.out.println(entered);
    topLabel.setText(entered);
    getChildren().add(topLabel);

   }

   private void onColor() {
    Random random = new Random();
    int r = random.nextInt(255);
    int g = random.nextInt(255);
    int b = random.nextInt(255);
    mColor = Color.rgb(r, g, b);
    setStyle("-fx-color= " + mColor);
   }

   private void onCopySettings() {
    throw new UnsupportedOperationException("Not supported yet."); //To 
   change body of generated methods, choose Tools | Templates.
  }

   private void onPSettings() {
    throw new UnsupportedOperationException("Not supported yet."); //To 
   change body of generated methods, choose Tools | Templates.
   }

   }//end of CustomPanel

enter image description here

0 个答案:

没有答案