- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想做的是,当我按下红色按钮时,它会将背景设置为红色,但由于我将其设置为私有(private),所以我的主面板类看不到我的按钮监听器类,所以我不断收到错误。对我能做什么有什么建议吗?
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
public class ColorFactory extends Application {
@Override
public void start(Stage stage)
{
BorderPane pane = new BorderPane();
// sets the width and height
stage.setHeight(300);
stage.setWidth(500);
//calls the mainpanel constructor
pane.setCenter(new MainPanel());
//make the mainpanel visible using the setVisible(true)
//call the stage.setScene
stage.setScene(new Scene(pane));
// set title to Color Factory
stage.setTitle("Color Factory");
//call stage.show
stage.show();
}
private class MainPanel extends BorderPane
{
public MainPanel()
{
HBox Tpanel = new HBox(25);
Tpanel = new HBox(25);
Tpanel.setPrefWidth(500);
Tpanel.setPrefHeight(50);
Tpanel.setAlignment(Pos.TOP_CENTER);
Button red = new Button("Red");
Button yellow = new Button("Yellow");
Button orange = new Button("Orange");
red.setStyle("-fx-background-color: red");
orange.setStyle("-fx-background-color: orange;");
yellow.setStyle("-fx-background-color: yellow;");
orange.setOnAction(new ButtonListener());
red.setOnAction(new ButtonListener());
yellow.setOnAction(new ButtonListener());
Tpanel.setStyle("-fx-background-color: white;");
Tpanel.getChildren().addAll(red,yellow,orange);
HBox Bpanel = new HBox(15);
Bpanel.setPrefWidth(500);
Bpanel.setPrefHeight(75);
RadioButton green = new RadioButton("Green");
RadioButton blue = new RadioButton("Blue");
RadioButton cyan = new RadioButton("Cyan");
blue.setOnAction(new RadioButtonListener());
green.setOnAction(new RadioButtonListener());
cyan.setOnAction(new RadioButtonListener());
green.setStyle("-fx-background-color: green;");
blue.setStyle("-fx-background-color: blue;");
cyan.setStyle("-fx-background-color: cyan;");
Bpanel.setAlignment(Pos.BOTTOM_CENTER);
Bpanel.getChildren().addAll(green,blue,cyan);
Label label = new Label("Top buttons change the panel color and bottom radio buttons change the text color");
label.setAlignment(Pos.CENTER_LEFT);
label.setTextFill(Color.BLUE);
setTop(Tpanel);
setBottom(Bpanel);
setCenter(label);
HBox.setMargin(Tpanel, new Insets(5,10,5,10));
HBox.setMargin(Bpanel, new Insets(5,10,5,10));
HBox.setMargin(label, new Insets(150,10,5,10));
}
}
private class ButtonListener implements EventHandler<ActionEvent>
{
public void handle(ActionEvent event)
{
if(event.getSource() == red)
{
red.setStyle("-fx-background-color: red;");
}
else if(event.getSource() == yellow)
{
yellow.setStyle("-fx-background-color: yellow;");
}
else
{
orange.setStyle("-fx-background-color: orange;");
}
}
}
private class RadioButtonListener implements EventHandler<ActionEvent>
{
public void handle(ActionEvent radio)
{
if(radio.getSource() == green)
{
label.setTextFill(Color.GREEN);
}
if(radio.getSource() == blue)
{
label.setTextFill(Color.BLUE);
}
if(radio.getSource() == cyan)
{
label.setTextFill(Color.BLUE);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
您的程序无法编译,因为您的按钮和标签在构造函数内局部可见。您必须将它们的定义移至顶级类,以便其他人可以看到它们。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
public class ColorFactory extends Application {
Button red;
Button yellow;
Button orange;
RadioButton green;
RadioButton blue;
RadioButton cyan;
Label label;
@Override
public void start(Stage stage)
{
BorderPane pane = new BorderPane();
// sets the width and height
stage.setHeight(300);
stage.setWidth(500);
//calls the mainpanel constructor
pane.setCenter(new MainPanel());
//make the mainpanel visible using the setVisible(true)
//call the stage.setScene
stage.setScene(new Scene(pane));
// set title to Color Factory
stage.setTitle("Color Factory");
//call stage.show
stage.show();
}
private class MainPanel extends BorderPane
{
public MainPanel()
{
HBox Tpanel = new HBox(25);
Tpanel = new HBox(25);
Tpanel.setPrefWidth(500);
Tpanel.setPrefHeight(50);
Tpanel.setAlignment(Pos.TOP_CENTER);
red = new Button("Red");
yellow = new Button("Yellow");
orange = new Button("Orange");
red.setStyle("-fx-background-color: red");
orange.setStyle("-fx-background-color: orange;");
yellow.setStyle("-fx-background-color: yellow;");
orange.setOnAction(new ButtonListener());
red.setOnAction(new ButtonListener());
yellow.setOnAction(new ButtonListener());
Tpanel.setStyle("-fx-background-color: white;");
Tpanel.getChildren().addAll(red,yellow,orange);
HBox Bpanel = new HBox(15);
Bpanel.setPrefWidth(500);
Bpanel.setPrefHeight(75);
green = new RadioButton("Green");
blue = new RadioButton("Blue");
cyan = new RadioButton("Cyan");
blue.setOnAction(new RadioButtonListener());
green.setOnAction(new RadioButtonListener());
cyan.setOnAction(new RadioButtonListener());
green.setStyle("-fx-background-color: green;");
blue.setStyle("-fx-background-color: blue;");
cyan.setStyle("-fx-background-color: cyan;");
Bpanel.setAlignment(Pos.BOTTOM_CENTER);
Bpanel.getChildren().addAll(green,blue,cyan);
label = new Label("Top buttons change the panel color and bottom radio buttons change the text color");
label.setAlignment(Pos.CENTER_LEFT);
label.setTextFill(Color.BLUE);
setTop(Tpanel);
setBottom(Bpanel);
setCenter(label);
HBox.setMargin(Tpanel, new Insets(5,10,5,10));
HBox.setMargin(Bpanel, new Insets(5,10,5,10));
HBox.setMargin(label, new Insets(150,10,5,10));
}
}
private class ButtonListener implements EventHandler<ActionEvent>
{
public void handle(ActionEvent event)
{
if(event.getSource() == red)
{
red.setStyle("-fx-background-color: red;");
}
else if(event.getSource() == yellow)
{
yellow.setStyle("-fx-background-color: yellow;");
}
else
{
orange.setStyle("-fx-background-color: orange;");
}
}
}
private class RadioButtonListener implements EventHandler<ActionEvent>
{
public void handle(ActionEvent radio)
{
if(radio.getSource() == green)
{
label.setTextFill(Color.GREEN);
}
if(radio.getSource() == blue)
{
label.setTextFill(Color.BLUE);
}
if(radio.getSource() == cyan)
{
label.setTextFill(Color.BLUE);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
关于JavaFX编程ActionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39844058/
如何让操作处理程序忽略箭头键?目前,当我尝试使用箭头键导航组合框时,组合框向下/向上移动一次,更改选择,然后触发操作处理程序将焦点移动到按钮。我希望能够使用箭头键导航组合框,并在准备好转到下一个组件时
我使用 Vaadin 6.8.8。我正在尝试将操作处理程序添加到面板(以获取上下文菜单)。该面板占据了窗口的所有位置,但当我单击鼠标右键时,我导致了浏览器上下文菜单。 public class MyP
我正在尝试通过按如下所示插入this.send()来在Ember中检测ActionHandler#send: Ember.ActionHandler.reopen({ send() { conso
我的问题是,当尝试执行我的代码时,我的actionListener 上不断收到NullPointerException。这是我的登录 View 。经过一些测试后,LoginView 类中出现异常。 p
我使用 javafx 和 scenebuilder 2.0 在 netbeans 中制作了一个基本的登录屏幕!通过复制 YouTube 教程。 如上所示,netbeans 在“#initialize
这是我的代码: seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ public voi
我是一名优秀的程序员,十分优秀!