我正在开发的应用程序涉及根据用户选择从 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;
}
但是,它不起作用,您仍然可以多次启动同一框架。我怎样才能防止这种情况发生?
最佳答案
How can I prevent this?
将框架交换为模态 JDialog
或 JOptionPane
。
关于java - 防止 Java 框架的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15009816/