[英]How to remove the panel from the panel it attached to in Java Swing?
I have met a serious problem with my Java swing. 我的Java摇摆遇到了严重的问题。
This is how I initialize my chart, everything seems fine now, xyChartPanel
is declared as a JPanel
in the field, I initialize it with the xyChart
I just created. 这就是我初始化图表的方式,现在一切似乎都很好,
xyChartPanel
在该字段中声明为JPanel
,我使用刚创建的xyChart
对其进行了初始化。 When this step is done, I am okay to see the chart (painted to xyChartPanel
) centered to the JPanel
I am writing code on, see add(xyChartPanel, BorderLayout.CENTER);
完成此步骤后,我可以看到图表(绘制到
xyChartPanel
)居中于我正在编写代码的JPanel
中心,请参见add(xyChartPanel, BorderLayout.CENTER);
. 。
private void initXYChart() {
// Create Chart
xyChart = new XYChartBuilder().width(800).height(800).xAxisTitle(xColName).yAxisTitle("Y").build();
// Customize Chart
xyChart.getStyler().setLegendPosition(LegendPosition.InsideNE);
xyChart.getStyler().setAxisTitlesVisible(true);
xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);
double[] yCoordArray = new double[xCoordArray.length];
// Loop through the series
for (int i = 0; i < yCoordinates.size(); i++) {
List<Double> yCoordOneSeries = yCoordinates.get(i);
// Convert list to array
for (int j = 0; j < yCoordArray.length; j++) {
yCoordArray[j] = yCoordOneSeries.get(j);
}
xyChart.addSeries(yColNames.get(i), xCoordArray, yCoordArray);
}
xyChartPanel = new XChartPanel<>(xyChart);
add(xyChartPanel, BorderLayout.CENTER);
xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
add(xyChartPanel, BorderLayout.CENTER);
}
Now the problem comes, I don't want my chart to be unchanged all the time, actually I want to change the style of my chart responded to my action on the radio buttons. 现在问题来了,我不想我的图表一直保持不变,实际上我想根据我对单选按钮的操作更改图表的样式。
I just wrote the updateChartPanelStyle(JRadioButton styleButton)
method that takes 我刚刚编写了
updateChartPanelStyle(JRadioButton styleButton)
方法,该方法需要
private void updateChartPanelStyle(JRadioButton styleButton) {
String style = styleButton.getText();
if (styleButton.isSelected()) {
System.out.println(style);
switch (style) {
case "Line":
xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);
break;
case "Area":
xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
break;
case "Scatter":
xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Scatter);
}
xyChartPanel = new XChartPanel<>(xyChart);
add(xyChartPanel, BorderLayout.CENTER);
}
}
See in this method, I changed the style of xyChart
I initialized in the last function, and reinitialize the xyChartPanel
, then add the updated xyChartPanel
to the working panel. 请参
xyChart
方法,我更改了在上一个函数中初始化的xyChartPanel
,然后重新初始化xyChartPanel
,然后将更新后的xyChartPanel
添加到工作面板中。 Interestingly, I didn't see any change in my GUI. 有趣的是,我的GUI没有任何变化。 I thought this might be a problem with my
xyChart
whose style could not be changed afterward. 我认为这可能是我的
xyChart
的问题,其样式之后无法更改。 But this is not really the case. 但是事实并非如此。
Even if I "removed" xyChartPanel
with this.remove(xyChartPanel);
即使我“删除”
xyChartPanel
与this.remove(xyChartPanel);
, the GUI doesn't seems to be changed. ,GUI似乎没有更改。
This is really weird, what should I do now? 这真的很奇怪,我现在该怎么办?
Every time you add/remove components to swing dynamically, you need to call revalidate();
每次添加/删除要动态摆动的组件时,都需要调用
revalidate();
and then repaint();
然后
repaint();
on your JPanel (or JFrame if you're adding it straight to that). 在您的JPanel上(如果您直接将其添加到JFrame上)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.