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

时间:2018-06-10 08:03:22

标签: java css button javafx

我有3个按钮来显示不同的面板。 如果我按下按钮btn_All,也会出现pnl_All等。 好吧,我想让用户知道他在哪里。 如果他按下btn_All,面板pnl_All也应该出现,按钮应该会添加一定类别的CSS。

有没有人知道我该怎么做?

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

Main.java

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);
    }
}

Controller.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();
        }

    }
}

sample.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>

的style.css

.button:active{
    -fx-text-fill: red;
}

1 个答案:

答案 0 :(得分:2)

您需要的功能恰好是ToggleButtonToggleGroup的用途:

Ti实现它,将您的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();
        }
    }
}