防止Java框架的多个实例

THE*_*TOR 0 java swing visibility jframe

我正在处理的应用程序涉及根据用户选择的内容从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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要阻止多个实例的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;
    }
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用,您仍然可以多次启动相同的帧.我怎么能阻止这个?

And*_*son 6

我怎么能阻止这个?

将框架交换为模态JDialogJOptionPane.