【问题标题】:How to create a graph window in java如何在java中创建图形窗口
【发布时间】:2012-03-02 14:13:26
【问题描述】:

我正在使用此代码

private void botaoGrafADMouseClicked(java.awt.event.MouseEvent evt) {
    try {
        boolean[] b=new boolean[8];
        if (Caixa9.isSelected()) b[0]=true; else b[0]=false;
        if (Caixa11.isSelected()) b[1]=true; else b[1]=false;
        if (Caixa10.isSelected()) b[2]=true; else b[2]=false;
        if (Caixa12.isSelected()) b[3]=true; else b[3]=false;
        b[4]=false;b[5]=false;b[6]=false;b[7]=false;
        final LineChartDemo1 demo = new LineChartDemo1("Leitura A/D",b,"outad.txt",4);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    } catch (IOException ex) {
        Logger.getLogger(Comunicacao.class.getName()).log(Level.SEVERE, null, ex);
    }       
}

调用图形接口。但是,当我这样做时,每次我调用图表时,它都会在新窗口上生成,如果我关闭这些窗口,整个程序就会关闭。 我想知道我做错了什么。我怎样才能避免这种情况(我会发布一个打印屏幕,但是,作为新用户,我不能,它在 http://i.stack.imgur.com/4JLxQ.png 我认为

编辑:图片

【问题讨论】:

    标签: java netbeans graph jfreechart


    【解决方案1】:

    JFrame 具有 EXIT_ON_CLOSE 的默认关闭操作(即,当您使用窗口管理器关闭窗口时会发生什么)。使用JFrame.setDefaultCloseOperation 设置不同的值。

    【讨论】:

      【解决方案2】:

      我不知道LineChartDemo1 是什么类,但您可以将其设置为JDialog 的内容并在每个对话框中调用setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE)。这样,当用户关闭对话框时,只有该窗口会关闭,其他窗口将保持打开状态。

      【讨论】:

      • 我只是在不同的 JFrame 中运行代码,因此当我关闭它时,程序仍在运行。然后,我使用了 public void windowClosing(final WindowEvent evt){ if(evt.getWindow() == this){ dispose(); } } 最后不要关闭。
      【解决方案3】:

      我创建了一个新的 netbeans JFrame 并像这样引用它:

      InterfaceGrafico minhaInterface = new InterfaceGrafico("Leitura I/O",b,"outio.txt",8);
      

      其中的参数与生成图形的位置相同。在这个“InterfaceGrafico”类中:

      public InterfaceGrafico(final String title,boolean[] b, String nomeArquivo, int col) {
      try {
              initComponents();
              final LineChartDemo1 demo = new LineChartDemo1("Leitura I/O", b, "outio.txt", 8);
              demo.pack();
              RefineryUtilities.centerFrameOnScreen(demo);
              demo.setVisible(true);
          } catch (IOException ex) {
              Logger.getLogger(InterfaceGrafico.class.getName()).log(Level.SEVERE, null, ex);
          }
      

      }

      也就是说,我只是将代码转移到另一个 JFrame。我还评论了 public void run method 。现在我可以关闭每个生成的图表,而无需关闭整个应用程序和其他生成的图表。在“LineChart1”类中,我添加了这个

      public void windowClosing(final WindowEvent evt){
          if(evt.getWindow() == this){
          dispose();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-19
        相关资源
        最近更新 更多