我用 x 和 y 轴创建了自己的图表。我发现目前的图表选择不适合我的需要。我已经设法根据自己的喜好构建图表,现在我想将所有组件添加到扩展 AnchorPane 的对象中。最终结果是我的代码编译时没有错误,没有运行时错误,但我没有看到我创建的任何窗格。我想知道扩展 AnchorPane 的正确程序是什么。
这是我的类中扩展应用程序的代码。奇迹般有效:
Vector <String[]> v = new Vector<String[]>();
double highest_high = 0;
double lowest_low = 0;
AnchorPane anchorpane = new AnchorPane();
double xaxisHeight = 20;
double yaxisWidth = 60;
double chartYAdjustment = 40;
double barGap = 3;
double bars = 15;
final Chart chart = new Chart(v, yaxisWidth, xaxisHeight, highest_high, lowest_low, chartYAdjustment, barGap);
final YAxis yaxis = new YAxis(yaxisWidth, highest_high, lowest_low);
final XAxis xaxis = new XAxis(xaxisHeight, barGap, bars);
AnchorPane.setTopAnchor(yaxis, 0.0);
AnchorPane.setRightAnchor(yaxis, 0.0);
AnchorPane.setBottomAnchor(yaxis, 20.0);
AnchorPane.setLeftAnchor(xaxis, 0.0);
AnchorPane.setRightAnchor(xaxis, 60.0);
AnchorPane.setBottomAnchor(xaxis, 0.0);
AnchorPane.setLeftAnchor(chart, 0.0);
AnchorPane.setTopAnchor(chart, 0.0);
AnchorPane.setBottomAnchor(chart, 0.0);
AnchorPane.setRightAnchor(chart, 0.0);
anchorpane.getChildren().addAll(chart, yaxis, xaxis);
Scene s = new Scene(anchorpane, 800, 400, Color.BLACK);
stage.setScene(s);
stage.show();
当我将代码放在扩展 AnchorPane 的类中时:
public class Main extends AnchorPane{
public void Main(){
Vector <String[]> v = new Vector<String[]>();
double highest_high = 0;
double lowest_low = 0;
double xaxisHeight = 20;
double yaxisWidth = 60;
double chartYAdjustment = 40;
double barGap = 3;
double bars = 15;
final Chart chart = new Chart(v, yaxisWidth, xaxisHeight, highest_high, lowest_low, chartYAdjustment, barGap);
final YAxis yaxis = new YAxis(yaxisWidth, highest_high, lowest_low);
final XAxis xaxis = new XAxis(xaxisHeight, barGap, bars);
AnchorPane.setTopAnchor(yaxis, 0.0);
AnchorPane.setRightAnchor(yaxis, 0.0);
AnchorPane.setBottomAnchor(yaxis, 20.0);
AnchorPane.setLeftAnchor(xaxis, 0.0);
AnchorPane.setRightAnchor(xaxis, 60.0);
AnchorPane.setBottomAnchor(xaxis, 0.0);
AnchorPane.setLeftAnchor(chart, 0.0);
AnchorPane.setTopAnchor(chart, 0.0);
AnchorPane.setBottomAnchor(chart, 0.0);
AnchorPane.setRightAnchor(chart, 0.0);
getChildren().addAll(chart, yaxis, xaxis);
然后在我的扩展应用程序的类中,我有:
Main main = new Main();
Scene s = new Scene(main, 800, 400, Color.BLACK);
stage.setScene(s);
stage.show();
它只显示一个黑色窗口。