我想要创建组合框,这是用来切换面板。例如,我想要创建几个不同颜色的面板,使用组合框,我希望只有一个可见的:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class MainApp extends Application
{
@Override
public void start(Stage primaryStage)
{
final ComboBox comboBox = new ComboBox();
comboBox.getItems().addAll(
"Bar Chart",
"Pie Chart");
comboBox.setValue("Bar Chart");
final Label label = new Label();
final StackPane stack = new StackPane();
Button btn = new Button();
btn.setText("Read comboBox");
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
label.setText("selectd: " + comboBox.getValue());
stack.getChildren();
}
});
stack.getChildren().add(0, new Rectangle(100,100,Color.BLUE));
stack.getChildren().add(1, new Rectangle(100,100,Color.GREEN));
VBox vBox = new VBox();
vBox.setPadding(new Insets(5, 5, 5, 5));
vBox.setSpacing(5);
vBox.getChildren().addAll(label, comboBox, btn);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}你能帮我的代码,将用于切换可见面板,洞察力,组合框,更改监听器。
发布于 2014-10-11 22:31:36
实现这一点的方法有很多种,您可以通过@Vitomir将ComboBox的值硬编码到开关情况下。
这里还有一个示例,它认为矩形是按照与将值添加到StackPane的顺序相同的顺序添加到ComboBox中的,每当您选择一个组合框(或选择并按下按钮)时,所选的矩形是可见的,其余的是不可见的。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ComboBoxIssue extends Application {
@Override
public void start(Stage primaryStage) {
final ComboBox comboBox = new ComboBox();
comboBox.getItems().addAll("Show Blue", "Show Green");
final Label label = new Label();
final StackPane stack = new StackPane();
Button btn = new Button();
btn.setText("Read comboBox");
/**
* Uncomment this to run on button selection
*
*/
/*btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
setVisibility(stack, comboBox, label);
}
});*/
comboBox.getSelectionModel().selectedIndexProperty()
.addListener((ObservableValue<? extends Number> observable,
Number oldValue, Number newValue) ->
setVisibility(stack, comboBox, label)
);
stack.getChildren().add(new Rectangle(100, 100, Color.BLUE));
stack.getChildren().add(new Rectangle(100, 100, Color.GREEN));
// Placing it after adding rectangle to stack
// will trigger the changelistener to show default rectangle
comboBox.setValue("Show Blue");
VBox vBox = new VBox();
vBox.setPadding(new Insets(5, 5, 5, 5));
vBox.setSpacing(5);
vBox.getChildren().addAll(label, comboBox, btn, stack);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setVisibility(Pane pane, ComboBox comboBox, Label label) {
//Set Label
label.setText("selectd: " + comboBox.getValue());
// Make all children invisible
for (Node node : pane.getChildren()) {
node.setVisible(false);
}
// make the selected rectangle visible
int selectedIndex = comboBox.getSelectionModel()
.selectedIndexProperty().getValue();
pane.getChildren().get(selectedIndex).setVisible(true);
}
public static void main(String[] args) {
launch(args);
}
}发布于 2014-10-11 22:00:23
试试这个:
public class MainApp extends Application {
@Override
public void start(Stage primaryStage) {
Rectangle blueRectangle = new Rectangle(100,100, Color.BLUE);
Rectangle grinRectangle = new Rectangle(100,100, Color.GREEN);
StackPane stackPane = new StackPane();
stackPane.getChildren().add(0, blueRectangle);
stackPane.getChildren().add(1, grinRectangle);
ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("Blue rectangle", "Green rectangle");
comboBox.setValue("Green rectangle");
comboBox.setOnAction(e -> {
switch (comboBox.getValue()) {
case "Blue rectangle":
blueRectangle.toFront();
break;
case "Green rectangle":
grinRectangle.toFront();
break;
}
});
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
vBox.setPadding(new Insets(5, 5, 5, 5));
vBox.setSpacing(10);
vBox.getChildren().addAll(comboBox, stackPane);
Scene scene = new Scene(vBox, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}https://stackoverflow.com/questions/26319222
复制相似问题