从另一个Applet调用Applet

时间:2011-06-23 12:46:51

标签: java applet

我创建了两个applet类来创建两个不同的图表。 在第一个applet中,我创建了一个弹出菜单框,其中包含一些带有动作执行功能的菜单项。单击菜单项后,我需要转到其他applet&必须显示该图表。在运行并执行鼠标单击操作时,它将转到另一个小程序,但不显示图形。 而且,我需要两个图表应该是活着的我的意思是在打开第二个图表后,第一个不应该被关闭。

第一个applet:

public class MouseClass extends JApplet implements ActionListener {
   /** Time series for total memory used. */
   JTextArea textArea;
   JFreeChart localJFreeChart;
   ChartPanel chartpanel;
   int time;
   double channel1;
   boolean chartClicked;
   final JPopupMenu cutpasteMenu = new JPopupMenu();
   JMenuItem DwellMenuItem = new JMenuItem("Dwell Id");
   JMenuItem TargetMenuItem = new JMenuItem("Target Id");
   JMenuItem TrackMenuItem = new JMenuItem("Track Id");
   XYSeries series1 = new XYSeries("channel1");
   XYSeries series2 = new XYSeries("channel2");
   XYSeriesCollection dataset = new XYSeriesCollection();

   public MouseClass() {
      try {
         Class.forName("com.mysql.jdbc.Driver");
         Connection con = DriverManager.getConnection(
                  "jdbc:mysql://localhost:3306/wbrdatabase", "root", "ronanki");
         Statement st = con.createStatement();
         System.out.println("data base created");
         ResultSet rs = st.executeQuery("select * from wbrdb");
         while (rs.next()) {
            double time = rs.getDouble(1);
            double channel1 = rs.getDouble(2);
            series1.add(time, channel1);
         }
         dataset.addSeries(series1);

      } catch (Exception e) {
         e.printStackTrace();
      }
      localJFreeChart = ChartFactory.createXYLineChart(
               "graph for particular id", "time", "amplitude", dataset,
               PlotOrientation.VERTICAL, true, true, false);
      chartpanel = new ChartPanel(localJFreeChart);
      MouseEvents();
      chartpanel.setHorizontalAxisTrace(true);
      chartpanel.setMouseWheelEnabled(true);
      chartpanel.setPreferredSize(new Dimension(400, 300));
      getContentPane().add(chartpanel);
   }

   public void MouseEvents() {
      DwellMenuItem.addActionListener(this);
      TargetMenuItem.addActionListener(this);
      TrackMenuItem.addActionListener(this);

      cutpasteMenu.add(DwellMenuItem);
      cutpasteMenu.add(TargetMenuItem);
      cutpasteMenu.add(TrackMenuItem);

      chartpanel.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e, Cursor cursor) {
            setCursor(cursor);

            switch (e.getModifiers()) {
            case InputEvent.BUTTON1_MASK: {
               cutpasteMenu.show(e.getComponent(), e.getX(), e.getY());
               System.out.println(e.getSource().getClass().getName());
               break;
            }
            }
         }
      });

      chartpanel.addMouseListener(new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            switch (e.getModifiers()) {
            case InputEvent.BUTTON1_MASK: {
               cutpasteMenu.show(e.getComponent(), e.getX(), e.getY());
               cutpasteMenu.setInvoker(e.getComponent());
               break;
            }
            }
         }
      });
   }

   public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
      System.out.println("came to perform action");
      Object source = e.getSource();
      if (source == DwellMenuItem) {
         System.out.println("calling dwell");
         sqlvaluesinhorizantal ob = new sqlvaluesinhorizantal("");
         // getAppletContext().showDocument(order1);

      }
      if (source == TargetMenuItem) {
         System.out.println("calling target");

         getAppletContext().showDocument(order2);
      }
      if (source == TrackMenuItem) {
         System.out.println("calling track");

         getAppletContext().showDocument(order3);
      }
   }
}

第二小程序:

    @SuppressWarnings("serial")
public class sqlvaluesinhorizantal extends ApplicationFrame implements ChangeListener {

    int time;

    public sqlvaluesinhorizantal(String title) {
        super(title);

        JPanel localjpanel=createpanel();
        localjpanel.setPreferredSize(new Dimension(400,300));
        localjpanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));   
        // make the slider units "minutes"

        add(localjpanel, BorderLayout.SOUTH);
        //setContentPane(localjpanel);

    }
        private  JFreeChart createChart(XYDataset Dataset)
         {
           JFreeChart localJFreeChart = ChartFactory.createXYLineChart("graph for  Dwellid", "time", "amplitude", Dataset, PlotOrientation.VERTICAL, true, true, false);

           return localJFreeChart;
          }

    private  static  XYDataset createDataset() {
         XYSeries series1 = new XYSeries("channel1");

        XYSeriesCollection dataset=new XYSeriesCollection(); 
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/wbrdatabase","root","ronanki");
        Statement st=con.createStatement();
        System.out.println("data base created");
        ResultSet rs=st.executeQuery("select * from wbrdb");

    while(rs.next())
    {

                double time=rs.getDouble(1);
        double channel1=rs.getDouble(2);
        series1.add(time,channel1);
    }
    dataset.addSeries(series1); 
    }
    catch(Exception e)
    {
            e.printStackTrace();
    }
    return dataset;
    }
    private JPanel createpanel() {
        JFreeChart chart=createChart(createDataset());
        ChartPanel chartpanel=new ChartPanel(chart);

        chartpanel.setHorizontalAxisTrace(true);
        chartpanel.setMouseWheelEnabled(true);

        return chartpanel;
    }



    public static void main(final String[] args) {

        final sqlvaluesinhorizantal demo = new sqlvaluesinhorizantal("hi hello");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
    @Override
    public void stateChanged(ChangeEvent e) {
        // TODO Auto-generated method stub

    }
}

1 个答案:

答案 0 :(得分:0)

为什么不让第二个小程序成为JDialog呢?

例如(未经测试的代码):

  if (source == DwellMenuItem) {
     System.out.println("calling dwell");
     SqlValuesInHorizantal ob = new SqlValuesInHorizantal(); //
     Window parentWindow = SwingUtilities.getWindowAncestor(this);
     JDialog dialog = new JDialog(parentWindow, "Dialog Title");
     dialog.setModalityType(ModalityType.MODELESS); //!! or make this modal if desired
     dialog.getContentPane().add(ob);
     dialog.pack();
     dialog.setLocationRelativeTo(null);
     dialog.setVisible(true);
     // getAppletContext().showDocument(order1);

  }

并在您的SqlValuesInHorizantal类中(注意更改名称的大小写以符合Java标准):

class SqlValuesInHorizantal extends JPanel implements ChangeListener {

   int time;

   public SqlValuesInHorizantal() {
       setLayout(new BorderLayout());
       JPanel localjpanel=createpanel();
       localjpanel.setPreferredSize(new Dimension(400,300));
       localjpanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));   
       // make the slider units "minutes"

       add(localjpanel, BorderLayout.SOUTH);
       //setContentPane(localjpanel);

   }