(Translation) The ninth return javafx2.0 button

Source: Internet
Author: User

Original address http://download.oracle.com/javafx/2.0/ui_controls/button.htm#CJHEEACB

 

 

The button class can beThe javafx API allows you to click a button to process a behavior.ButtonClass isLabeledClass, 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.ButtonControls, 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));

BecauseButtonClass inheritedLabeledClass, so you can use the following method to specify content for buttons without icons and titles.

  • setText(String text)Method-specify the title for the button

  • ThesetGraphic(Node graphic)Method-specify the icon

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 isImageViewObject. However, you can also use other graphical objects, suchjavafx.scene.shapeVarious 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. ThesetOnActionMethod 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");    }});

ActionEventYesbyAn event type processed by eventhandler.The eventhandler object providesHandle Method handles button-triggered Behavior. Example
3-3 demonstrate how to overridehandleMethod, 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 inheritsNodeClass, you can useThe visual presentation of any special effect improvement button in the javafx. Scene. effect package. InExample
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 belowSkinClass. 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 usesetStyleClassMethod Used in the application. This method is also inherited fromNodeClass 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 isButton1 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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.