我正在处理的应用程序涉及根据用户选择从 JPanel 创建的框架。如果选择两次相同的项目,我试图阻止用户启动同一框架的多个实例。这是我为此目的而写的条件。
主类:
public void showPieGraphFrame()
{
final PieGraph gPieGraph = new PieGraph("Traffic Type Distribution", counterOne, counterTwo);
gPieGraph.pack();
RefineryUtilities.positionFrameOnScreen(gPieGraph, 0.35, 0.03);
if(!gPieGraph.isVisible())
{
gPieGraph.setVisible(true);
}
}
我想防止多个实例的 PieGraph 类:
public class PieGraph extends ApplicationFrame implements ActionListener {
public PieGraph(final String title) {
super(title);
// create a menubar
setJMenuBar(createMenuBar());
// create a dataset...
final PieDataset dataset = trafficTypeDataset();
// create the chart...
final JFreeChart chart = createChart(dataset);
// add the chart to a panel...
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
setContentPane(chartPanel);
}
private JFreeChart createChart(final PieDataset dataset) {
final JFreeChart chart = ChartFactory.createPieChart("Test Chart", dataset, false, false, false);
final PiePlot plot = (PiePlot) chart.getPlot();
return chart;
}
但是,它不起作用,您仍然可以多次启动同一帧。我怎样才能防止这种情况?