代码之家  ›  专栏  ›  技术社区  ›  Joe Mau

向用户显示他单击了哪个按钮

  •  0
  • Joe Mau  · 技术社区  · 6 年前

    我有三个按钮可以显示不同的面板。 如果我按下按钮 btn_All 这个 pnl_All 也应该出现等。 嗯,我想让用户知道他在哪里。 如果他按 全部BTN_ 还有这个小组 PNL_所有 此外,该按钮还应添加特定的CSS类。

    有人知道我该怎么做吗?

    你可以通过代码将CSS类添加到按钮中,但是我必须从按钮中删除它们,所以我觉得这个选项非常麻烦。

    主类

    package sample;
    
    import com.sun.javafx.scene.control.skin.DatePickerSkin;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.DatePicker;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    
    import java.time.LocalDate;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    控制器.java

    package sample;
    
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.control.Button;
    import javafx.scene.layout.Pane;
    
    public class Controller {
    
        @FXML
        private Pane pnl_GraphicCard,pnl_Processors,pnl_All;
    
        @FXML
        private Button btn_GraphicCard,btn_Processors,btn_All;
    
        @FXML
        void buttonClicked(ActionEvent event) {
            if(event.getSource() == btn_All) {
                pnl_All.toFront();
            }
            else if(event.getSource() == btn_GraphicCard) {
                pnl_GraphicCard.toFront();
            }
            else if(event.getSource() == btn_Processors){
                pnl_Processors.toFront();
            }
    
        }
    }
    

    样品.fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.Pane?>
    
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
       <children>
          <Pane fx:id="pnl_GraphicCard" layoutX="317.0" prefHeight="400.0" prefWidth="284.0" style="-fx-background-color: red;" />
          <Pane fx:id="pnl_Processors" layoutX="317.0" prefHeight="400.0" prefWidth="284.0" style="-fx-background-color: blue;" />
          <Pane fx:id="pnl_All" layoutX="317.0" prefHeight="400.0" prefWidth="284.0" style="-fx-background-color: green;" />
          <Button fx:id="btn_GraphicCard" layoutX="43.0" layoutY="59.0" mnemonicParsing="false" onAction="#buttonClicked" text="Processors" />
          <Button fx:id="btn_Processors" layoutX="43.0" layoutY="96.0" mnemonicParsing="false" onAction="#buttonClicked" text="GraphicCard" />
          <Button fx:id="btn_All" layoutX="43.0" layoutY="27.0" mnemonicParsing="false" onAction="#buttonClicked" text="All" />
       </children>
    </AnchorPane>
    

    样式表

    .button:active{
        -fx-text-fill: red;
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   c0der    6 年前

    ToggleButton ToggleGroup

    Buttons ToggleButtons

    <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.control.ToggleButton?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.Pane?>
    
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" 
    prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" 
    xmlns:fx="http://javafx.com/fxml/1"  fx:controller="src.tests.xml.Controller">
       <children>
          <Pane fx:id="pnl_GraphicCard" layoutX="317.0" prefHeight="400.0" prefWidth="284.0" style="-fx-background-color: red;" />
          <Pane fx:id="pnl_Processors" layoutX="317.0" prefHeight="400.0" prefWidth="284.0" style="-fx-background-color: blue;" />
          <Pane fx:id="pnl_All" layoutX="317.0" prefHeight="400.0" prefWidth="284.0" style="-fx-background-color: green;" />
          <ToggleButton fx:id="btn_GraphicCard" layoutX="43.0" layoutY="59.0" mnemonicParsing="false" onAction="#buttonClicked" text="Processors" />
          <ToggleButton fx:id="btn_Processors" layoutX="43.0" layoutY="96.0" mnemonicParsing="false" onAction="#buttonClicked" text="GraphicCard" />
          <ToggleButton fx:id="btn_All" layoutX="43.0" layoutY="27.0" mnemonicParsing="false" onAction="#buttonClicked" text="All" />
       </children>
    </AnchorPane>
    

    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.ToggleButton;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.layout.Pane;
    
    public class Controller implements Initializable{
    
        @FXML
        private Pane pnl_GraphicCard,pnl_Processors,pnl_All;
    
        @FXML
        private ToggleButton btn_GraphicCard,btn_Processors,btn_All;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
    
            ToggleGroup bg = new ToggleGroup();
            bg.getToggles().addAll( btn_GraphicCard,btn_Processors,btn_All);
        }
    
        @FXML
        void buttonClicked(ActionEvent event) {
            if(event.getSource() == btn_All) {
                pnl_All.toFront();
            }
            else if(event.getSource() == btn_GraphicCard) {
                pnl_GraphicCard.toFront();
            }
            else if(event.getSource() == btn_Processors){
                pnl_Processors.toFront();
            }
        }
    }