gpt4 book ai didi

JavaFX 组合框在我选择一个选项后不允许我选择另一个选项

转载 作者:行者123 更新时间:2023-12-01 11:02:03 25 4
gpt4 key购买 nike

嘿伙计们,我正在尝试让这个组合框工作,但是在我选择一个选项后,它会更改为图形(就像我想要的那样),但它不会让我选择另一个图形选项。有谁知道如何选择另一个图表?这是我的代码:

public class Grapher extends JApplet {

private static final int JFXPANEL_WIDTH_INT = 800;
private static final int JFXPANEL_HEIGHT_INT = 800;
private static JFXPanel fxContainer;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}

JFrame frame = new JFrame("Graph Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JApplet applet = new Grapher();
applet.init();

frame.setContentPane(applet.getContentPane());

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

applet.start();
}
});
}

@Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {

@Override
public void run() {
createScene();
}
});
}

private void createScene() {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25,25,25,50));

final Label gType = new Label ("Type of Graph: ");

Scene scene = new Scene(grid, 1000, 800);
fxContainer.setScene(scene);
//Multi select Combo box Code
ObservableList<String> options = FXCollections.observableArrayList(
"OSNR_Eff (dB) vs Frequency",
"OSNR (dB) vs Frequency",
"Non-Linear OSNR Penalty (dB) vs Frequency",
"Tx Pwr (dBm) vs Frequency",
"BOL/EOL Span Loss vs. Frequency",
"Chromatic Dispersion (ps/nm) vs. Frequency",
"EOL Worst-Channel OSNR_Eff Per Span",
"Tx Pwr, Opt Tx Pwr, & P_max vs Frequency Per Span",
"EOL Final OSNR_Eff (dB) vs Frequency",
"Tx Pwr vs. Opt Tx Pwr Config 0",
"Tx Pwr vs. Opt Tx Pwr Config 1",
"Tx Pwr vs. Opt Tx Pwr Config 2",
"Tx Pwr vs. Opt Tx Pwr Config 3",
"Tx Pwr vs. Opt Tx Pwr Config 4",
"Tx Pwr vs. Opt Tx Pwr Config 5",
"Tx Pwr vs. Opt Tx Pwr Config 6",
"Tx Pwr vs. Opt Tx Pwr Config 7"
);
final ComboBox comboBox = new ComboBox(options);

comboBox.setVisibleRowCount(4);
comboBox.setValue(null);
//Event Handeling for the ComboBox


comboBox.setOnAction((event) -> {
//Graph Code

//set x and y axis
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();

//Get x/y axis labels
String xLabel;
String yLabel;
String gTitle = (String) comboBox.getValue();

//save x/y axis label
if (gTitle.contains("Tx Pwr vs. Opt Tx Pwr Config" )){
xLabel = "Frequency (THz)";
yLabel = "Tx Pwr (dBm)";
}
else if (gTitle.contains("OSNR_Eff (dB) vs Frequency")){
xLabel = "Frequency (THz)";
yLabel = "OSNR_Eff (dB)";
}
else if (gTitle.contains("OSNR (dB) vs Frequency")){
xLabel = "Frequency (THz)";
yLabel = "OSNR (dB)";
}
else if (gTitle.contains("Non-Linear OSNR Penalty (dB) vs Frequency")){
xLabel = "Frequency (THz)";
yLabel = "Non-Linear Penalty (dB)";
}
else if (gTitle.contains("Tx Pwr (dBm) vs Frequency")){
xLabel = "Frequency (THz)";
yLabel = "Tx Pwr (dBm)";
}
else if (gTitle.contains("BOL/EOL Span Loss vs. Frequency")){
xLabel = "Frequency (THz)";
yLabel = "BOL/EOL Span Loss (dB)";
}
else if (gTitle.contains("Chromatic Dispersion (ps/nm) vs. Frequency")){
xLabel = "Frequency (THz)";
yLabel = "Chromatic Dispersion (ps/nm)";
}
else if (gTitle.contains("EOL Worst-Channel OSNR_Eff Per Span")){
xLabel = "Span Index";
yLabel = "OSNR_Eff (dB)";
}
else if (gTitle.contains("Tx Pwr, Opt Tx Pwr, & P_max vs Frequency Per Span")){
xLabel = "Span Index";
yLabel = "Pwr (dBm)";
}
else{ // for "EOL Final OSNR_Eff (dB) vs Frequency" graph
xLabel = "Frequency (THz)";
yLabel = "OSNR_Eff (dB)";
}

//Set x/y Axis Label
xAxis.setLabel(xLabel);
yAxis.setLabel(yLabel);

//creating the chart
final LineChart<Number,Number> lineChart = new LineChart<>(xAxis,yAxis);
lineChart.setTitle(gTitle);

//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
series.getData().add(new XYChart.Data(1, 23));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(3, 15));
series.getData().add(new XYChart.Data(4, 24));
series.getData().add(new XYChart.Data(5, 34));
series.getData().add(new XYChart.Data(6, 36));
// series.getData().add(new XYChart.Data(7, 22));
// series.getData().add(new XYChart.Data(8, 45));
// series.getData().add(new XYChart.Data(9, 43));
// series.getData().add(new XYChart.Data(10, 17));
// series.getData().add(new XYChart.Data(11, 29));
// series.getData().add(new XYChart.Data(12, 25));
Scene scene1 = new Scene(lineChart,800,600);
lineChart.getData().add(series);
fxContainer.setScene(scene1);

});

grid.add(gType, 0,0,1,1); //Adds label
grid.add(comboBox, 1, 0); //Adds ComboBox


fxContainer.show();


}

}

最佳答案

问题是您正在创建一个新场景,向其中添加图表,然后将 JFXPanel 的场景设置为该场景,因此带有 ComboBox 的现有场景将被覆盖。

基本上,您需要将折线图添加到组合框正在使用的同一场景中。您可以将折线图添加到 GridPane。这是一个示例,您可以随意调整布局。

......
//Set x/y Axis Label
xAxis.setLabel(xLabel);
yAxis.setLabel(yLabel);
//THIS CHECKS TO SEE IF A CHART IS ADDED AND REMOVES IT TO PREVENT
//DUPLICATING THE CHART
if(grid.getChildren().size() == 3){
grid.getChildren().remove(2);

}
//creating the chart
final LineChart<Number,Number> lineChart = new LineChart<>(xAxis,yAxis);
lineChart.setTitle(gTitle);

//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
series.getData().add(new XYChart.Data(1, 23));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(3, 15));
series.getData().add(new XYChart.Data(4, 24));
series.getData().add(new XYChart.Data(5, 34));
series.getData().add(new XYChart.Data(6, 36));
grid.getChildren().add(lineChart); //Add linechart to grid
lineChart.getData().add(series);


});

关于JavaFX 组合框在我选择一个选项后不允许我选择另一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33264302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com