当前位置: 代码迷 >> Java相关 >> 面板切换问题
  详细解决方案

面板切换问题

热度:389   发布时间:2010-05-20 10:47:36.0
面板切换问题
程序代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Zuoye extends JFrame implements ActionListener
{
         
              JButton jb1,jb2,jb3,jb4;
         
         
          public Zuoye()
          {
            super("the first");
           
            this.setSize(400,400);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(3,1));
           
           
            JPanel p1 =new JPanel();
            this.add(p1);
              p1.setBackground(Color.RED);
            
            
              JPanel p2 = new JPanel(new GridLayout(1,2));
              JButton jb1=new JButton("确定");
               jb1.addActionListener(this);
              JPanel  p4=new JPanel(new GridLayout(6,1));
              p2.add(p4);
              JButton jb2=new JButton("登录界面");
               jb2.addActionListener(this);
              JButton jb3=new JButton("温度转化");
            jb2.addActionListener(this);
              p4.add(jb2);
              p4.add(jb3);
            
              p2.add(jb1);
            
            
            
         
              this.add(p2);
              //p2.setBackground(Color.BLUE);
            
            
              JPanel p3 = new JPanel();
              this.add(p3);
              p3.setBackground(Color.YELLOW);
            
            
            
              this.setVisible(true);
          }
         
         
          public static void main(String arg[])
          {
             new Zuoye();   
          }
          public void actionPerformed(ActionEvent e)
          {
              if(e.getSource()==jb1)
              {
                  MyFirstEvent Mf=new MyFirstEvent();
                  Mf.show();
                  Zuoye zy=new Zuoye();
                  zy.show(false);
              }
        
          }
}


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFieldExample extends JFrame implements ActionListener
{
    JLabel JL1,JL2,JL3;
    JButton JB1,JB2;
    JTextField JF1;
    JPasswordField JP1;
    public MyFieldExample()
    {
        super("登录界面");
        JL1=new JLabel("用户名称");
        JL1.setBounds(15,5,60,20);
        JL2=new JLabel("登录密码");
        JL2.setBounds(15,25,60,20);
        JF1=new JTextField("",20);
        JF1.setBounds(110,5,100,20);
        JP1=new JPasswordField("",20);
        JP1.setBounds(110,25,100,20);
        JB1=new JButton("确定");
        JB1.setBounds(35,50,60,30);
        JB1.addActionListener(this);
        JB2=new JButton("取消");
        JB2.setBounds(130,50,60,30);
        JB2.addActionListener(this);
        JL3=new JLabel("");
        JL3.setBounds(60,90,200,30);
        Container winContainer=this.getContentPane();
        winContainer.setLayout(null);
        winContainer.add(JL1);
        winContainer.add(JL2);
        winContainer.add(JB1);
        winContainer.add(JB2);
        winContainer.add(JL3);
        winContainer.add(JF1);
        winContainer.add(JP1);
        this.setSize(250,150);
        this.setVisible(true);
     }
     public static void main(String args[])
     {
         MyFieldExample w1=new MyFieldExample();
        
     }
     public void actionPerformed(ActionEvent e)
     {
         if(e.getSource()==JB1)
         {
             if(JF1.getText().equals("JAVA")&&JP1.getText().equals("1234"))
             {JL3.setText("输入正确,登录成功");
             MyFirstEvent Mf=new MyFirstEvent();
             Mf.show();
             MyFieldExample Mf1=new MyFieldExample();
             Mf1.show(false);
         }
             else
             JL3.setText("输入错误,请重新输入");
         }
         else if
         (e.getSource()==JB2)
          System.exit(0);
      
     }
      
      
      
      
    }
为什么在MyFieldExample里可以切换成功,而在Zuoye中运行通过但是不切换
搜索更多相关的解决方案: 面板  

----------------解决方案--------------------------------------------------------
原因很简单:

你的作业那个类中间定义了成员变量jb1, jb2等,但是后面有重新定义了局域变量覆盖了这个成员变量的定义,所以实际事件并没有加到成员变量上啊。


public class Zuoye extends JFrame implements ActionListener
{
         
              JButton jb1,jb2,jb3,jb4;
         
         
          public Zuoye()
          {
            super("the first");
           
            this.setSize(400,400);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(3,1));
           
           
            JPanel p1 =new JPanel();
            this.add(p1);
              p1.setBackground(Color.RED);
            
            
              JPanel p2 = new JPanel(new GridLayout(1,2));
              JButton jb1=new JButton("确定");   改为:jb1=new JButton("确定");就可以了。
               jb1.addActionListener(this);
              JPanel  p4=new JPanel(new GridLayout(6,1));
              p2.add(p4);
              JButton jb2=new JButton("登录界面");
               jb2.addActionListener(this);
              JButton jb3=new JButton("温度转化");
            jb2.addActionListener(this);
              p4.add(jb2);
              p4.add(jb3);
            
              p2.add(jb1);
            
            
            
         
              this.add(p2);
              //p2.setBackground(Color.BLUE);
            
            
              JPanel p3 = new JPanel();
              this.add(p3);
              p3.setBackground(Color.YELLOW);
            
            
            
              this.setVisible(true);
          }
         
         
          public static void main(String arg[])
          {
             new Zuoye();   
          }
          public void actionPerformed(ActionEvent e)
          {
              if(e.getSource()==jb1)
              {
                  MyFirstEvent Mf=new MyFirstEvent();
                  Mf.show();
                  Zuoye zy=new Zuoye();
                  zy.show(false);
              }
        
          }
}

----------------解决方案--------------------------------------------------------
  相关解决方案