如何在JScrollPane中调用另一个JPanel的实例

时间:2013-03-12 11:04:34

标签: java swing jpanel jscrollpane mouseclick-event

我在JFrame的borderlayout的CENTER中插入了一个jscrollpane 我有一个名为“UI”的JPANEL已经插入到jscrollpane中 我想在UI Jpanel中单击按钮时将新的JPanel名称“OUTPUTPAGE1”插入jscrollpane

class OutputPage1 extends JPanel 
{
//JButton jb;
XDash xd;
Plain p;
YourPersonal yp;
    Heading sp; 
StartTalkTable sTalk;
AtGlance g;
BlueIndex in;
    JLabel image1,image2,image3,image4,image5;
    MemberInfo mi;
    OrangeBorder o1;
    Footer f;
OutputPage1()
{

    //jb=new JButton("HI");
    xd=new XDash();
    p=new Plain();
    yp=new YourPersonal();
    sp=new Heading("Statement Period: Jan 1, 2013 to Mar 31, 2013","#000000","#C7D9F1",13);           //Heading(String t,String fg,String bg,int size)//
            sp.setHorizontalAlignment(sp.CENTER);
            sp.setFont(new Font("Segoe UI",Font.BOLD,13));
    sTalk=new StartTalkTable();
    g=new AtGlance();
    in=new BlueIndex();
            image1=new JLabel(new ImageIcon("D:/Syntel_Java/images/firstimage.jpg"));
    image2=new JLabel(new ImageIcon("D:/Syntel_Java/images/secondimage.jpg"));
            image3=new JLabel(new ImageIcon("D:/Syntel_Java/images/thirdimage.jpg"));
            image4=new JLabel(new ImageIcon("D:/Syntel_Java/images/fourthimage.jpg"));
            image5=new JLabel(new ImageIcon("D:/Syntel_Java/images/secondimage.jpg"));
            mi=new MemberInfo();
            o1=new OrangeBorder();
            f=new Footer();

    this.setLayout(null);


    //this.add(jb);
    //jb.setBounds(50,800,100,100);         //setBounds(int x, int y, int width, int height)//

    this.add(xd);
            xd.setBounds(50,33,150,150);           //setBounds(int x, int y, int width, int height)//

            this.add(p);
            p.setBounds(100,36,150,150);            //setBounds(int x, int y, int width, int height)//

            this.add(yp);
            yp.setBounds(50,80,400,90);          //setBounds(int x, int y, int width, int height)//

    this.add(sp);
    sp.setBounds(420,120,335,27);           //setBounds(int x, int y, int width, int height)//

    this.add(sTalk);
    sTalk.setBounds(230,160,457,314);           //setBounds(int x, int y, int width, int height)//

    this.add(g);
    g.setBounds(230,481,457,36);            //setBounds(int x, int y, int width, int height)//

    this.add(in);
    in.setBounds(230,525,458,429);          //setBounds(int x, int y, int width, int height)//

            this.add(image1);
            image1.setBounds(50,187,138,138);           //setBounds(int x, int y, int width, int height)//

            this.add(image2);
            image2.setBounds(50,320,138,138);           //setBounds(int x, int y, int width, int height)//

            this.add(image3);
            image3.setBounds(50,453,138,138);           //setBounds(int x, int y, int width, int height)//

            this.add(image4);
            image4.setBounds(50,580,138,138);           //setBounds(int x, int y, int width, int height)//

            this.add(image5);
            image5.setBounds(50,700,138,138);           //setBounds(int x, int y, int width, int height)//

            this.add(mi);
            mi.setBounds(50,840,168,116);           //setBounds(int x, int y, int width, int height)//

            this.add(f);
            f.setBounds(220,1000,270,100);          //setBounds(int x, int y, int width, int height)//

            this.add(o1);
            o1.setBounds(20,980,700,200);           //setBounds(int x, int y, int width, int height)//



    this.setBackground(Color.white);
    this.setPreferredSize(new Dimension(getWidth(),1500));          //Dimension(int width, int height) //
    this.setBorder(BorderFactory.createLineBorder(Color.decode("#C1FC79"),20));
}
 }






    class UI extends JPanel implements ActionListener,MouseListener
     {
    JButton account,member_details,loadTemplate,viewSummary;
    JLabel member_xplain1,member_xplain2;
    JTextField userID,memberID,firstName,lastName;
    JPasswordField password;
    Image bg1,bg2;
    JComboBox fdate,fmonth,fyear,tdate,tmonth,tyear;
    static int CURRENT=1;
    String[] dates,months,years;
UI()
    {
        account=new JButton(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/account.jpg"));
        member_details=new JButton(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/member.jpg"));
        loadTemplate=new JButton(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/load_normal.jpg"));
        viewSummary=new JButton(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/view_normal.jpg"));
        member_xplain1=new JLabel(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/member_xplain.jpg"));
        member_xplain2=new JLabel(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/member_details.jpg"));
        memberID=new JPasswordField(10);
        firstName=new JTextField("ENTER YOUR NAME",10);
        lastName=new JTextField("ENTER YOUR SURNAME",10);
        userID=new JTextField("Enter your UserID",10);
        password=new JPasswordField(10);
        dates=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
        months=new String[]{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        years=new String[]{"2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014"};
        fdate=new JComboBox(dates);
        fmonth=new JComboBox(months);
        fyear=new JComboBox(years);
        tdate=new JComboBox(dates);
        tmonth=new JComboBox(months);
        tyear=new JComboBox(years);


        this.setLayout(null);

        this.add(account);
        account.setBounds(352,582,266,45);         //setBounds(int x, int y, int width, int height)//

        this.add(member_details);
        member_details.setBounds(618,582,266,45);          //setBounds(int x, int y, int width, int height)//

        this.add(userID);
        userID.setBounds(610,160,160,28);          //setBounds(int x, int y, int width, int height)//

        this.add(password);
        password.setBounds(610,216,160,28);          //setBounds(int x, int y, int width, int height)//

        this.add(memberID);
        memberID.setBounds(610,160,160,28);          //setBounds(int x, int y, int width, int height)//

        this.add(firstName);
        firstName.setBounds(610,216,160,28);          //setBounds(int x, int y, int width, int height)//

        this.add(lastName);
        lastName.setBounds(610,272,160,28);          //setBounds(int x, int y, int width, int height)//

        this.add(fdate);
        fdate.setBounds(605,327,51,28);          //setBounds(int x, int y, int width, int height)//

        this.add(fmonth);
        fmonth.setBounds(656,327,100,28);          //setBounds(int x, int y, int width, int height)//

        this.add(fyear);
        fyear.setBounds(756,327,65,28);          //setBounds(int x, int y, int width, int height)//

        this.add(tdate);
        tdate.setBounds(605,382,51,28);          //setBounds(int x, int y, int width, int height)//

        this.add(tmonth);
        tmonth.setBounds(656,382,100,28);          //setBounds(int x, int y, int width, int height)//

        this.add(tyear);
        tyear.setBounds(756,382,65,28);          //setBounds(int x, int y, int width, int height)//

        this.add(loadTemplate);
        loadTemplate.setBounds(424,425,420,58);          //setBounds(int x, int y, int width, int height)//
        loadTemplate.setBorderPainted(false);

        this.add(viewSummary);
        viewSummary.setBounds(425,495,420,58);          //setBounds(int x, int y, int width, int height)//
        viewSummary.setBorderPainted(false);

         this.add(member_xplain1);
        member_xplain1.setBounds(350,96,551,485);          //setBounds(int x, int y, int width, int height)//

        this.add(member_xplain2);
        member_xplain2.setBounds(350,96,551,485);          //setBounds(int x, int y, int width, int height)//



        //ACTION LISTENER//
        viewSummary.addActionListener(this);
        loadTemplate.addActionListener(this);
        account.addActionListener(this);
        member_details.addActionListener(this);


        //MOUSE LISTENER//
        viewSummary.addMouseListener(this);
        loadTemplate.addMouseListener(this);

    }
    public void actionPerformed(ActionEvent e)              //public void actionPerformed(ActionEvent e)//
    {
        if(e.getSource()==account)
        {
            if(CURRENT==1)
            {
               //DO NOTHING//
            }
            if(CURRENT==2)
            {
                CURRENT=1;
                this.repaint();
            }
        }
        if(e.getSource()==member_details)
        {
            if(CURRENT==1)
            {
                CURRENT=2;
                this.repaint();
            }
            if(CURRENT==2)
            {
                //DO NOTHING//
            }
        }
    }
    public void mouseClicked(MouseEvent e)
    {

    }
    public void mouseEntered(MouseEvent e)
    {
        if(e.getSource()==loadTemplate)
        {
            loadTemplate.setIcon(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/load_hover.jpg"));
        }
        if(e.getSource()==viewSummary)
        {
            viewSummary.setIcon(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/view_over.jpg"));
        }
    }
    public void mouseExited(MouseEvent e)
    {
        if(e.getSource()==loadTemplate)
        {
            loadTemplate.setIcon(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/load_normal.jpg"));
        }
        if(e.getSource()==viewSummary)
        {
            viewSummary.setIcon(new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/view_normal.jpg"));
        }
    }
    public void mousePressed(MouseEvent e)
    {
    }
    public void mouseReleased(MouseEvent e)
    {
    }
    public void paintComponent(Graphics g)
    {
        if(CURRENT==1)
        {
            bg1=new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/1.jpg").getImage();
            g.drawImage(bg1,0,0,getWidth(),getHeight(),this);


             //SETTING PAGE2 ELEMENTS TO FALSE//
                memberID.setVisible(false);
                firstName.setVisible(false);
                lastName.setVisible(false);
                fdate.setVisible(false);
                fmonth.setVisible(false);
                fyear.setVisible(false);
                tdate.setVisible(false);
                tmonth.setVisible(false);
                tyear.setVisible(false);
                loadTemplate.setVisible(false);
                viewSummary.setVisible(false);
                member_xplain2.setVisible(false);


                //SETTING PAGE1 ELEMENTS TO TRUE//
                userID.setVisible(true);
                password.setVisible(true);
                member_xplain1.setVisible(true);
        }
        if(CURRENT==2)
        {
            member_xplain1.setVisible(false);
            userID.setVisible(false);
            password.setVisible(false);
            bg1=new ImageIcon("D:/SYNTEL_JAVA_PROJECT/images/2.jpg").getImage();
            g.drawImage(bg1,0,0,getWidth(),getHeight(),this);


             //SETTING PAGE2 ELEMENTS TO TRUE//
                memberID.setVisible(true);
                firstName.setVisible(true);
                lastName.setVisible(true);
                fdate.setVisible(true);
                fmonth.setVisible(true);
                fyear.setVisible(true);
                tdate.setVisible(true);
                tmonth.setVisible(true);
                tyear.setVisible(true);
                loadTemplate.setVisible(true);
                viewSummary.setVisible(true);


                //SETTING PAGE1 ELEMENTS TO FALSE//
                userID.setVisible(false);
                password.setVisible(false);
        }
    }
}








     class Demo extends JFrame implements AdjustmentListener,MouseListener
      {
      Demo x;
      UI ui;
      OutputPage1 op1;
      JScrollPane jsp,jsp2;
      Demo()
      {
          ui=new UI();
          op1=new OutputPage1();
          jsp=new JScrollPane(ui,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          jsp.setViewportView(ui);

          this.setLayout(new BorderLayout());


          this.add(new BlackPanel(),BorderLayout.WEST);
          this.add(new BlackPanel(),BorderLayout.EAST);
          this.add(new BlackPanel(),BorderLayout.NORTH);
          this.add(new BlackPanel(),BorderLayout.SOUTH);
          this.add(jsp,BorderLayout.CENTER);

          ui.loadTemplate.addMouseListener(this);
          jsp.getVerticalScrollBar().addAdjustmentListener(this);
          jsp.getHorizontalScrollBar().addAdjustmentListener(this);
          this.setVisible(true);
          this.setDefaultCloseOperation(EXIT_ON_CLOSE);
          this.setExtendedState(MAXIMIZED_BOTH);
          x=this;
      }


      public void mouseClicked(MouseEvent e)
      {
          if(e.getSource()==ui.loadTemplate)
          {

                jsp.setViewportView(op1);
                jsp.setVisible(false);
                op1.setVisible(true);
                jsp.getViewport().revalidate();

             // new Demo();
              //jsp.getViewport().repaint();

          }
      }
      public void mouseEntered(MouseEvent e)
      {}
      public void mouseExited(MouseEvent e)
      {}
      public void mousePressed(MouseEvent e)
      {}
      public void mouseReleased(MouseEvent e)
      {}


      //ADJUSTMENT LISTENER//
      public void adjustmentValueChanged(AdjustmentEvent e)           //adjustmentValueChanged(AdjustmentEvent e) //
      {
          jsp.getVerticalScrollBar().setEnabled(true);
          jsp.getVerticalScrollBar().setBlockIncrement(10);
          jsp.getVerticalScrollBar().setUnitIncrement(10);
      }
      public static void main(String args[])
      {
          Demo z=new Demo();
      }
}

我通过写一个更简单的代码来解决问题,这个代码执行了我想要复杂的一个任务....希望它能帮助所有遇到同样麻烦的人:) :)  `

      class Panel1 extends JPanel
      {
      JLabel jb1;
      JButton but;
      Panel1()
      {
        jb1=new JLabel("I AM ON PANEL 1");
        but=new JButton("click me to go to PANEL2");
        this.setLayout(new BorderLayout());

        this.add(jb1,BorderLayout.CENTER);
        this.add(but,BorderLayout.SOUTH);
     }
   }
        class Panel2 extends JPanel
        {
           JLabel jb2;
           Panel2()
           {
             jb2=new JLabel("NOW PANEL 2 SHOULD APPEAR");

             this.setLayout(new BorderLayout());

            this.add(jb2,BorderLayout.CENTER);
           }
    }
  class Trial extends JFrame implements MouseListener
 {
   JFrame mainFrame;
   Panel1 p1;
 Panel2 p2;
 JScrollPane jsp;
Trial()
{
    p1=new Panel1();
    p2=new Panel2();
    jsp=new JScrollPane(p1);


    this.setLayout(new BorderLayout());

    p1.but.addMouseListener(this);


    this.add(jsp,BorderLayout.CENTER);
    this.setVisible(true);
    this.setExtendedState(MAXIMIZED_BOTH);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
   public void mousePressed(MouseEvent e)
   {}
  public void mouseReleased(MouseEvent e)
  {}
  public void mouseClicked(MouseEvent e)
  {
    if(e.getSource()==p1.but)
    {
        jsp.setViewportView(p2);
        jsp.getViewport().revalidate();
        System.out.println("YES I AM LISTENING TO YOU");
    }
   }
  public static void main(String args[])
  {
    new Trial();
  }
}`

它可以解决问题。

0 个答案:

没有答案