【发布时间】:2015-10-21 16:13:50
【问题描述】:
嘿伙计们,我正在尝试让这个组合框工作,但是在我选择一个选项后,它会变成一个图表(就像我想要的那样),但它不会让我选择另一个图表选项。有谁知道如何选择另一个图表?这是我的代码:
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();
}
}
【问题讨论】:
标签: java graph javafx combobox