2015-06-10 12 views
0

这里是我的货币转换程序代码。 先前的空指针异常发生,但已被整理。现在程序中没有错误。程序运行但转换按钮不执行任何操作。我相信问题存在于ActionPerformed函数中,但我无法识别它。JButton没有执行它在java中的必需功能

import java.awt.event.*; 
    import javax.swing.*; 
    import java.awt.*; 
    import java.io.*; 



    public class Trial extends JFrame implements ActionListener{ 
      public String[] list={"DOLLARS","EUROS ","YEN","POUNDS","RUPEES"}; 

     JPanel panel1 = new JPanel(); 

     JPanel panel2 = new JPanel(); 
     public JComboBox cb,cb1; 

     public JTextField tf1; 
     public JTextField tf2; 
     public JLabel label3; 
     public JLabel label4; 

     JPanel panel3 = new JPanel(); 

     public JButton button1; 
     public JButton button2; 

     public Trial(){ 
     setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));   
    getContentPane().setLayout(
     new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS) 
    ); 


     JLabel label1 = new JLabel("FROM :"); 
     label1.setFont(new Font("Serif", Font.BOLD, 15)); 

     JComboBox cb = new JComboBox(); 


     cb= new JComboBox(list); 
     cb.getSelectedIndex(); 
     panel1.setBackground(Color.RED); 
     panel1.add(label1); 
     panel1.add(cb); 


     JLabel label2 = new JLabel("TO :"); 
     label2.setFont(new Font("Serif", Font.BOLD, 15)); 
     JComboBox cb1 = new JComboBox(); 
     cb1=new JComboBox(list); 
     cb1.getSelectedIndex(); 
     cb.addActionListener(this); 
     cb1.addActionListener(this);  

     panel1.add(label2); 
     panel1.add(cb1); 
     add(panel1); 
     JLabel label3 = new JLabel("\n\nENTER THE AMOUNT :"); 
     label3.setFont(new Font("Serif", Font.BOLD, 15)); 
     JTextField tf1 = new JTextField(15); 

    // label3.setHorizontalAlignment(SwingConstants.CENTER); 
    // label3.setVerticalAlignment(SwingConstants.CENTER); 
     panel2.add(label3); 
     panel2.add(tf1);  

     JLabel label4 = new JLabel("CONVERTED AMOUNT :"); 
     label4.setFont(new Font("Serif", Font.BOLD, 15)); 
     label4.setDisplayedMnemonic(KeyEvent.VK_O); 
     JTextField tf2 = new JTextField(15); 

     tf1.addActionListener(this);  
     tf2.addActionListener(this); 

     panel2.add(label4); 
     panel2.add(tf2); 
     panel2.setBackground(Color.BLUE); 
     add(panel2); 

     JButton button1 = new JButton("CONVERT"); 
     button1.setFont(new Font("Serif", Font.BOLD, 15)); 
     button1.setMnemonic(KeyEvent.VK_K); 
     panel3.add(button1); 

     JButton button2 = new JButton("CLEAR "); 
     button2.setFont(new Font("Serif", Font.BOLD, 15)); 
     button2.setMnemonic(KeyEvent.VK_C); 

     button1.addActionListener(this);  
     button2.addActionListener(this);  

     panel3.add(button2); 
     panel3.setBackground(Color.GREEN); 

     add(panel3);  
    } 

     public void actionPerformed(ActionEvent e) { 
      double a,b=0; 
      Object src = e.getSource(); 
      if(src.equals(button1)){ 
      a=Double.valueOf(tf1.getText()); 
      try{ 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==1) 
        {b=a*0.89;} 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==2) 
        {b=a*124.75;} 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==3) 
        {b=a*0.65;} 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==4) 
        {b=a*64.08;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==0) 
        {b=a*1.13;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==2) 
        {b=a*140.49;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==3) 
        {b=a*0.74;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==4) 
        {b=a*71.34;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==0) 
        {b=a*0.0080;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==1) 
        {b=a*0.0071;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==3) 
        {b=a*0.0052;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==4) 
        {b=a*0.51;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==0) 
        {b=a*1.53;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==1) 
        {b=a*1.36;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==2) 
        {b=a*191.26;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==4) 
        {b=a*97.88;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==0) 
        {b=a*0.0156;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==1) 
       { b=a*0.014;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==2) 
        {b=a*1.9607;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==3) 
        {b=a*0.0108;} 

       tf2.setText(String.valueOf(b)); 

      }catch(Exception x){System.out.println("Error");} 
      } 
      if(src.equals(button2)){ 
       tf1.setText("0000"); 
       tf2.setText("0000"); 
          } 

     } 


     } 

回答

1

你是shadowing变量button1。更换

JButton button1 = new JButton("CONVERT"); 

与下面的代码

button1 = new JButton("CONVERT"); 
+0

我试着改变你的建议,但发生错误:线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException \t at Trial $ thehandler.actionPerformed(Trial.java:116)Error at line - a = Double.valueOf(tf1.getText()); –

+0

你也影响变量,'tf1','tf2'等... – Reimeus

+0

我已纠正所有这些错误。现在没有空点异常,但它仍然没有执行任何功能。代码在异常条款中执行。 –

0

用途:

import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 


    public class Trial extends JFrame implements ActionListener 
    { 
      public String[] list={"DOLLARS","EUROS ","YEN","POUNDS","RUPEES"}; 

     JPanel panel1 = new JPanel(); 

     JPanel panel2 = new JPanel(); 
     public JComboBox cb,cb1; 

     public JTextField tf1; 
     public JTextField tf2; 
     public JLabel label3; 
     public JLabel label4; 

     JPanel panel3 = new JPanel(); 

     public JButton button1; 
     public JButton button2; 

     public Trial(){ 
     setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));   
    getContentPane().setLayout(
     new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS) 
    ); 


     JLabel label1 = new JLabel("FROM :"); 
     label1.setFont(new Font("Serif", Font.BOLD, 15)); 

     cb = new JComboBox(); 


     cb= new JComboBox(list); 
     cb.getSelectedIndex(); 
     panel1.setBackground(Color.RED); 
     panel1.add(label1); 
     panel1.add(cb); 


     JLabel label2 = new JLabel("TO :"); 
     label2.setFont(new Font("Serif", Font.BOLD, 15)); 
     cb1 = new JComboBox(); 
     cb1=new JComboBox(list); 
     cb1.getSelectedIndex(); 
     cb.addActionListener(this); 
     cb1.addActionListener(this);  

     panel1.add(label2); 
     panel1.add(cb1); 
     add(panel1); 
     label3 = new JLabel("\n\nENTER THE AMOUNT :"); 
     label3.setFont(new Font("Serif", Font.BOLD, 15)); 
     tf1 = new JTextField(15); 

    // label3.setHorizontalAlignment(SwingConstants.CENTER); 
    // label3.setVerticalAlignment(SwingConstants.CENTER); 
     panel2.add(label3); 
     panel2.add(tf1);  

     label4 = new JLabel("CONVERTED AMOUNT :"); 
     label4.setFont(new Font("Serif", Font.BOLD, 15)); 
     label4.setDisplayedMnemonic(KeyEvent.VK_O); 
     tf2 = new JTextField(15); 

     tf1.addActionListener(this);  
     tf2.addActionListener(this); 

     panel2.add(label4); 
     panel2.add(tf2); 
     panel2.setBackground(Color.BLUE); 
     add(panel2); 

     button1 = new JButton("CONVERT"); 
     button1.setFont(new Font("Serif", Font.BOLD, 15)); 
     button1.setMnemonic(KeyEvent.VK_K); 
     panel3.add(button1); 

     button2 = new JButton("CLEAR "); 
     button2.setFont(new Font("Serif", Font.BOLD, 15)); 
     button2.setMnemonic(KeyEvent.VK_C); 

     button1.addActionListener(this);  
     button2.addActionListener(this);  

     panel3.add(button2); 
     panel3.setBackground(Color.GREEN); 

     add(panel3);  
    } 

     public void actionPerformed(ActionEvent e) { 
      double a,b=0; 
      Object src = e.getSource(); 
      if(src == button1){ 
      a=Double.valueOf(tf1.getText()); 
      try{ 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==1) 
        {b=a*0.89;} 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==2) 
        {b=a*124.75;} 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==3) 
        {b=a*0.65;} 
       if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==4) 
        {b=a*64.08;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==0) 
        {b=a*1.13;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==2) 
        {b=a*140.49;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==3) 
        {b=a*0.74;} 
       if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==4) 
        {b=a*71.34;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==0) 
        {b=a*0.0080;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==1) 
        {b=a*0.0071;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==3) 
        {b=a*0.0052;} 
       if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==4) 
        {b=a*0.51;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==0) 
        {b=a*1.53;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==1) 
        {b=a*1.36;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==2) 
        {b=a*191.26;} 
       if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==4) 
        {b=a*97.88;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==0) 
        {b=a*0.0156;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==1) 
       { b=a*0.014;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==2) 
        {b=a*1.9607;} 
       if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==3) 
        {b=a*0.0108;} 

       tf2.setText(String.valueOf(b)); 

      }catch(Exception x){System.out.println("Error");} 
      } 
      if(src.equals(button2)){ 
       tf1.setText("0000"); 
       tf2.setText("0000"); 
          } 

     }   
     } 

问题与您的代码,有很多局部变量的名字是与实例变量相同的。这些局部变量正在隐藏实例变量。在构造函数中,你写了如JComboBox cb = new JComboBox();这样的语句,这个cb变量是一个局部变量。语句panel1.add(cb);将一个本地cb变量添加到panel1。但在执行时执行cb.getSelectedIndex()==0时,它使用声明为public JComboBox cb,cb1;的实例变量。因为这个实例变量永远不会被添加到gui中。 comboBox用户选择的是本地可变cb而非实例变量cb。

+0

是的,我编辑和它的工作,但你可以解释进一步的概念吗?这将是高度赞赏。 –