Original address http://download.oracle.com/javafx/2.0/ui_controls/button.htm#CJHEEACB
The button class can be
The javafx API allows you to click a button to process a behavior.Button
Class isLabeled
Class, which can display text and images at the same time ). Figure
3-1 shows buttons with various effects. Through this article, you can learn how to create such a button.
Figure 3-1 Types of buttons
Description of "Figure 3-1 Types of buttons"
Create button
There are three button class constructor methods in the javafx application.Button
Controls, see Example 3-1.
Example 3-1 Creating a button
//A button with an empty text caption.Button button1 = new Button();//A button with the specified text caption.Button button2 = new Button("Accept");//A button with the specified text caption and icon.Image imageOk = new Image(getClass().getResourceAsStream("ok.png"));Button button3 = new Button("Accept", new ImageView(imageOk));
BecauseButton
Class inheritedLabeled
Class, so you can use the following method to specify content for buttons without icons and titles.
Example 3-2 shows how to create an untitled icon button.
Example 3-2 adding an icon to a button
Image imageDecline = new Image(getClass().getResourceAsStream("not.png"));Button button5 = new Button();button5.setGraphic(new ImageView(imageDecline));
The effect of adding a code block to an application is shown in Figure 3-2.
Figure 3-2 button with icon
Description of "Figure 3-2 button with icon"
Example 3-2 and Figure
The icon in 3-2 isImageView
Object. However, you can also use other graphical objects, suchjavafx.scene.shape
Various shapes in the package. You can useThe setgraphictextgap method sets a gap between the two.
The default skin of the button class has the following visual differences.
Figure 3-3 shows the difference with an icon button.
Figure 3-3 button states
Description of "Figure 3-3 button states"
Allocation Behavior
The most basic function of a button is to process the action when the button is pressed. ThesetOnAction
Method defines what will happen when you press the button. Example 3-3 is the code block that defines behavior for button2.
Example 3-3 defining an action for a button
button2.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { label.setText("Accepted"); }});
ActionEvent
YesbyAn event type processed by eventhandler.
The eventhandler object provides
Handle Method handles button-triggered Behavior
. Example
3-3 demonstrate how to overridehandle
Method, so when the user presses button2, the label title becomes "accepted ."
You can use the button class to set any number of event handling methods that you need to cause specific behaviors or apply specific effects.
Application Special Effects
BecauseThe button class inherits
Node
Class, you can useThe visual presentation of any special effect improvement button in the javafx. Scene. effect package. In
Example
In 3-4, button3 is applied when the onmouseentered event occurs.Dropshadow effect.
Example 3-4 applying the dropshadow Effect
DropShadow shadow = new DropShadow();//Adding the shadow when the mouse cursor is onbutton3.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { button3.setEffect(shadow); }});//Removing the shadow when the mouse cursor is offbutton3.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { button3.setEffect(null); }});
Figure 3-4 is the effect of moving the mouse over button3.
Figure 3-4 button with drop shadow
Description of "Figure 3-4 button with drop shadow"
Shape a button
To beautify the buttons belowSkin
Class. Using CSS in javafx 2.0 is similar to using CSS in HTML because they are all based on the same CSS specification.
You can first define a separate CSS file, and then usesetStyleClass
Method Used in the application. This method is also inherited fromNode
Class and available to all UI controls.Of course, you can also use the setstyle method to define the button style directly in the code.
Example
3-5 and figure 3-4 demonstrate the following method.
Example 3-5 styling a button
button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
-The FX-font attribute is
Button1 sets the font size,-The FX-base attribute changes the default color of the button.
. In this way, button1 has a light green and big font, such as figure
3-5.
Figure 3-5 button Styled with CSS
Description of "Figure 3-5 button Styled with CSS"